【发布时间】:2015-12-18 08:02:07
【问题描述】:
我正在 ANDROID 中使用 BLE 设备。
这里是我设置端点 0000fff4-0000-1000-8000-00805f9b34fb 来接收通知的地方(CLIENT_CHARACTERISTIC_CONFIG 是 00002902-0000-1000-8000-00805f9b34fb
public void k2DigitalNotification(BluetoothGattCharacteristic characteristic,
boolean enabled)
{
Boolean myStatus;
if (MY_BLUETOOTH_SERVICE.equals(characteristic.getUuid()))
{
Log.v(TAG, "Characteristic: " + characteristic.getUuid());
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
myStatus = mBluetoothGatt.writeDescriptor(descriptor);
Log.v(TAG,"Write Status::"+myStatus);
bluetoothGatt.setCharacteristicNotification(characteristic, true);
}
}
这是我写入设备的代码。这行得通! 100%,数据显示在嵌入式 BLE 设备上。 (端点为 0000fff1-0000-1000-8000-00805f9b34fb )
public void k2digitalWriteToCharacteristic(BluetoothGattCharacteristic characteristic) {
if (BLE_ENDPOINT.equals(characteristic.getUuid()))
{
Log.v(TAG,"0Xfff1");
byte[] data3Send = new byte[4];
data3Send[0] = 0x31;
data3Send[1] = 0x01;
data3Send[2] = 0x5a;
data3Send[2] = 0x0d;;
characteristic.setValue(data3Send);
boolean status = mBluetoothGatt.writeCharacteristic(characteristic);
Log.v(TAG, "Status is:" + String.valueOf(status));
}
}
BLE 设备正在发送数据.....我已经在 Nordic Semi 的 nRFMaster 或 Bluepixel 的 BLEScanner 等应用程序中验证了这一点。
但我从未见过回调。
public void onCharacteristicChanged(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic) {
Log.v(TAG,"A Characteristic Change?!?!?!");
byte[] data = characteristic.getValue();
Log.v(TAG,"Here is the data: "+data[0]);
broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
}
有什么帮助吗?我整个星期都在用头撞墙。
【问题讨论】:
-
启用通知非常棘手。我可以发布你可以尝试的各种事情以及你必须注意的事情的答案,但为了快速进行理智检查,我建议尝试SweetBlue,特别是BleDevice.enableNotify()。这是我的库,但老实说,这是了解问题出在 Android 端代码还是其他地方的最快方法,然后你可以从那里开始。
-
感谢您的提示。我现在就下载这个试试看。
-
您是否验证了特征数据可以通过indication而不是notification接收?
-
感谢 Zimano 的提醒。我试过了。实际上,答案是我的一个错误。注意上面的数据包......我覆盖了5a,因为我没有在我发送的数据包中推进数组索引......愚蠢的错误