Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/180.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android BLE-我无法同时读取温度服务和电池服务的两个特征_Android_Bluetooth_Bluetooth Lowenergy_Android Bluetooth - Fatal编程技术网

Android BLE-我无法同时读取温度服务和电池服务的两个特征

Android BLE-我无法同时读取温度服务和电池服务的两个特征,android,bluetooth,bluetooth-lowenergy,android-bluetooth,Android,Bluetooth,Bluetooth Lowenergy,Android Bluetooth,这是我的代码,我在其中发现服务,然后获取温度和电池特性的特征和设置描述符 在开始时,我发现了温度和电池的服务。 然后,我发现每个温度和电池服务的特点 并为两者编写描述符 当我运行代码时,调用将onCharactersticChanged,我得到了温度结果。 但是,在读取电池的字符时,呼叫不会继续 for (BluetoothGattService service : services) { Log.e("asd service discoverd", se

这是我的代码,我在其中发现服务,然后获取温度和电池特性的特征和设置描述符

在开始时,我发现了温度和电池的服务。 然后,我发现每个温度和电池服务的特点 并为两者编写描述符

当我运行代码时,调用将onCharactersticChanged,我得到了温度结果。 但是,在读取电池的字符时,呼叫不会继续

    for (BluetoothGattService service : services) {
                Log.e("asd service discoverd", service.getUuid().toString());

//                check for service should be temperature service or Battery service
                if (service.getUuid().equals(BT_THERMO_SERVICE) || service.getUuid().equals(BT_BATTERY_SERVICE)) {
                    Log.e("asd service discoverd", service.getUuid().toString());


                    List<BluetoothGattCharacteristic> characteristics = service.getCharacteristics();

                    //
                    // Create a compartor
                    // sort list with cpmparor

                    // addd in queue


                    // read same


                    for (BluetoothGattCharacteristic characteristic : characteristics) {
                        Log.e("asd charac discoverd:", characteristic.getUuid().toString());
                        if (characteristic.getUuid().equals(BT_REAL_TIME_TEMPERATURE_CHARTERISTICS) || characteristic.getUuid().equals(BT_BATERY_LEVEL_CHARACTERISTICS)) {
                            Log.e("asd charac discoverd:", characteristic.getUuid().toString());

                            arrayList.add(characteristic);

                            // check if characterstic is RealTime Temperature Measurement characterstic  or Battery Level characterstic
                            if (characteristic.getUuid().equals(BT_REAL_TIME_TEMPERATURE_CHARTERISTICS)) {
                                //indicate ble to send temperature data each time when new data value found

                                //notify ble device to send data
                                gatt.setCharacteristicNotification(characteristic, true);

                                for (BluetoothGattDescriptor descriptor : characteristic.getDescriptors()) {
                                    descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
                                    gatt.writeDescriptor(descriptor);

                                }
                            } else
                            if (characteristic.getUuid().equals(BT_BATERY_LEVEL_CHARACTERISTICS)) {

                                //notify ble device to send data
                                gatt.readCharacteristic(characteristic);

                                for (BluetoothGattDescriptor descriptor : characteristic.getDescriptors()) {
                                    descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
                                    gatt.writeDescriptor(descriptor);

                                }

                            }
                        }
                    }


                }

不幸的是,一次同步发送所有读写请求不起作用,因为android一次只允许一个挂起的GATT操作。您必须以某种方式将工作排队,并在前一个请求的回调到达后继续发送另一个请求。

您能提供一个示例吗
 @Override
        public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
            super.onCharacteristicRead(gatt, characteristic, status);
            Log.e("charvalu", "" + characteristic);
            if (characteristic.getUuid().equals(BT_BATERY_LEVEL_CHARACTERISTICS)) {
                byte b[] = characteristic.getValue();
                if (b.length != 0) {
                    Log.e("battery", Integer.toString(b.length));
//                    ByteBuffer batterybuffer = ByteBuffer.wrap(b);
//                    Long batteryStatus = batterybuffer.getLong();
//                    Log.e("battery","" + batteryStatus);

                }
            }
        }





@Override
        public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
            super.onCharacteristicChanged(gatt, characteristic);
            Log.e("inside char", "" + characteristic);
            if (characteristic != null) {
                Log.e("on change char", characteristic.getUuid().toString());

                if (characteristic.getUuid().equals(BT_REAL_TIME_TEMPERATURE_CHARTERISTICS)) {
                    Log.e("on change inside", characteristic.getUuid().toString());

                    //temperature data comes in byte array of size 12
                    byte b[] = characteristic.getValue();


                    if (b.length != 0) {
                        //check header and tail of data packet
                        if (b[0] == 0x7C && b[11] == 0x7D) {
                            //Temp reading is stored in 7 and 8 byte
                            ByteBuffer tempBuffer = ByteBuffer.wrap(b, 7, 2);
                            tempBuffer.order(ByteOrder.LITTLE_ENDIAN);
                            Short temp = tempBuffer.getShort();
                            final Float fTemp = (float) (temp / 100.0);
                            Log.e("sunittemp", Float.toString(fTemp));
                            runOnUiThread(new Thread() {
                                @Override
                                public void run() {
                                    super.run();
                                    workingListener.unstableReading(new IvyThermoReading(fTemp));

                                }
                            });
                        }
                    }
                }
            }
        }