gpt4 book ai didi

android - 重新连接后无法再写入 BLE 设备的特性

转载 作者:行者123 更新时间:2023-12-05 07:41:33 25 4
gpt4 key购买 nike

我正在将打印数据写入 Zebra ZD410 打印机的 BluetoothGattCharacteristic。为此,我将数据分成 20 个字节的 block ,并使用以下代码一次写入一个 block :

mCharacteristic.setValue(bytes);
boolean status = mGatt.writeCharacteristic(mCharacteristic);

然后等到我收到 BluetoothGattCallback.onCharacteristicWrite(),然后再开始写入下一个 block 。这很好用。

如果我 disconnect()close() BluetoothGatt 之后再次使用 BluetoothDevice.connectGatt 连接到同一设备() 然后在 onServicesDiscovered() 被调用完成后尝试写入 Characteristic 并且我再次拥有我的 Characteristic , 写入会失败。我的意思是,当我现在写入 Characteristic 时,onCharacteristicWrite() 将被调用,CharacteristicgetValue () 返回旧 Gatt 上最后一次写入的值。

在尝试解决这个问题两天并阅读大量 SO 帖子后,我还没有找到解决方案。

我该如何解决这个问题?

编辑这是 BluetoothGattCallback

的代码
private final BluetoothGattCallback gattCallback = new BluetoothGattCallback()
{
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState)
{
FALog.i(TAG, "onConnectionStateChange Status: " + status);
switch (newState)
{
case BluetoothProfile.STATE_CONNECTED:
FALog.i(TAG, "gattCallback STATE_CONNECTED");
gatt.discoverServices();
break;
case BluetoothProfile.STATE_DISCONNECTED:
disconnectAndCloseGatt();
mCharacteristic = null;
connectionFailed();
FALog.e(TAG, "gattCallback STATE_DISCONNECTED");
break;
default:
FALog.e(TAG, "gattCallback STATE_OTHER");
}
}

@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status)
{
BluetoothGattService service = gatt.getService(PRINTER_SERVICE_UUID);
if (service != null)
{
BluetoothGattCharacteristic characteristic = service.getCharacteristic
(PRINTER_SERVICE_CHARACTERISTIC_UUID);
if (characteristic != null)
{
mCharacteristic = characteristic;
mInternalState = STATE_CONNECTED;
mState = State.CONNECTED;
notifyStateChanged();
print("~JA");
FALog.d(TAG, "Printer connected");
mBluetoothActivity.runOnUiThread(new Runnable()
{
@Override
public void run()
{
mListener.onPrinterConnected();
}
});
}
}
}

@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status)
{
FALog.d(TAG, "received onCharacteristicWrite " + new String(characteristic.getValue()) + "; success: " +
(status == BluetoothGatt.GATT_SUCCESS));
if (status == BluetoothGatt.GATT_SUCCESS)
{
handler.removeCallbacks(writeRunnable);
popQueueAndReleaseLock();
}
}
};

最佳答案

onDescriptorWrite() 回调之后尝试 writeCharacteristic 而不是 onServicesDiscovered() 回调。 writeDescriptor 持有 mDeviceBusy。

关于android - 重新连接后无法再写入 BLE 设备的特性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45237878/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com