Android BLE阅读特征

时间:2017-03-26 21:34:45

标签: android bluetooth-lowenergy atmel

我目前正在使用Android应用程序来读取远程设备的温度。在远程设备上,我有一台运行设备信息服务和健康温度计服务的服务器。我的Android应用程序可以扫描并检测服务及其特征。我的应用程序上有一个按钮,点击它应该从远程服务器请求温度。下面是按钮单击执行的代码。

public void readCustomCharacteristic() {
    if (mBluetoothAdapter == null || mBluetoothGatt == null) {
        Log.w(TAG, "BluetoothAdapter not initialized");
        return;
    }
    /*check if the service is available on the device*/
    BluetoothGattService mCustomService = mBluetoothGatt.getService(UUID.fromString("00001809-0000-1000-8000-00805f9b34fb"));
    if(mCustomService == null){
        Log.w(TAG, "Custom BLE Service not found");
        return;
    }

    /*get the read characteristic from the service*/
    BluetoothGattCharacteristic mReadCharacteristic = mCustomService.getCharacteristic(UUID.fromString("00002a1c-0000-1000-8000-00805f9b34fb"));
    Log.i(TAG, mReadCharacteristic.toString());
    BluetoothGattDescriptor descriptor = mReadCharacteristic.getDescriptor(
            UUID.fromString(GattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
    Log.i(TAG, descriptor.toString());
   // final byte[] data = descriptor.getValue();
   // Log.i(TAG, data.toString());
   // descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE);
    if(mBluetoothGatt.readCharacteristic(mReadCharacteristic) == false){
        Log.w(TAG, "Failed to read characteristic");
    }
}

代码与描述符一起正确检测服务,但是在读取特征上,返回的值为false,并且我无法将特征读取为日志消息。有什么我不做的事。对此有任何建议肯定会有所帮助。 谢谢

1 个答案:

答案 0 :(得分:0)

正如您在https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/bluetooth/BluetoothGatt.java#919的源代码中看到的那样,调用返回false基本上有三个原因: 1.特征没有读取属性 2.已经有正在运行的gatt操作,你必须先等待它完成 3. gatt对象已关闭

相关问题