gatt.readCharacteristic中的Android蓝牙状态133

Qua*_*lve 6 android bluetooth bluetooth-lowenergy

在onCharacteristicwrite成功解决熟悉的问题后,我继续133readCharacteristic功能中遇到这些状态.

这里有一个简短的代码:我将特性存储到onServicesDiscovered函数中的变量:

@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 ...
}
Run Code Online (Sandbox Code Playgroud)

然后在写入SYNC_DATA_INPUT_CHAR设备之后,设备将更改其中一个特性的值,我将需要获取该值.所以我在下面写代码.

@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 ...
}
Run Code Online (Sandbox Code Playgroud)

readCharacteristic函数发生错误,它会触发onCharacteristicRead具有状态的函数133.

这是一些日志:

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
Run Code Online (Sandbox Code Playgroud)

正如@benka告诉我的那样,我检查了特性的属性,发现值为10.我认为它应该是2(PROPERTY_READ)+ 8(PROPERTY_WRITE),所以直接调用函数readCharacteristic应该没问题.我将把这个特征的所有属性放在下面.

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
Run Code Online (Sandbox Code Playgroud)

我希望如果有人有这个熟悉的问题,可能会给我一些建议.

非常感谢!

---编辑1

我忘了说,上面的值都是小数,但它可以视为十六进制.

在此输入图像描述

---编辑2

经过很长一段时间的努力,问题仍然没有解决,但我已经做了一些实验.

由于读取的特性是可读写的,我试着在其中写一些东西,只是为了看看会发生什么.

@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 ...
}
Run Code Online (Sandbox Code Playgroud)

出乎意料的是,它产生一个DeadObjectException并退出.这很奇怪,可能是导致问题的一些线索.而且我也认为Unhandled exception: java.lang.NullPointerException: src == null上面的日志也值得挖掘.