【问题标题】:How to write the Characteristics into BLE GATT server in android?如何在 android 中将特征写入 BLE GATT 服务器?
【发布时间】:2016-03-29 04:37:21
【问题描述】:

我正在研究蓝牙低功耗 GATT 以与芯片进行通信。我可以读取芯片的响应,但我无法将特征发送到该芯片并通知一些特征。能不能帮忙啊。

提前致谢。

【问题讨论】:

  • 请说明您使用的是什么蓝牙设备/芯片。请记住,如果它是您从供应商处获得的定制芯片 - 请与供应商联系以了解具体信息。对于这些有限的信息,没有人可以提供帮助。
  • 您好,他们在 BoreCam 芯片和 CoreCam 芯片中提供了一些 MAC 地址
  • 我不知道 BoreCam 是什么,除非它是 Broadcom。如果没有具体的制造商和型号等细节,也没有详细展示您尝试过的东西,没有人可以帮助您。
  • 是不是像Beacon一样为你提供ff2/ ff1之类的服务?

标签: android bluetooth-lowenergy gatt


【解决方案1】:

假设您的 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");
    }

【讨论】:

    【解决方案2】:

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

    在项目中包含该库后,您需要执行以下操作:

    1. 确保蓝牙已启用,并且您已向用户请求位置权限。
    2. 扫描设备

    例子:

    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() 方法写入所需的字节。

    例子:

    device.establishConnection(false)
        .flatMapSingle(rxBleConnection -> rxBleConnection.writeCharacteristic(characteristicUUID, bytesToWrite))
        .subscribe(
            characteristicValue -> {
                // Characteristic value confirmed.
            },
            throwable -> {
                // Handle an error here.
            }
        ); 
    
    1. 如果您想为某个特征设置通知/指示,则可以执行以下操作:

    例子:

    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-02
      • 1970-01-01
      • 2018-04-23
      相关资源
      最近更新 更多