Android Bluetooth - detect a disconnection from a device

15,559

Solution 1

Use this code for receiving a disconnect (when Bluetooth is still turned on):

<intent-filter> <action android:name="android.bluetooth.device.action.ACL_CONNECTED" />
    <action android:name="android.bluetooth.device.action.ACL_DISCONNECT_REQUESTED" />
    <action android:name="android.bluetooth.device.action.ACL_DISCONNECTED" />
</intent-filter>

Also you have to register a service that then registers a broadcast receiver for Bluetooth status change, so your app knows when Bluetooth was turned off and therefore discards all active devices, as you won't receive a ACL_DISCONNECT when Bluetooth is simply turned off.

@Override
    public int onStartCommand(Intent intent, int flags, int startId) {

    IntentFilter filter = new IntentFilter();
    filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
    registerReceiver(bluetoothTurnedOnOff, filter);
    return START_STICKY;
}

private final BroadcastReceiver bluetoothTurnedOnOff = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1) == BluetoothAdapter.STATE_OFF) {
 [...]

Solution 2

For new users. it will works . Your manifest file looks

<receiver android:name="(package name).BluetoothReceiver" >
            <intent-filter>
                <action android:name="android.bluetooth.device.action.ACL_CONNECTED" />
                <action android:name="android.bluetooth.device.action.ACL_DISCONNECTED" />
            </intent-filter>
        </receiver>

and your receiver will look like

else if (intent.getAction().equals(`enter code here`
    BluetoothDevice.ACTION_ACL_DISCONNECTED)) {
    // connection lost

Solution 3

Try removing the <category .../> from the intent-filter and try again.

Share:
15,559
user1528794
Author by

user1528794

Updated on June 10, 2022

Comments

  • user1528794
    user1528794 almost 2 years

    I am trying to catch a bluetooth device disconnection intent filter. I added a log to the onReceive but it never reaches it and is not displayed in the logcat. I suspect that the problem is with my manifest.xml configuration:

    manifest:

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.company"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="10"
            android:targetSdkVersion="16" />
    
        <uses-permission android:name="android.permission.BLUETOOTH" />
        <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    
        <application
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <receiver android:name="com.company.MyReceiver" android:enabled="true" android:exported="true">
                <intent-filter>
                    <category android:name="android.intent.category.DEFAULT" />
                    <action android:name="android.bluetooth.device.action.ACL_DISCONNECT_REQUESTED" />
                    <action android:name="android.bluetooth.adapter.action.CONNECTION_STATE_CHANGED" />
                    <action android:name="android.bluetooth.adapter.action.DISCOVERY_STARTED" />
                </intent-filter>
            </receiver>
    
            <activity
                android:name=".BTActivity"
                android:label="BTActivity" >
            </activity>
        </application>
    
    </manifest>
    

    MyReceiver extends BroadcastReceiver:

    @Override
        public void onReceive(Context context, Intent intent) {
            Log.i("got-in", "got-in-");
            // String action = intent.getAction();
            BluetoothDevice device = intent
                    .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
    
            Log.i("disconnect", device.getName());
            Intent i = new Intent(context, BTActivity.class);
            Bundle b = new Bundle();
            b.putString("deviceName", device.getName());
            intent.putExtras(b); // Put your id to your next Intent
            context.startActivity(i);
            // finish();
    
        }