在小米Band 2中无法读取心率

时间:2019-09-30 17:27:51

标签: android bluetooth xiaomi

我试图从小米Band 2中读取心率值。为此,我尝试使用和改编https://github.com/android/connectivity-samples/tree/master/BluetoothLeGatt中的BluetoothLeGatt项目。

到目前为止,我已经能够调查附近的BLE设备。之后,我选择了小米Band 2,可以成功列出该设备具有的所有服务,包括心率服务。在心率服务内部,我找到了想要的特色心率测量。

我尝试从此特征打印数据,但没有成功。即使做了一些细微的更改,我仍然无法在日志中看到任何数据。

这是我的LEScanCallback(进行了一些小的更改)

    // Implements callback methods for GATT events that the app cares about.  For example,
    // connection change and services discovered.
    private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
        @Override
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
            String intentAction;
            if (newState == BluetoothProfile.STATE_CONNECTED) {
                intentAction = ACTION_GATT_CONNECTED;
                mConnectionState = STATE_CONNECTED;
                broadcastUpdate(intentAction);
                Log.i(TAG, "Connected to GATT server.");
                // Attempts to discover services after successful connection.
                Log.i(TAG, "Attempting to start service discovery:" +
                        mBluetoothGatt.discoverServices());

            } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
                intentAction = ACTION_GATT_DISCONNECTED;
                mConnectionState = STATE_DISCONNECTED;
                Log.i(TAG, "Disconnected from GATT server.");
                broadcastUpdate(intentAction);
            }
        }

        @Override
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
            if (status == BluetoothGatt.GATT_SUCCESS) {
                broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);
            } else {
                Log.w(TAG, "onServicesDiscovered received: " + status);
            }

            BluetoothGattCharacteristic characteristic =
                            gatt.getService(HEART_RATE_SERVICE_UUID)
                            .getCharacteristic(HEART_RATE_MEASUREMENT_CHAR_UUID);

            System.out.println("HEART_RATE_MEASUREMENT_CHAR_UUID: "+HEART_RATE_MEASUREMENT_CHAR_UUID);
            /*gatt.setCharacteristicNotification(characteristic, true);

            BluetoothGattDescriptor descriptor = characteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG_UUID);
            descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
            descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
            gatt.writeDescriptor(descriptor); */

            setCharacteristicNotification(characteristic,true);

        }

        @Override
        public void onCharacteristicRead(BluetoothGatt gatt,
                                         BluetoothGattCharacteristic characteristic,
                                         int status) {
            if (status == BluetoothGatt.GATT_SUCCESS) {
                broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
            }

            System.out.println("onCharacteristicRead: " + Arrays.toString(characteristic.getValue()));

        }

        @Override
        public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status){
            super.onDescriptorWrite(gatt, descriptor, status);

            System.out.println("onDescriptorWrite");
            BluetoothGattCharacteristic characteristic = gatt.getService(HEART_RATE_SERVICE_UUID)
                    .getCharacteristic(HEART_RATE_MEASUREMENT_CHAR_UUID);

            try {
                Thread.sleep(2000);
                } catch (InterruptedException e) {
            e.printStackTrace();
            }
            characteristic.setValue(new byte[]{1, 1});
            //gatt.writeCharacteristic(characteristic);
            boolean success = gatt.readCharacteristic(characteristic);
            System.out.println("success? "+success);
            System.out.println("status: "+status);

        }


        @Override
        public void onCharacteristicChanged(BluetoothGatt gatt,
                                            BluetoothGattCharacteristic characteristic) {
            broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);

            System.out.println("onCharacteristicChanged: " + Arrays.toString(characteristic.getValue()));

        }
    };

我不太了解onCharacteristicRead()或onCharacteristicChanged()的调用。我相信这是因为onDescriptorWrite()函数中的gatt.readCharacteristic(characteristic)为FALSE且状态为3(GATT_WRITE_NOT_PERMITTED)。

有人可以帮我解决问题吗?我开始失去所有希望...

0 个答案:

没有答案