简体   繁体   English

如何在Android的BLE GATT服务器中写入特征?

[英]How to write the Characteristics into BLE GATT server in android?

I am working on Bluetooth low energy GATT to communicate with the chip. 我正在使用蓝牙低功耗GATT与该芯片进行通信。 I can able to read the response from chip but i could not able to send the characteristics into that chip and also notify some characteristics. 我可以读取芯片的响应,但是无法将特性发送到该芯片中,也无法通知某些特性。 Can any only help. 只能帮上什么忙。

Thanks in advance. 提前致谢。

Assuming you have your BluetoothGattServer setup correctly, the characteristics registered with the service and the service added to the BluetoothGattServer, here is an example of sending some data to a notifying characteristic: 假设您已正确设置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);
    }

You will receive an event when the data has been sent in the BluetoothGattServerCallback.onNotificationSent method: BluetoothGattServerCallback.onNotificationSent方法中发送数据后,您将收到一个事件:

    @Override
    public void onNotificationSent(BluetoothDevice device, int status) {
        super.onNotificationSent(device, status);
        Log.d("SVC", "BluetoothGattServerCallback.onNotificationSent");
    }

Well, first of all, I strongly recommend that you use the amazing Bluetooth LE open-source library called RxAndroidBle . 好吧,首先,我强烈建议您使用名为RxAndroidBle的令人惊叹的Bluetooth LE开源库。 It will make the whole process way easier. 这将使整个过程更容易。

Once you have included that library in your project, you will want to do the following: 在将该库包含在项目中之后,您将需要执行以下操作:

  1. Make sure that the Bluetooth is enabled and that you have already asked the user for Location permissions. 确保已启用蓝牙,并且您已经向用户询问了位置权限。
  2. Scan for devices 扫描设备

Example: 例:

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();
  1. Connect to the desired device and use the writeCharacteristic() method to write the bytes you want. 连接到所需的设备,然后使用writeCharacteristic()方法写入所需的字节。

Example: 例:

device.establishConnection(false)
    .flatMapSingle(rxBleConnection -> rxBleConnection.writeCharacteristic(characteristicUUID, bytesToWrite))
    .subscribe(
        characteristicValue -> {
            // Characteristic value confirmed.
        },
        throwable -> {
            // Handle an error here.
        }
    ); 
  1. If instead, you want to set up the notification/indication on a characteristic, you can do the following: 相反,如果要在特征上设置通知/指示,则可以执行以下操作:

Example: 例:

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.
        }
    );

There is plenty of information in their Github page and also they have their own dedicated tag in Stackoverflow . 他们的Github页面上有很多信息,并且在Stackoverflow中还有自己的专用标签

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM