Android Turn On / Off Bluetooth

I have a code in my application where the power button turns on and off bluetooth.

I want to change the background of this button to green and red when bluetooth is on or off. I made Google answers for it, and there is a postoverflow column similar to it, and the way it mentioned does not work. I registered the receiver and have a switching case, as usual, where I mentioned the color change of this button, and it does not work.

Infact log.d in receivers is not even displayed in the Android studio terminal.

THE CODE IS NOT ABOUT HOW TO PERFORM THE COLOR, BUT ACCESS TO CHANGE STATE CHANGE BLUETOOTH FROM BROADCASTING

public void enableDisableBT(){ if(mBluetoothAdapter == null) { Log.d(TAG, "enableDisableBT: Does not have BT capabilities"); Toast.makeText(getApplicationContext(),"No Bluetooth Capability", Toast.LENGTH_SHORT).show(); } if (!mBluetoothAdapter.isEnabled()){ Log.d(TAG, "enableDisableBT: enabling BT"); //btnONOFF.setBackgroundColor(Color.GREEN); // Since bluetooth is NOT enabled, it enables bluetotoh and sets background color to green Intent enableBTIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivity(enableBTIntent); IntentFilter BTIntent = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED); registerReceiver(mBroadcastReceiver1, BTIntent); } if(mBluetoothAdapter.isEnabled()){ Log.d(TAG, "enableDisableBT: disabling BT"); mBluetoothAdapter.disable(); btnONOFF.setBackgroundColor(Color.RED);// Since bluetooth is enabled, it disables bluetotoh and sets background color to green incomingMessages.setText("Incoming Messages"); IntentFilter BTIntent = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED); registerReceiver(mBroadcastReceiver1, BTIntent); } } private final BroadcastReceiver mBroadcastReceiver1 = new BroadcastReceiver() { public void onReceive(Context context, Intent intent) { String action = intent.getAction(); // when discovery finds a device if (action.equals(mBluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED)){ final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,mBluetoothAdapter.ERROR); switch (state){ case BluetoothAdapter.STATE_OFF: Log.d(TAG, "onReceive: STATE OFF"); break; case BluetoothAdapter.STATE_TURNING_OFF: Log.d(TAG, "mBroadcastReceiver1: STATE TURNING OFF"); break; case BluetoothAdapter.STATE_ON: //Log.d(TAG, "mBroadcastReceiver1: STATE ON"); btnONOFF.setBackgroundColor(Color.GREEN); break; case BluetoothAdapter.STATE_TURNING_ON: Log.d(TAG, "mBroadcastReceiver1: STATE TURNING ON"); break; } } } }; 
+1
android bluetooth
Mar 30 '17 at 15:26
source share
1 answer

You need to declare BroadcastReceiver in the manifest in order to listen on the status of Bluetooth. This article sums up: https://droidhat.com/broadcast-receiver-using-change-in-bluetooth-status

one)

 /** * A receiver that listens to Bluetooth getting turned on. */ public class BluetoothReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); int state; switch (action) { case BluetoothAdapter.ACTION_STATE_CHANGED: state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1); if (state == BluetoothAdapter.STATE_ON) { // change my button color } } } } 

2)

 <receiver android:name="com.myapp.BluetoothReceiver" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="android.bluetooth.adapter.action.STATE_CHANGED" /> </intent-filter> </receiver> 
0
Aug 09 '17 at 20:50
source share



All Articles