首页 文章

Android作为BLE外设

提问于
浏览
3

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

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

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

任何帮助将受到高度赞赏 .

1 回答

  • 0

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

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

    并且您可以使用所有模式构建特征

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

    完成后,在构建特性时给出的BluetoothGattServerCallback()中查找onCharacteristicWriteRequest() . 当中心想要发送数据时,它可以使用WRITE模式将数据写入特性,你将在外围端触发onCharacteristicWriteRequest()回调方法,你将获得byte []中的数据,并确保使用btGattServer.sendResponse发送响应(设备, requestId,BluetoothGatt.GATT_SUCCESS,0,null);通过检查回调方法中的responseNeeded bool值 . 通过这种方式,数据从中央传输到外围 .

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

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

    .

相关问题