title: Detect Enable/Disable Bluetooth
body: <p>I made an application to turn on/off bluetooth. To turn on/off i used a switch button. Here is the code:</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  private static final String TAG = "MainActivity";
  private final static int REQUEST_ENABLE_BT = 1;
  BluetoothAdapter mBluetoothAdapter = null;
  
  private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
      public void onReceive(Context context, Intent intent) {
          String action = intent.getAction();
          if (action.equals(mBluetoothAdapter.ACTION_STATE_CHANGED)) {
              final int state = intent.getIntExtra(mBluetoothAdapter.EXTRA_STATE, mBluetoothAdapter.ERROR);
  
              switch (state){
                  case BluetoothAdapter.STATE_OFF:
  
                      break;
                  case BluetoothAdapter.STATE_ON:
                      break;
              }
          }
      }
  };
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      final Switch bluetoothSwitch = (Switch) findViewById(R.id.bluetoothSwitch);
  
      mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
      if (mBluetoothAdapter == null){
          //Device does not support bluetooth
      }
      else{
          bluetoothSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
              @Override
              public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                  if(bluetoothSwitch.isChecked() &amp;&amp; !mBluetoothAdapter.isEnabled()){
                      Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                      startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
  
                      IntentFilter BTIntent = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
                      registerReceiver(mReceiver, BTIntent);
                  }
                  if(!bluetoothSwitch.isChecked() &amp;&amp; mBluetoothAdapter.isEnabled()){
                      mBluetoothAdapter.disable();
                      IntentFilter BTIntent = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
                      registerReceiver(mReceiver, BTIntent);
                  }
              }
          });
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>The problem is that if i turn on/off bluetooth from settings and not from my app the switch doesn't change.
  I have implemented a broadcast receiver but i can't access the switch from it.
  I tried:</p>
  
  <pre><code>bluetoothSwich.setChecked(true) 
  </code></pre>
  
  <p>inside the broadcast receiver but it doesn't work.</p>
  
  <p>Ty</p>
  
  <p>EDIT :
  I partially solved the problem with the global switch but to catch the on/off action from settings first i have to click the on/off button from my app at least one time. any suggestion?</p>