我正在研究蓝牙低功耗GATT与芯片进行通信。我能够从芯片读取响应但我无法将特性发送到该芯片并且还能够通知某些特性。可以任何帮助。
提前致谢。
答案 0 :(得分:0)
假设您已正确设置BluetoothGattServer,已向服务注册的特征以及已将服务添加到BluetoothGattServer,下面是向通知特征发送一些数据的示例:
private static final UUID serviceUuid = UUID.fromString("SOME-SERVICE-UUID");
private static final UUID characteristicUuid = UUID.fromString("SOME-CHAR-UUID");
private BluetoothGattServer gattServer;
private BluetoothDevice peerDevice;
public void sendNotification(byte p1, byte p2, byte p3, byte p4, int correlationid) {
ByteBuffer bb = ByteBuffer.allocate(8);
bb.order(ByteOrder.LITTLE_ENDIAN);
bb.put(p1).put(p2).put(p3).put(p4).putInt(correlationid);
BluetoothGattCharacteristic notifyingChar = gattServer.getService(serviceUuid).getCharacteristic(characteristicUuid);
notifyingChar.setValue(bb.array());
gattServer.notifyCharacteristicChanged(peerDevice, notifyingChar, false);
}
通过BluetoothGattServerCallback.onNotificationSent
方法发送数据后,您将收到一个事件:
@Override
public void onNotificationSent(BluetoothDevice device, int status) {
super.onNotificationSent(device, status);
Log.d("SVC", "BluetoothGattServerCallback.onNotificationSent");
}
答案 1 :(得分:0)
首先,我强烈建议您使用名为RxAndroidBle的令人惊叹的Bluetooth LE开源库。它将简化整个过程。
在该项目中包含该库后,您将需要执行以下操作:
示例:
RxBleClient rxBleClient = RxBleClient.create(context);
Disposable scanSubscription = rxBleClient.scanBleDevices(
new ScanSettings.Builder()
// .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY) // change if needed
// .setCallbackType(ScanSettings.CALLBACK_TYPE_ALL_MATCHES) // change if needed
.build()
// add filters if needed
)
.subscribe(
scanResult -> {
// Process scan result here.
},
throwable -> {
// Handle an error here.
}
);
// When done, just dispose.
scanSubscription.dispose();
writeCharacteristic()
方法写入所需的字节。示例:
device.establishConnection(false)
.flatMapSingle(rxBleConnection -> rxBleConnection.writeCharacteristic(characteristicUUID, bytesToWrite))
.subscribe(
characteristicValue -> {
// Characteristic value confirmed.
},
throwable -> {
// Handle an error here.
}
);
示例:
device.establishConnection(false)
.flatMap(rxBleConnection -> rxBleConnection.setupNotification(characteristicUuid))
.doOnNext(notificationObservable -> {
// Notification has been set up
})
.flatMap(notificationObservable -> notificationObservable) // <-- Notification has been set up, now observe value changes.
.subscribe(
bytes -> {
// Given characteristic has been changes, here is the value.
},
throwable -> {
// Handle an error here.
}
);
他们的Github页面上有很多信息,而且他们还有own dedicated tag in Stackoverflow。