Java onCharacteristicwrite中的Android蓝牙状态133
我是Android新手,现在正在做一个简单的应用程序,需要将一些数据写入外围设备 事实上,三星GT-S7272C设备没有任何问题。但当我切换到索尼LT29i时,当我试图写入某个特性时,总会出现状态133。我将给出一些简短的代码Java onCharacteristicwrite中的Android蓝牙状态133,java,android,bluetooth,bluetooth-lowenergy,Java,Android,Bluetooth,Bluetooth Lowenergy,我是Android新手,现在正在做一个简单的应用程序,需要将一些数据写入外围设备 事实上,三星GT-S7272C设备没有任何问题。但当我切换到索尼LT29i时,当我试图写入某个特性时,总会出现状态133。我将给出一些简短的代码 BluetoothGattService syncService = gatt.getService(SYNC_DATA_SERVICE); BluetoothGattCharacteristic tChar = syncService.getCharacteristic
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);
以及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) {
...
}
fun BluetoothGattCharacteristic.containsProperty(property: Int): Boolean {
return properties and property != 0
}
写入第一个特征没有任何错误,控件将到达onCharacteristicWrite并输入第一个状态为0
的if
语句,这意味着成功。问题是if
语句中的第二个写入操作,它也将触发onCharacteristicWrite函数,但会产生一个状态133
,在中找不到该状态。然后设备自动断开连接
我已经确认数据类型和偏移量都是正确的。因为在另一个设备上它工作得非常好,我认为不同设备之间的蓝牙协议栈实现可能有一些细微的差异,我应该做一些更棘手的事情来解决这个问题
我已经搜索结果很长时间了。一些结果让我找到了C源代码(很抱歉,我将在下面发布链接,因为我没有足够的声誉发布超过2个链接),但我只能发现133
意味着那里的GATT\u错误,这并不比133
更有用。我在谷歌集团也发现了一个问题,讨论了一些熟悉的问题,但我在这里没有找到解决方案
我有点难过,因为,如果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
特性的属性
和写类型
因为特征可以设置值:
无响应写入
或
随响应写入
对于此属性,在将实际数据写入特征之前,必须设置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);
对于那些可能因为状态133 onCharacteristicWrite而找到此帖子的人,我发现我们之所以得到此133结果是因为远程设备断开了连接。我花了很多时间寻找安卓方面的问题,后来才发现问题出在另一边
从这一点上,我推断status=133似乎是某种常见的错误原因。在我的情况下,我需要在一个新的活动中重新使用活动的Gatt连接,但无法,并且经常与错误133断开连接。
因此,我求助于在startActivity()
之前调用BluetoothGatt.close()
,并(重新)连接到onStart()
。
如果有人对如何保持连接有更好的想法,请发布。这里是错误/成功状态代码和含义
GATT\u非法参数
0x0087
(135)
GATT\u NO\u资源
0x0080
(128)
- 关贸总协定内部错误
0x0081
(129)
GATT\u错误状态
0x0082
(130)GATT\u DB\u FULL
0x0083
(131)GATT\u BUSY
0x0084
(132)GATT\u错误
0x0085
(133)GATT\u CMD\u已启动
0x0086
(134)GATT\u未决
0x0088
(136)GATT\u认证失败
0x0089
(137)GATT\u更多信息
0x008a
(138)GATT\u无效\u CFG
0x008b
(139)0x008c
(140)GATT\u ENCRYPED\u MITM
GATT\u成功
GATT\u ENCRYPED\u NO\u MITM
0x008d
(141)GATT\u未加密
0x008e
(142)我遇到了与错误133相同的问题,我必须使用可靠写入来使其工作(很抱歉使用Kotlin代码而不是Java): 使用扩展功能:
@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) {
...
}
fun BluetoothGattCharacteristic.containsProperty(property: Int): Boolean {
return properties and property != 0
}
谢谢你的帮助。这正是我的问题所在。但现在我遇到了另一个类似的问题,那就是当我成功地将某些内容写入设备后立即读取另一个特征时,我也会得到一个
133
。但到目前为止,我还没有找到像ReadType这样的东西。你有什么想法吗@Benkath也有用于读取的属性,但这有点不同:它是读取
,通知
或指示
。基于这些属性,您可以从订阅onCharacteristicChanged
事件的特征中读取。查看我在这里的帖子:如果您需要更多帮助,只需打开一个新问题,发布您的代码和一些详细信息,或者在我发布的关于这个新问题的评论中发布链接。H