安卓作为BLE外设



我正在尝试创建一个Android 5(棒棒糖)应用程序,作为蓝牙低能耗(BLE)外设。该应用程序运行在支持BLE外围设备模式的Nexus 9上。到目前为止,我已经成功地为一项服务做了广告,并允许另一台BLE设备成功连接到它。但是,在尝试读取特征值时失败。

我已经检查了特性是否具有读取属性以及是否设置了读取权限。

使用LightBlue iOS应用程序时(https://itunes.apple.com/gb/app/lightblue-bluetooth-low-energy/id557428110?mt=8)我设法发现并连接到我的Nexus,并看到了特征uuid,但价值没有显示出来。

如有任何帮助,我们将不胜感激。

首先检查您在外围模式中广告的特征数据通常有三种模式

BluetoothGattCharacteristic.PROPERTY_WRITE,      BluetoothGattCharacteristic.PROPERTY_READ,
BluetoothGattCharacteristic.PROPERTY_NOTIFY;

你可以使用建立所有模式的特征

BluetoothGattCharacteristic.PROPERTY_WRITE |BluetoothGattCharacteristic.PROPERTY_READ | BluetoothGattCharacteristic.PROPERTY_NOTIFY;

完成后,在BluetoothGattServerCallback()中查找您在构建特性时给出的onCharacteristicWriteRequest()。当中心想要发送数据时,它可以用write模式将数据写入特征,您将在外围侧触发onCharacteristicWriteRequest()回调方法,您将有字节[]中的数据,并确保使用btGattServer.sendResponse(device,requestId,BluetoothGatt.GATT_SUCCESS,0,null)发送响应;通过检查回调方法中的responseNeeded布尔值。通过这种方式,数据从中央传输到外围。

并将数据从外围设备发送到中央使用通知特征

BluetoothGattCharacteristic bgc = bluetoothGattService
                    .getCharacteristic(chartersticUUID);
                bgc.setValue(bnd.data);
    btGattServer.notifyCharacteristicChanged(centralbluetoothdevice, bgc, false);

最新更新