gpt4 book ai didi

android - Android BLE 数据写入问题 - 获取错误 133

转载 作者:行者123 更新时间:2023-11-29 16:46:25 29 4
gpt4 key购买 nike

在过去的 6 个月里,我一直在研究 BLE 应用程序。在我最近的项目中,我正在集成一个自定义 BLE 设备,它在读取数据时出现了同步问题。 “onCharacteristicRead”始终返回 133 错误代码。请查看完整的场景,如果有人有任何解决方案,请帮助我。

场景自定义 BLE 设备提供三种服务。

  1. 紧急情况

  2. 捕获所有

  3. 电池电量

我的应用程序中只需要两项服务。电池和应急。最初,我将使用以下代码扫描设备,

 private void scan() {
if (isLollyPopOrAbove()) {// Start Scanning For Lollipop devices
mBluetoothAdapter.getBluetoothLeScanner().startScan(scanFilters(), scanSettings(), scanCallback); // Start BLE device Scanning in a separate thread
} else {
mBluetoothAdapter.startLeScan(mLeScanCallback); // Start Scanning for Below Lollipop device
}
}




private List<ScanFilter> scanFilters() {
String emergencyUDID = "3C02E2A5-BB87-4BE0-ADA5-526EF4C14AAA";
ScanFilter filter = new ScanFilter.Builder().setServiceUuid(ParcelUuid.fromString(emergencyUDID)).build();
List<ScanFilter> list = new ArrayList<ScanFilter>(1);
list.add(filter);
return list;
}



private ScanSettings scanSettings() {
ScanSettings settings = new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_BALANCED).build();
return settings;
}

这工作正常,我得到了扫描结果,我可以连接到设备。建立连接后,我将从 GattCallBack 进行以下调用

mBluetoothGatt.discoverServices();

然后我得到服务发现完成的回调

   @Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) { //BLE service discovery complete
if (status == BluetoothGatt.GATT_SUCCESS) { //See if the service discovery was successful
Log.i(TAG, "**ACTION_SERVICE_DISCOVERED**" + status);
broadcastUpdate(BLEConstants.ACTION_GATT_SERVICES_DISCOVERED); //Go broadcast an intent to say we have discovered services
} else { //Service discovery failed so log a warning
Log.i(TAG, "onServicesDiscovered received: " + status);
}
}

从这里我找到了使用提供的 UUID 的可用 MLDP 服务。这也工作正常。

但是当我阅读特征时,

public void readCharacteristic(BluetoothGattCharacteristic characteristic) {
if (mBluetoothAdapter == null || mBluetoothGatt == null) { //Check that we have access to a Bluetooth radio
return;
}
boolean status = mBluetoothGatt.readCharacteristic(characteristic); //Request the BluetoothGatt to Read the characteristic
Log.i(TAG, "READ STATUS " + status);
}

响应状态为真,但“onCharacteristicRead”回调始终为 133

 @Override
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { //A request to Read has completed
//String value = characteristic.getStringValue(0);
//int value = characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT32, 0);
// if(characteristic.getUuid().e)
if (status == BluetoothGatt.GATT_SUCCESS) {
//See if the read was successful
Log.i(TAG, "**ACTION_DATA_READ**" + characteristic);
broadcastUpdate(BLEConstants.ACTION_DATA_AVAILABLE, characteristic); //Go broadcast an intent with the characteristic data
} else {
Log.i(TAG, "ACTION_DATA_READ: Error" + status);
}


}

最佳答案

最后,我找到了处理 133 错误的解决方案。在深入分析的过程中,我发现BLE请求响应过程存在一定的延迟。所以我为每次读写设置了 500 毫秒的延迟。现在它运行良好。我已经在各种设备上测试过它及其工作情况。我不知道这是否是一种解决方法。但这可能对面临类似问题的开发人员有所帮助。

关于android - Android BLE 数据写入问题 - 获取错误 133,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47900831/

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