Catching DeadObjectException that occurred when calling BluetoothGatt.connect () after restarting the Bluetooth chip on Android 4.3 Bluetooth BLE

I am developing an application that uses the new Bluetooth BLE interface from Android 4.3.

I used Samsung BLE Stack for Android 4.2 and it works fine even if stability could be better.

Now with 4.3 for each client connection we have an instance of the class BluetoothGatt.

That is, I connect to the BLE device by calling

BluetoothGatt gatt = device.connectGatt(this, true, callbacks);

These objects BluetoothGattare those that are used to actually interact with the device.

Since I want to connect to and interact with several devices, I cache the instances BluetoothGattin HashMap, defined as a private service property:

private HashMap<String, BluetoothGatt> devicesGatts 
    = new HashMap<String, BluetoothGatt>();

The keys are device addresses.

, , BluetoothGatt HashMap:

public BluetoothGatt connectGatt(final BluetoothDevice device){
    if(device == null){
        return null;
    }
    String adr = device.getAddress();
    BluetoothGatt gatt = devicesGatts.get(adr);
    if(gatt == null){
        gatt = device.connectGatt(this, true, mGattCallbacks);
    } else {
        BluetoothDevice gattDevice = gatt.getDevice();
        if(gattDevice != null && adr.equals(gattDevice.getAddress())){
                gatt.connect(); // PROBLEM APPEARS HERE
        } else {
            gatt = device.connectGatt(this, true, mGattCallbacks);
        }
    }
    devicesGatts.put(adr, gatt);
    return gatt;
}

, , BluetoothGatt, gatt.connect(), ( Fatal) DeadObjectException.

, Bluetooth, .

, , , , , .

:

02-18 10:43:51.884: E/BluetoothGatt(23312): android.os.DeadObjectException
02-18 10:43:51.884: E/BluetoothGatt(23312):     at android.os.BinderProxy.transact(Native Method)
02-18 10:43:51.884: E/BluetoothGatt(23312):     at android.bluetooth.IBluetoothGatt$Stub$Proxy.clientConnect(IBluetoothGatt.java:841)
02-18 10:43:51.884: E/BluetoothGatt(23312):     at android.bluetooth.BluetoothGatt.connect(BluetoothGatt.java:759)
02-18 10:43:51.884: E/BluetoothGatt(23312):     at MYSERVICE.connectGatt(...)

, Bluetooth.

?

+4
1

.

reset HashMap, Bluetooth .

IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
this.registerReceiver(bluetoothStatusChangeReceiver, filter);

private final BroadcastReceiver bluetoothStatusChangeReceiver
= new BroadcastReceiver() {

    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if(action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)){
            if(intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1) 
                    == BluetoothAdapter.STATE_OFF){
                devicesGatts.clear();
                resetBluetooth();
            } else if(intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1) 
                    == BluetoothAdapter.STATE_ON){
                initBluetooth();
            }
        }
    }
}

initBluetooth resetBluetooth reset BluetoothGattServer BluetoothManager.

+1

Source: https://habr.com/ru/post/1527476/


All Articles