【发布时间】:2016-08-30 12:35:51
【问题描述】:
我尝试通过 BLE 通过 Android 设备与 µ 控制器通信。我可以编写我的自定义特征(在我的开发套件上切换 LED),但无法读取 LED 状态的值('0x00' 表示关闭,'0x01' 表示开启)。
我想在单击某个项目时单击 ExpandableListView 时阅读它。目前我在 onChildClickListener 中实现了它。如果一个特征权限"PROPERTY_WRITE" > 0 那么它应该写入值。
private final ExpandableListView.OnChildClickListener servicesListClickListner =
new ExpandableListView.OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition,
int childPosition, long id) {
if (mGattCharacteristics != null) {
final BluetoothGattCharacteristic characteristic =
mGattCharacteristics.get(groupPosition).get(childPosition);
final int charaProp = characteristic.getProperties();
if ((charaProp | BluetoothGattCharacteristic.PROPERTY_READ) > 0) {
// If there is an active notification on a characteristic, clear
// it first so it doesn't update the data field on the user interface.
if (mNotifyCharacteristic != null) {
mBluetoothLeService.setCharacteristicNotification(
mNotifyCharacteristic, false);
mNotifyCharacteristic = null;
}
mBluetoothLeService.readCharacteristic(characteristic);
}
if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
mNotifyCharacteristic = characteristic;
mBluetoothLeService.setCharacteristicNotification(
characteristic, true);
}
if ((charaProp | BluetoothGattCharacteristic.PROPERTY_WRITE) > 0) {
mBluetoothLeService.readCustomCharacteristic();
mBluetoothLeService.writeCustomCharacteristic(0x01);
}
return true;
}
return false;
}
但它总是无法读取特征。 LED 值被写入,LED 切换到“ON”,但它不读取 LED 的值。 我想通过单击列表中的特性来读取打开/关闭 LED 的值。
我不知道我做错了什么。它应该读取特征,将其写入文本字段。在我的 BLE 设备上,我启用了阅读功能。我可以使用gatttool -> char-read-hnd [hnd] [val] 使用我的ubuntu 终端读取该值。
这是我在 mBluetoothLeService 中实现readCustomCharacteristic()
public void readCustomCharacteristic() {
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
Log.w(TAG, "BluetoothAdapter not initialized");
return;
}
/*check if the service is available on the device*/
BluetoothGattService mCustomService = mBluetoothGatt.getService(UUID.fromString("edfec62e-9910-0bac-5241-d8bda6932a2f"));
if(mCustomService == null){
Log.w(TAG, "Custom BLE Service not found");
return;
}
/*get the read characteristic from the service*/
BluetoothGattCharacteristic mReadCharacteristic = mCustomService.getCharacteristic(UUID.fromString("18192021-2223-2425-2627-282930313233"));
mBluetoothGatt.readCharacteristic(mReadCharacteristic);
if(mBluetoothGatt.readCharacteristic(mReadCharacteristic) == false){
Log.w(TAG, "Failed to read characteristic");
}
return;
}
【问题讨论】:
-
为什么要执行两次mBluetoothGatt.readCharacteristic(mReadCharacteristic)?
-
在哪里执行两次?我执行它以执行,如果它成功在 logcat 中打印“读取特征失败”,我需要确认。有没有办法将状态保存在变量中?但是如果我读了两次它应该不会错。它应该只做两次并通过回调和广播更新方法更新我的数据。
-
mBluetoothGatt.readCharacteristic(mReadCharacteristic); if(mBluetoothGatt.readCharacteristic(mReadCharacteristic) == false){ Log.w(TAG, "读取特征失败"); }
-
您首先将它放在一行中,然后在 if 表达式中。 if 表达式可能总是会失败,因为一次只能有一个未完成的 GATT 操作。
-
“失败”是什么意思?它打印到日志中,所以据我了解,if 表达式工作正常。所以“false == false”..我怎样才能将状态放入变量中?
标签: android bluetooth-lowenergy characteristics