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

时间:2017-12-20 07:28:50

标签: android android-studio bluetooth bluetooth-lowenergy

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

情景 定制BLE设备有三种服务可用。

  1. 紧急

  2. 全部抓住

  3. 电池电量

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

     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);
        }
    

    响应状态变为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);
                    }
    
    
            }
    

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

错误代码:133表示GATT__ERROR,设备不在范围内。读取事件期间可能存在错误。我建议先从设备方面查看。

一个问题:这个设备需要绑定连接吗?