首页 > 解决方案 > 从没有 READ 权限的 BLE 客户端读取数据

问题描述

我目前有一个工作的 Android 应用程序(代码片段发布在下面),它扫描 BLE 设备,但使用硬编码的 UUID 仅适用于 1 个特定设备。虽然应用程序成功连接并读取了具有读取权限的数据,但似乎我想要的数据仅带有 NOTIFY 和 WRITE NO RESPONSE,因此无法正常读取。我正在寻找解决这个障碍的解决方案。我已经尝试阅读所有其他服务及其各自的特征,以查看我想要的数据是否在那里,但到目前为止还没有运气。

我已扫描设备以获取其所有服务和特征(提供的所有服务的屏幕截图):

https://imgur.com/a/mYWnAwO

请记住,我知道我正在展示设备的 mac 地址。但是,知道它是什么类型的设备并不重要。

以及整合一个安卓应用程序来读取这些服务的信息。目前,我将 UUID 硬编码并连接到可读的服务/特征,但它的值始终为 0100(显然不是我想要的)。

    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("0000feba-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("0000fa12-0000-1000-8000-00805f9b34fb"));
        if(mBluetoothGatt.readCharacteristic(mReadCharacteristic) == false){
            Log.w(TAG, "Failed to read characteristic");
        }
    }

    public void writeCustomCharacteristic(int value) {
        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("0000feba-0000-1000-8000-00805f9b34fb"));
        if(mCustomService == null){
            Log.w(TAG, "Custom BLE Service not found");
            return;
        }
        /*get the read characteristic from the service*/
        BluetoothGattCharacteristic mWriteCharacteristic = mCustomService.getCharacteristic(UUID.fromString("0000fa11-0000-1000-8000-00805f9b34fb"));
        mWriteCharacteristic.setValue(value,android.bluetooth.BluetoothGattCharacteristic.FORMAT_UINT8,0);
        if(mBluetoothGatt.writeCharacteristic(mWriteCharacteristic) == false){
            Log.w(TAG, "Failed to write characteristic");
        }
    }

我的服务器(外围设备)上显示的数字需要是通过 BLE 捕获的值,并且肯定应该在这些服务之一和一个特定特征下。然而,这完全是找到它并破译它的问题,这样它就变成了我的可读性(Android 应用程序)。

标签: javaandroidandroid-studiobluetooth-lowenergy

解决方案


推荐阅读