Qua*_*lve 15 java android bluetooth bluetooth-lowenergy
我是Android新手,现在正在做一个简单的应用程序,需要将一些数据写入外围设备.
实际上三星GT-S7272C设备没有出错.但是当我切换到Sony LT29i时,当我试图写入某个特征时,总会有状态133.我会给出一些简短的代码.
BluetoothGattService syncService = gatt.getService(SYNC_DATA_SERVICE);
BluetoothGattCharacteristic tChar = syncService.getCharacteristic(SYNC_TIME_INPUT_CHAR);
if (tChar == null) throw new AssertionError("characteristic null when sync time!");
int diff = /*a int*/;
tChar.setValue(diff, BluetoothGattCharacteristic.FORMAT_SINT32, 0);
gatt.writeCharacteristic(tChar);
Run Code Online (Sandbox Code Playgroud)
和onCharacteristicWrite函数:
@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
Log.d(TAG, String.format("Sync: onCharWrite, status = %d", status));
try {
if (status != BluetoothGatt.GATT_SUCCESS) throw new AssertionError("Error on char write");
super.onCharacteristicWrite(gatt, characteristic, status);
if (characteristic.getUuid().equals(SYNC_TIME_INPUT_CHAR)) {
BluetoothGattService syncService = gatt.getService(SYNC_DATA_SERVICE);
BluetoothGattCharacteristic tChar = syncService.getCharacteristic(SYNC_HEIGHT_INPUT_CHAR);
if (tChar == null) throw new AssertionError("characteristic null when sync time!");
tChar.setValue(/*another int*/, BluetoothGattCharacteristic.FORMAT_SINT32, 0);
gatt.writeCharacteristic(tChar);
}
else if {
...
}
} catch (AssertionError e) {
...
}
Run Code Online (Sandbox Code Playgroud)
写入第一个特征没有任何错误,控制将到达onCharacteristicWrite并输入if带状态的第一个语句0,这意味着成功.问题是if语句中的第二个写操作,它还会触发onCharacteristicWrite函数但会产生一个状态133,这在官方站点中找不到.然后设备自动断开连接.
我已经确认数据类型和偏移都是正确的.并且因为在另一个设备中它工作得非常好,我认为不同设备之间的蓝牙堆栈实现可能存在一些细微差别,我应该做一些更棘手的解决这个问题.
我已经搜索了很长时间的结果.一些结果将我引向C源代码(对不起,我将在下面发布链接,因为我没有足够的声誉来发布超过2个链接),但我只能发现那133意味着GATT_ERROR,这没有比只是一个133.我也在google小组中发现了一个问题,讨论了一些熟悉的问题,但我在这里找不到解决方案.
我有点难过,因为如果C代码有问题,即使我找到了什么问题,我仍然无法在我自己的代码中做到正确,对吧?
我希望以前有人有熟悉的经验,可能会给我一些建议.非常感谢!
链接:
C源代码: https://android.googlesource.com/platform/external/bluetooth/bluedroid/+/android-4.4.2_r1/stack/include/gatt_api.h
问题: https://code.google.com/p/android/issues/detail?id=58381
我有一个类似的问题,当我试图写一些我不记得的特征,但如果我有相同的错误代码.(它在一些设备上工作,而在其他设备上没有).
什么原来是这个问题是property的characteristics和writeType.
因为特征可以设置值:
write without response 要么write with response在参考此属性时,您必须writeType在将实际数据写入特征之前进行设置.
一旦获得特征,但在写入之前,您可以设置类型.
BluetoothGattCharacteristic tChar = syncService.getCharacteristic(SYNC_HEIGHT_INPUT_CHAR);
if (tChar == null) throw new AssertionError("characteristic null when sync time!");
// use one of them in regards of the Characteristic's property
tChar.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
//tChar.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
tChar.setValue(/*another int*/, BluetoothGattCharacteristic.FORMAT_SINT32, 0);
gatt.writeCharacteristic(tChar);
Run Code Online (Sandbox Code Playgroud)
这里的错误/成功状态代码和含义
GATT_ILLEGAL_PARAMETER 0x0087 (135)GATT_NO_RESOURCES 0x0080 (128)GATT_INTERNAL_ERROR 0x0081 (129)GATT_WRONG_STATE 0x0082 (130)GATT_DB_FULL 0x0083 (131)GATT_BUSY 0x0084 (132)GATT_ERROR 0x0085 (133)GATT_CMD_STARTED 0x0086 (134)GATT_PENDING 0x0088 (136)GATT_AUTH_FAIL 0x0089 (137)GATT_MORE 0x008a (138)GATT_INVALID_CFG 0x008b (139)GATT_SERVICE_STARTED 0x008c (140)GATT_ENCRYPED_MITM GATT_SUCCESSGATT_ENCRYPED_NO_MITM 0x008d (141)GATT_NOT_ENCRYPTED 0x008e (142)