首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何将这些特性写入android中的BLE GATT服务器?

如何将这些特性写入android中的BLE GATT服务器?
EN

Stack Overflow用户
提问于 2016-03-29 12:37:21
回答 2查看 1.6K关注 0票数 2

我正在研究蓝牙低功耗GATT,以便与芯片进行通信。我可以读取芯片的响应,但我无法将特征发送到芯片中,也无法通知某些特征。只能帮上忙了。

提前谢谢。

EN

回答 2

Stack Overflow用户

发布于 2019-08-08 03:44:24

假设您的BluetoothGattServer设置正确,注册到服务的特征和添加到BluetoothGattServer的服务,以下是向通知特征发送一些数据的示例:

代码语言:javascript
运行
复制
    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方法中发送数据后,您将收到一个事件:

代码语言:javascript
运行
复制
    @Override
    public void onNotificationSent(BluetoothDevice device, int status) {
        super.onNotificationSent(device, status);
        Log.d("SVC", "BluetoothGattServerCallback.onNotificationSent");
    }
票数 0
EN

Stack Overflow用户

发布于 2019-08-08 04:00:09

首先,我强烈建议您使用令人惊叹的蓝牙LE开源库RxAndroidBle。这将使整个过程变得更容易。

一旦您在项目中包含了该库,您将需要执行以下操作:

对于设备,请确保蓝牙已启用,并且您已要求用户提供Location permissions.

  • Scan devices

示例:

代码语言:javascript
运行
复制
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. 连接到所需的设备并使用writeCharacteristic()方法写入所需的字节。

示例:

代码语言:javascript
运行
复制
device.establishConnection(false)
    .flatMapSingle(rxBleConnection -> rxBleConnection.writeCharacteristic(characteristicUUID, bytesToWrite))
    .subscribe(
        characteristicValue -> {
            // Characteristic value confirmed.
        },
        throwable -> {
            // Handle an error here.
        }
    ); 

  1. 如果您要针对特征设置通知/指示,则可以执行以下操作:

示例:

代码语言:javascript
运行
复制
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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36275550

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档