Question

How to disconnect from a device right after connection has been established? I need to prevent my device from data exchange with blacklisted devices

public class BluetoothReceiver extends BroadcastReceiver {
    if (action.equals(BluetoothDevice.ACTION_ACL_CONNECTED)) {
        BluetoothDevice remoteDevice = (BluetoothDevice) intent.getExtras().get(BluetoothDevice.EXTRA_DEVICE);
        // I'd like to disconnect from remoteDevice here 
    }
}

AndroidManifest.xml

<receiver android:name="com.app.receivers.BluetoothReceiver" >
    <intent-filter>
        <action android:name="android.bluetooth.device.action.ACL_CONNECTED" />
    </intent-filter>
</receiver>
Was it helpful?

Solution

I found the following solution.

ACTION_UUID is sent during pairing and a file transfer attempt, and I can get the device due to EXTRA_DEVICE. If I want to immediately disconnect from this device, I can run removeBond

private void removeBond(BluetoothDevice device) {
    try {
        Method m = device.getClass().getMethod("removeBond", (Class[]) null);
        m.invoke(device, (Object[]) null);
    } catch (Exception e) {
        Log.e("TAG", "Failed to disconnect from the device");
    }
}

It is not quite disconnection.

Update #1.

Sometimes removeBond is called, but the device I connected to gets unpaired after a file has been sent/received. So, the only way to prevent a device from data exchange via Bluetooth I know now is to disable its Bluetooth module by calling BluetoothAdapter.getDefaultAdapter().disable()

if (action.equals(BluetoothDevice.ACTION_ACL_CONNECTED)) {
        BluetoothDevice remoteDevice = (BluetoothDevice) intent.getExtras().get(BluetoothDevice.EXTRA_DEVICE);
    if (isBlackListed(remoteDevice)) {
        BluetoothAdapter.getDefaultAdapter().disable();
    } 
}

Benefits

  • Reliability

Drawbacks

  • A headset stops working

OTHER TIPS

private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
        @Override
        public void onConnectionStateChange(BluetoothGatt gatt, int status,
                int newState) {
            // TODO Auto-generated method stub
            String intentAction;
            if(newState == BluetoothProfile.STATE_CONNECTED) {
                intentAction = ACTION_GATT_CONNECTED;
                mConnectionState = STATE_CONNECTED;
                broadcastUpdate(intentAction);
                Log.i(TAG, "Attempting to start service discovery:" + mBluetoothGatt.discoverServices());

            } else if(newState == BluetoothProfile.STATE_DISCONNECTED) {
                intentAction = ACTION_GATT_DISCONNECTED;
                mConnectionState = STATE_DISCONNECTED;
                Log.i(TAG, "Disconnected from GATT server");
                broadcastUpdate(intentAction);
            }
        }
}

after you define mBluetoothGatt ,and you can call the following code after you push the button or any operation:

mBluetoothGatt.disconnect();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top