
我想通过蓝牙在android应用程序中连续从ble硬件读取数据.
连接已完成,我可以将数据从应用程序发送到ble,但是无法从ble读取数据.
从ble获取一些数据时必须调用onCharactersticChanged方法,但此回调方法未调用.我想通知ble但
writeChar.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAulT);BluetoothGattDescriptor descriptor = new BluetoothGattDescriptor(bleutils.CLIENT_CHaraCTERISTIC_CONfig_UUID, BluetoothGattDescriptor.PERMISSION_WRITE_SIGNED);descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);gatt.writeDescriptor(descriptor); // Enable local notifications gatt.setCharacteristicNotification(writeChar, true); boolean succes = gatt.writeDescriptor(descriptor);其返回假
解决方法:
我建议您在订阅特性时应设置ENABLE_NOTIFICATION_VALUE.这是我执行此 *** 作的代码:
public voID onServicesdiscovered(BluetoothGatt gatt, int status) { if (status == BluetoothGatt.GATT_SUCCESS) { List<BluetoothGattService> services = getSupportedGattServices(); for (BluetoothGattService service : services) { if (!service.getUuID().equals(UUID_TARGET_SERVICE)) continue; List<BluetoothGattCharacteristic> gattcharacteristics = service.getcharacteristics(); // Loops through available characteristics. for (BluetoothGattCharacteristic gattCharacteristic : gattcharacteristics) { if (!gattCharacteristic.getUuID().equals(UUID_TARGET_CHaraCTERISTIC)) continue; final int charaProp = gattCharacteristic.getPropertIEs(); if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) { setCharacteristicNotification(gattCharacteristic, true); } else { Log.w(TAG, "Characteristic does not support notify"); } } } } else { Log.w(TAG, "onServicesdiscovered received: " + status); } }那么您可以通过NOTIFICATION订阅特征:
public voID setCharacteristicNotification(BluetoothGattCharacteristic characteristic, boolean enabled) { if (mBluetoothAdapter == null || mBluetoothGatt == null) { Log.w(TAG, "BluetoothAdapter not initialized"); return; } mBluetoothGatt.setCharacteristicNotification(characteristic, enabled); BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID_DESCRIPTOR); descriptor.setValue(enabled?BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE :BluetoothGattDescriptor.disABLE_NOTIFICATION_VALUE); mBluetoothGatt.writeDescriptor(descriptor);}之后,您可以开始写入设备:
@OverrIDe public voID onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) { if (status == BluetoothGatt.GATT_SUCCESS) { if (descriptor.getCharacteristic().getUuID().equals(UUID_TARGET_CHaraCTERISTIC)) { Log.i(TAG, "Successfully subscribed"); } byte[] data = <Your data here>; BluetoothGattService Service = mBluetoothGatt.getService(UUID_TARGET_SERVICE); BluetoothGattCharacteristic charac = Service .getCharacteristic(UUID_TARGET_CHaraCTERISTIC); charac.setValue(data); mBluetoothGatt.writeCharacteristic(charac); } else { Log.e(TAG, "Error subscribing"); } }并且您将收到回调onCharactersticChanged以进行读取,而无需实际调用读取 *** 作.
请记住,mBluetoohGatt一次只能处理1个 *** 作,如果您在上一个未完成的情况下执行另一个 *** 作,则不会放入队列中,但会返回false.
总结以上是内存溢出为你收集整理的从android应用程序中的ble连续读取数据全部内容,希望文章能够帮你解决从android应用程序中的ble连续读取数据所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)