谁把我名字用了! 2019-11-07 19:45 采纳率: 0%
浏览 1685

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

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

  • 写回答

2条回答 默认 最新

  • leeisshe 2019-11-08 14:03
    关注

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

    在回调里面重写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);   
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Jenkins+k8s部署slave节点offline
  • ¥15 微信小游戏反编译后,出现找不到分包的情况
  • ¥15 如何实现从tello无人机上获取实时传输的视频流,然后将获取的视频通过yolov5进行检测
  • ¥15 WPF使用Canvas绘制矢量图问题
  • ¥15 用三极管设计一个单管共射放大电路
  • ¥15 孟德尔随机化r语言运行问题
  • ¥15 pyinstaller编译的时候出现No module named 'imp'
  • ¥15 nirs_kit中打码怎么看(打码文件是csv格式)
  • ¥15 怎么把多于硬盘空间放到根目录下
  • ¥15 Matlab问题解答有两个问题