排队Ble Android只按时发送



我在Ble外围设备上工作,我使用本文中的队列发送数据,但它只工作一次。

在成功连接后,我收到了来自Central的请求;0301";然后按照同样的特点,我设置了数据(ssid(并通知它,所以现在一切都很好,应用程序(peroppheral(停止接收请求。

@Override
public void onCharacteristicWriteRequest(final BluetoothDevice device, final int requestId, final BluetoothGattCharacteristic characteristic, boolean preparedWrite, boolean responseNeeded, final int offset, final byte[] value) {
super.onCharacteristicWriteRequest(device, requestId, characteristic, preparedWrite, responseNeeded, offset, value);
final String hexValue = byteArrayToHexString(value);
// 0x83, 0x01, 0x41, 0x62, 0x63
final byte[] ssid  = new byte[]{(byte)  0x83, (byte) 0x01, (byte) 0x41, (byte) 0x62, (byte) 0x63};
final byte[] pwd  = new byte[]{(byte)  0x01, (byte) 0x02};
BluetoothGattCharacteristic charact = mBluetoothGattServer.getService(serviceUUID).getCharacteristic(characteristicUUID);

if(hexValue.equalsIgnoreCase("0301"))
writeQueue.add(ssid);
if(hexValue.equalsIgnoreCase("0401"))
writeQueue.add(pwd);
if (isWriting) {
return;
}
if (writeQueue.size() == 0) {
return;
}
isWriting = true;
charact.setValue(writeQueue.poll());
boolean result =  mBluetoothGattServer.notifyCharacteristicChanged(device, charact , false);

if (responseNeeded) {
boolean sendResponse =  mBluetoothGattServer.sendResponse(device,
requestId,
BluetoothGatt.GATT_SUCCESS,
0,
null);

}
}

正如文档所说:"应用程序必须调用BluetoothGattServer#sendResponse才能完成请求">

https://developer.android.com/reference/android/bluetooth/BluetoothGattServerCallback#onCharacteristicWriteRequest(android.bluetooth.BluetoothDevice,%20int,%20android.bbluetoothGattCharacteristic,%20boolean,%20integ,%20byte[](

最新更新