Android Bluetooth status 133 in gatt.readCharacter

2019-06-20 05:56发布

问题:

After successfully solving the familiar problem in onCharacteristicwrite, I continue to encounter those status 133 in readCharacteristic function.

A brief code here: I store characteristics to variables in onServicesDiscovered function:

@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
    try {
        syncDataService = gatt.getService(GiraffeFriendAttributes.SYNC_DATA_SERVICE);
        if (syncDataService == null) throw new AssertionError("sync data service null!");
        syncDataInputChar = syncDataService.getCharacteristic(GiraffeFriendAttributes.SYNC_DATA_INPUT_CHAR);
        syncDataOutputChar = syncDataService.getCharacteristic(GiraffeFriendAttributes.SYNC_DATA_OUTPUT_CHAR);
        if (syncDataInputChar == null || syncDataOutputChar == null) throw new AssertionError("sync data service null!");
        ...
    } catch ...
}

And then after some writing of SYNC_DATA_INPUT_CHAR into the device, the device will change the value of one of it's characteristic and I will need to fetch that value. So I write codes below.

@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
    Log.d(TAG, String.format("Sync: onCharWrite, status = %d", status));
    try {
        ...
        else if (characteristic.getUuid().equals(SYNC_DATA_INPUT_CHAR)) {
            Log.d(TAG, String.format("Sync: on write data index: %x, %x", dataIndexs[0], dataIndexs[1]));
            gatt.readCharacteristic(syncDataOutputChar);
        }
    } catch ...
}

Error occurs in the readCharacteristic function, it triggers the onCharacteristicRead function with status 133.

Here are some logs:

D/BluetoothGatt﹕ writeCharacteristic() - uuid: 0000ffa6-0000-1000-8000-00805f9b34fb
D/BluetoothGatt﹕ onCharacteristicRead() - Device=78:A5:04:3D:4F:C6 UUID=0000ffab-0000-1000-8000-00805f9b34fb Status=133
W/BluetoothGatt﹕ Unhandled exception: java.lang.NullPointerException: src == null
onClientConnectionState() - status=0 clientIf=4 device=78:A5:04:3D:4F:C6

As @benka has told me, I've checked the properties of the characteristic and found that the value is 10. I thought it should be 2(PROPERTY_READ) + 8(PROPERTY_WRITE), so directly call function readCharacteristic should be OK. I will put all attributes of the characteristic below.

syncDataOutputChar = {android.bluetooth.BluetoothGattCharacteristic@830030678056}
    mDescriptors = {java.util.ArrayList@830030679448} size = 0
    mValue = null
    mUuid = {java.util.UUID@830030680416}"0000ffab-0000-1000-8000-00805f9b34fb"
    mService = {android.bluetooth.BluetoothGattService@830031005904}
    mProperties = 10
    mPermissions = 0
    mKeySize = 16
    mInstance = 0
    mWriteType = 2

I hope if anyone has this familiar problem and may kindly give me some suggestions.

Thanks a lot!

--- EDIT 1

I forgot to say that, the value above is all decimal, it can be view as hex though.

--- EDIT 2

After trying for a long time, problem remains unsolved, yet I've made some experiments.

Since the characteristic to read is both readable and writable, I tried to write something into it, just to see what will happen.

@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
    Log.d(TAG, String.format("Sync: onCharWrite, status = %d", status));
    try {
        ...
        else if (characteristic.getUuid().equals(SYNC_DATA_INPUT_CHAR)) {
            Log.d(TAG, String.format("Sync: on write data index: %x, %x", dataIndexs[0], dataIndexs[1]));
            //gatt.readCharacteristic(syncDataOutputChar);
            syncDataOutputChar.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
            syncDataOutputChar.setValue(0, BluetoothGattCharacteristic.FORMAT_SINT32, 0);
            gatt.writeCharacteristic(syncDataOutputChar);
        }
    } catch ...
}

Unexpectedly, it yields a DeadObjectException and quits. This is weird and likely to be some clues leading to the problem. And I also think the Unhandled exception: java.lang.NullPointerException: src == nullin the logs above is worth digging too.