Reputation: 154
I am facing the issue in Bluetooth event specially disconnecting event is not receiving but rest of the events are receiving. Why does the disconnection event is miss?.
public class BTReciver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
//Device found
Log.d(TAG, "onReceive: Bluetooth ACTION_FOUND ");
} else if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
//Device is now connected
Log.d(TAG, "onReceive: Bluetooth ACTION_ACL_CONNECTED ");
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
//Done searching
Log.d(TAG, "onReceive: Bluetooth ACTION_DISCOVERY_FINISHED ");
} else if (BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED.equals(action)) {
//Device is about to disconnect
Log.d(TAG, "onReceive: Bluetooth REQUESTED disconnected ");
} else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
//Device has disconnected
Log.d(TAG, "onReceive: Bluetooth has been disconnected ");
}
}
<receiver android:name=".BTReciver"
android:exported="true">
<intent-filter>
<action android:name="android.bluetooth.device.action.ACL_CONNECTED" />
<action android:name="android.bluetooth.device.action.ACL_DISCONNECTED" />
<action android:name="android.bluetooth.device.action.ACL_DISCONNECT_REQUESTED" />
</intent-filter>
</receiver>```
Upvotes: 0
Views: 413
Reputation: 154
I have fixed this issue by using of BluetoothAdapter action instead of the BluetoothDevice action.
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,
BluetoothAdapter.ERROR);
switch (state) {
case BluetoothAdapter.STATE_OFF:
Log.d(TAG, "onReceive: STATE_OFF");
break;
case BluetoothAdapter.STATE_TURNING_OFF:
Log.d(TAG, "onReceive: STATE_TURNING_OFF");
break;
case BluetoothAdapter.STATE_ON:
Log.d(TAG, "onReceive: STATE_ON");
break;
case BluetoothAdapter.STATE_TURNING_ON:
Log.d(TAG, "onReceive: STATE_TURNING_ON");
break;
}
}
}
};
IntentFilter flBluetooth= new IntentFilter();
flBluetooth.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
registerReceiver(mReceiver, flBluetooth);
Upvotes: 1