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

Android BLE数据写入问题-获取错误133,android,android-studio,bluetooth,bluetooth-lowenergy,Android,Android Studio,Bluetooth,Bluetooth Lowenergy,在过去的6个月里,我一直在开发BLE应用程序。 在我最近的项目中,我集成了一个定制的BLE设备,它在读取数据时产生了一个同步问题。“onCharacteristicRead”始终返回133个错误代码。请浏览完整的场景,如果有人有任何解决方案,请帮助我 情景 自定义BLE设备有三种可用服务 紧急情况 一网打尽 电池电量 我的申请中只需要两项服务。电池和紧急情况。 首先,我将使用以下代码扫描设备 private void scan() { if (isLollyPopOrAbove

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

情景 自定义BLE设备有三种可用服务

  • 紧急情况

  • 一网打尽

  • 电池电量

  • 我的申请中只需要两项服务。电池和紧急情况。 首先,我将使用以下代码扫描设备

     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;
        }
    
    然后,我将完成服务发现回调

       @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);
        }
    
    响应状态为true,但“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毫秒的延迟。现在它工作得很好。我已经在各种设备上测试了它的工作性能。我不知道这是否是一个解决办法。但这可能对面临类似问题的开发人员有所帮助

    是的,我还添加了粘合。现在,人们的反应越来越强烈。但仍然是随机投掷133。我面临同样的问题,然后我也使用了延迟。。。但这不是一个好的解决方案,因为当您的距离更长时,回调可能需要500毫秒以上。最好的解决方案是依赖读/写回调。我正在捕获读写回调(onCharacteristicRead()和onCharacteristicWrite())。但这里的问题是,我在读写回调中毫不延迟地得到GATT错误。我认为没有解决这个问题的有效方法。当您写入多个值或读取多个值时会发生错误,对吗?如果是,那么我有一个解决方案。是的,我正在阅读一些“连接服务”。在读取回调中,我正在将应用程序状态写入同一服务。在写回调中,我正在读取“紧急服务”状态,并将应用程序状态写入读回调。最后,我在读电池。每次读写时,我都需要一些延迟。对于多次读取,请尝试解决方案。对于多篇文章,问一个问题,我会在那里发布答案。
     @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);
                    }
    
    
            }