蓝牙BLE开发,使用特征发送数据问题

Android 码拜 7年前 (2017-04-26) 2775次浏览
最近在做蓝牙功能开发,先是开启蓝牙,然后扫描,结果用ListView显示,点击设备连接,现在卡在数据写入与读取的问题,这个设备只有一个核心服务,服务里只有一条特征,本人有3条指令要发送,根据设备的状态,返回不同的值,可是现在wrtie方法写入指令后,没有数据返回.求高手指点下
解决方案

50

onCharacteristicChanged()中收不到数据是吗?
在发现service后
public void setNotification(List<BluetoothGattService> gattServices, String uuid) {
for (BluetoothGattService gattService : gattServices) {
Log.d(TAG, “service:” + gattService.getUuid());
List<BluetoothGattCharacteristic> gattCharacteristics =gattService.getCharacteristics();
for (final BluetoothGattCharacteristic  gattCharacteristic: gattCharacteristics) {
Log.d(TAG, “charactor:” + gattCharacteristic.getUuid());
if(gattCharacteristic.getUuid().toString().contains(uuid)) {
BluetoothGattDescriptor descriptor = gattCharacteristic.getDescriptor(BLEConst.NOTIFICATION_UUID);
if(descriptor != null) {
Log.d(TAG, “descriptor:” + descriptor.getUuid());
Log.d(TAG, “enable notify uuid:” + descriptor.getUuid());
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
ble.writeDescriptor(descriptor);
ble.setCharacteristicNotification(gattCharacteristic, true);
}
else{
Log.e(“TAG”,”getDescriptor return null uuid is ” + BLEConst.NOTIFICATION_UUID);
}
}
}
}
}

10

 public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic,
boolean enabled) {
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
Log.w(TAG, “BluetoothAdapter not initialized”);
return;
}
mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
// This is specific to Heart Rate Measurement.
//        if (UUID_HEART_RATE_MEASUREMENT.equals(characteristic.getUuid())) {
//            BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
//                    UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
//            descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
//            mBluetoothGatt.writeDescriptor(descriptor);
//        }
}
//    public void setCharacteristicServerNotification(
//            BluetoothGattCharacteristic characteristic, boolean enabled) {
//        if (mBluetoothAdapter == null || mBluetoothGatt == null) {
//            Log.w(TAG, “BluetoothAdapter not initialized”);
//            return;
//        }
//
//        try {
//            BluetoothGattDescriptor descriptor = characteristic
//                    .getDescriptor(UUID
//                            .fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
//            if (descriptor != null) {
//                Log.e(TAG,
//                        “set descriptor” + descriptor + “|”+descriptor.getValue());
//                if (enabled)
//                    descriptor
//                            .setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
//                else
//                    descriptor
//                            .setValue(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
//                mBluetoothGatt.writeDescriptor(descriptor);
//            }
//        } catch (NullPointerException e) {
//            e.printStackTrace();
//        } catch (IllegalArgumentException e) {
//            e.printStackTrace();
//        }
//    }
有  setCharacteristicNotification 吗
一般在发现服务的广播接收中调用setCharacteristicNotification   有的蓝牙可能还需要setCharacteristicServerNotification

40

没有数据返回一般需要检查设备能否支持通知方式,再者看有没有注册可通知的服务,除直接读取的服务外,一般设备返回的数据都是通过通知方式给到的,看看你有没有在写入数据前进行可通知服务的注册,没有具体代码没法具体分析你的问题,本人最近也在做蓝牙相关的项目,本人这里有个开源库,使用方式介绍http://blog.csdn.net/xiaoyaoyou1212/article/details/52346904,希望对你的项目开发有所帮助!

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明蓝牙BLE开发,使用特征发送数据问题
喜欢 (0)
[1034331897@qq.com]
分享 (0)