【发布时间】:2015-05-24 04:38:55
【问题描述】:
我希望读取存储在设备中的特征值,修改该值,然后将其写入设备。出于某种原因,writeCharacteristic() 返回 true,但内部设备值没有改变,也没有调用 onCharacteristicWrite()。事实上,出于某种原因,它仅在我尝试编写某些内容时被调用,然后在我关闭或重新打开应用程序后不久被调用 - 并且不会更改设备值。我已经研究了几天,这让我发疯了。值得注意的是,手动和通过通知读取特征都可以正常工作。
为什么writeCharacteristic() 没有通过,为什么onCharacteristicWrite() 在如此奇怪的时间被调用?
我不确定问题出在哪里。这可能是一些愚蠢而简单的事情,比如错误地调用writeCharacteristic()(我的实现基于问题“Working with BLE Android 4.3 how to write characteristics?”)。是否有可能因为onCharacteristicRead() 未完成而忽略该请求?
我认为这些链接似乎最有助于指出问题,但我自己无法从中提取任何内容:
- Android BLE API: GATT Notification not received
- https://code.google.com/p/android-developer-preview/issues/detail?id=1714
作为我的代码的演练,onItemClick() 事件被触发,它启动了序列。
int flag = 1;
((MainActivity)getActivity()).BtService.readFlag(flag);
它调用一个函数(在Service 中)验证蓝牙连接并读取特征。
public boolean readFlag(int flag){
/*... removed code here verifies that the Bluetooth Gatt is available,
that the Service exists...*/
BluetoothGattCharacteristic characteristic = Service.getCharacteristic(SEND_FLAGS_CHAR);
if (characteristic == null) {
Log.e(TAG, "char not found!");
return false;
}
try {
// Store intended flag value in SharedPreferences, then read the current one.
Editor fEditor = sPrefs.edit();
fEditor.putInt(calibrationSetFlagToSendKey, flag);
fEditor.commit();
mConnectedGatt.readCharacteristic(characteristic);
// Catch response in onCharacteristicRead() callback.
return true;
}
catch (NullPointerException e) {
Log.w("readCharacteristic", "The characteristic could not be read.");
return false;
}
}
调用onCharacteristicRead(),它只调用一个Handler来修改并将结果广播回MainActivity。 trueFlagValue 是一个全局存储在 Service 中的字节(我知道这是不好的做法,但我打算稍后修改。)
case MSG_SEND_FLAG:
characteristic = (BluetoothGattCharacteristic) msg.obj;
if (characteristic.getValue() == null) {
Log.w(TAG, "Error obtaining current flags");
return;
}
int recentReadDeviceFlag = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0);
int initialFlag = sPrefs.getInt(calibrationSetFlagToSendKey, 0);
if (initialFlag == 0) Log.e("Write Debug", "Flag to send is apparently 0");
/*... Arithmetic modifying flag integer value...*/
//Desired value is the OR'd value of the value read and value desired
trueFlagValue = (byte) ((byte) initialFlag | (byte) recentReadDeviceFlag);
Log.d("Read Debug", "OR'd value is " + trueFlagValue +", sending broadcast");
Intent fIntent = new Intent("com.example.appwithble.SEND_FLAGS");
sendBroadcast(fIntent);
break;
MainActivity 中的BroadcastReceiver 然后调用我的Service 中的一个方法,验证请求writeCharacteristic(),就像对readCharacteristic() 所做的那样。访问之前设置的trueFlagValue。
else if("com.example.appwithble.SEND_FLAGS".equals(intent.getAction())) {
BtService.performWriteFlag();
}
// ...
public boolean performWriteFlag () {
/*... check Gatt is available and that Service exists...*/
BluetoothGattCharacteristic characteristic = Service.getCharacteristic(SEND_FLAGS_CHAR);
if (characteristic == null) {
Log.e(TAG, "char not found!");
return false;
}
byte[] byteValue = new byte[1];
byteValue[0] = trueFlagValue;
characteristic.setValue(byteValue);
boolean status = mConnectedGatt.writeCharacteristic(characteristic);
return status;
}
然后应该调用我的onCharacteristicWrite() 回调,目前只包含一行代码来记录一条消息。实际上,只有在应用程序关闭或重新打开时才会调用它。存储在我的外围设备特性中的值永远不会改变。
【问题讨论】:
标签: android bluetooth bluetooth-lowenergy