BLE - 我无法同时读取温度服务和电池服务中的另一个特性

时间:2016-09-22 12:12:01

标签: android bluetooth bluetooth-lowenergy android-bluetooth

这是我的代码,我发现服务,然后获得温度和电池特性的特性和设置描述符。

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

当我运行代码时,调用将转到onCharactersticChanged并获得温度结果。 但是,OnCharactersticRead没有打电话

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

                                }

                            }
                        }
                    }


                }

这些是我的gattcallback方法。

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

                                }
                            });
                        }
                    }
                }
            }
        }

1 个答案:

答案 0 :(得分:1)

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