Pages

Thursday, April 23, 2015

Android BluetoothAdapter state change listner


In this post I have write code for BuletoothAdapter state change listener. When we use Bluetooth in application then we can check bluetooth is On or Off. But on run time we can use BroadcardReceiver.

Register a BroadcastReceiver to listen for any state changes of the BluetoothAdapter.


Add below code in Activity Class.

Create BroadcastReciver instance variable in your activity (also you can create separate class file)


private final BroadcastReceiver mbluetoothStateReceiver = new BroadcastReceiver() {
  @Override
  public void onReceive(Context context, Intent intent) {
   final String action = intent.getAction();

   if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
    final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
    switch (state) {
    case BluetoothAdapter.STATE_OFF:
     Toast.makeText(getApplicationContext(), "Bluetooth off", Toast.LENGTH_SHORT).show();
     break;
    case BluetoothAdapter.STATE_TURNING_OFF:
     Toast.makeText(getApplicationContext(), "Turning Bluetooth off...", Toast.LENGTH_SHORT).show();
     break;
    case BluetoothAdapter.STATE_ON:
     Toast.makeText(getApplicationContext(), "Bluetooth on", Toast.LENGTH_SHORT).show();
     break;
    case BluetoothAdapter.STATE_TURNING_ON:
     Toast.makeText(getApplicationContext(), "Turning Bluetooth on...", Toast.LENGTH_SHORT).show();
     break;
    }
   }
  }
 };


and Then add code to Register and Unregister BroadcastReceiver as follows


@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_bluetoothlistner);
 
  // Register broadcasts receiver for bluetooth state change
  IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
  registerReceiver(mbluetoothStateReceiver, filter);
 }
 
 @Override
 public void onDestroy() {
  super.onDestroy();
  // Unregister broadcast listeners
  unregisterReceiver(mbluetoothStateReceiver);
 }


Full code of the Activity

public class BluetoothStateListenerActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_bluetoothlistner);

  // Register broadcasts receiver for bluetooth state change
  IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
  registerReceiver(mbluetoothStateReceiver, filter);
 }

 private final BroadcastReceiver mbluetoothStateReceiver = new BroadcastReceiver() {
  @Override
  public void onReceive(Context context, Intent intent) {
   final String action = intent.getAction();

   if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
    final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
    switch (state) {
    case BluetoothAdapter.STATE_OFF:
     Toast.makeText(getApplicationContext(), "Bluetooth off", Toast.LENGTH_SHORT).show();
     break;
    case BluetoothAdapter.STATE_TURNING_OFF:
     Toast.makeText(getApplicationContext(), "Turning Bluetooth off...", Toast.LENGTH_SHORT).show();
     break;
    case BluetoothAdapter.STATE_ON:
     Toast.makeText(getApplicationContext(), "Bluetooth on", Toast.LENGTH_SHORT).show();
     break;
    case BluetoothAdapter.STATE_TURNING_ON:
     Toast.makeText(getApplicationContext(), "Turning Bluetooth on...", Toast.LENGTH_SHORT).show();
     break;
    }
   }
  }
 };

 @Override
 public void onDestroy() {
  super.onDestroy();
  // Unregister broadcast listeners
  unregisterReceiver(mbluetoothStateReceiver);
 }
}


Add below permission on AndroidManifest.xml file

<uses-permission android:name="android.permission.BLUETOOTH" />

Thank you :)