Android BLE 在不知道蓝牙设备的Notify特征的情况下如何接收数据

之前有个设备,使用nRFConnect工具扫描出了所有的UUID,然后通过Notify的特征值实现了接收数据。今天忽然换了个不知道从哪儿找来的设备,居然扫描不出Notify。但是用nRFConnect工具依然可以收到数据,而且还是通过Notify的方式。这就很懵了,现在这个设备用我自己的代码是能发数据,但是不能接收数据。可否给个思路,图在下面~~~然后多句嘴,这个设备是老板(兼硬件)不知道从哪儿翻出来的,估计是很久以前的库存,为了节约成本已经决定用这款设备了,问他一问三不知……图片说明

不能接受数据是因为权限没给

在回调里面重写onServicesDiscovered

    @Override
    public void onServicesDiscovered(BluetoothGatt gatt, int status) {
        super.onServicesDiscovered(gatt, status);
        Log.e(TAG, "onServicesDiscovered gatt = " + gatt);
        if (status == BluetoothGatt.GATT_SUCCESS) { 
            String address = gatt.getDevice().getAddress(); 
            threadSleep(200);
            displayGattServices(gatt, address); 
        }  
    } //发现所连接设备的服务和特征

对所有的特征都付给权限,肯定能行(我这里是对特征的uuid做了过滤,你不知道UUID的情况下可以不过滤,对所有的特征都赋权限)

private void displayGattServices(BluetoothGatt gatt, String address) { 
    List<BluetoothGattService> gattServices = getSupportedGattServices(gatt);
    if (gattServices == null) return;
    String uuid = null;
    for (BluetoothGattService gattService : gattServices) {
        // 列表中有(gattServices) gattServices 此处遍历每一个gattService 找到单片机的通知特征 打开通知(后续就可以自动接到消息)
        uuid = gattService.getUuid().toString();
        Log.e(TAG, "displayGattServices uuid = " + uuid);
        if (uuid.equals(SERVICE_UUID)) {
            mReadCharacteristic = gattService.getCharacteristic(UUID.fromString(NOTIFICATION_UUID));
            mWriteCharacteristic = gattService.getCharacteristic(UUID.fromString(WRITE_UUID)); 
        }
    } 
    if (mReadCharacteristic != null) {
        final int charaProp = mReadCharacteristic.getProperties();
        if ((charaProp | BluetoothGattCharacteristic.PROPERTY_READ) > 0) {
            Log.e(TAG, "displayGattServices PROPERTY_READ > 0 ");
            readCharacteristic(gatt, mReadCharacteristic);
        }
        threadSleep(1000);
        if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
            Log.e(TAG, "displayGattServices PROPERTY_NOTIFY > 0 ");
            setCharacteristicNotification(gatt, mReadCharacteristic, true);
        }
    } else {
        Log.w(TAG, "unable to find NOTIFICATION_UUID");
    }
}

赋权限方法如下

public void readCharacteristic(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
    if (gatt == null || mBluetoothAdapter == null) {
        Log.w(TAG, "BluetoothAdapter not initialized");
        return;
    } 
    gatt.readCharacteristic(characteristic);
}

public void setCharacteristicNotification(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, boolean enable) {
    if (gatt == null || mBluetoothAdapter == null) {
        Log.w(TAG, "BluetoothAdapter not initialized");
        return;
    } 
    gatt.setCharacteristicNotification(characteristic, enable);
    BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID.fromString(CLIENT_CHARACTERISTIC_CONFIG));
    descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
    gatt.writeDescriptor(descriptor);   
}

这个问题已解决,写在了这篇博客里https://blog.csdn.net/baidu_36743221/article/details/102967689