Queue Ble Android 只按时发送

Queue Ble Android send only on time

我在 Ble 外围设备上工作,我使用了这个 article 的队列来发送数据,但它只工作了一次。

在成功连接值“0301”后,我收到了来自 Central 的请求,然后在相同的特征中我设置了数据 (ssid) 并通知了它,所以现在一切正常,应用程序(peropheral ) 停止接收请求。

@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.bluetooth.BluetoothGattCharacteristic,%20boolean,%20boolean,%20int,%20byte[])