【问题标题】:BLE receiving GATT notifications from a characteristicBLE 从特性接收 GATT 通知
【发布时间】:2018-02-16 00:14:54
【问题描述】:

我想在此特性更改时收到通知Micro:Bit

我在做的基本上是这样的:

1) 检查系统是否兼容BLE

2) 启用蓝牙以防它被禁用

3) 连接到唯一配对的设备(Micro:Bit)

4) 连接更改时激活此代码(¿已连接/已断开连接?)

5) 特性更新时激活此代码 ¿?

public class MainActivity extends Activity {

BluetoothAdapter bleAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    **(1)**

    if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
        Toast.makeText(this, "BLE Not Supported", Toast.LENGTH_SHORT).show();
        finish();
    }

    **(2)**

    bleAdapter = ((BluetoothManager) getSystemService(BLUETOOTH_SERVICE)).getAdapter();

    if (bleAdapter == null || !bleAdapter.isEnabled()) {
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBtIntent, 1);
    }

    Set<BluetoothDevice> pairedDevices = bleAdapter.getBondedDevices();

    for (BluetoothDevice device : pairedDevices) {

        **(3)**

        device.connectGatt(this, true, new BluetoothGattCallback() {

            **(4)**

            @Override
            public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
                super.onConnectionStateChange(gatt, status, newState);
                switch (newState) {
                    case BluetoothProfile.STATE_CONNECTED:
                        gatt.setCharacteristicNotification("6E400003B5A3F393E0A9E50E24DCCA9E", true); // This doesn't work
                        break;
                }
            }

            **5**

            @Override
            public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
                super.onCharacteristicChanged(gatt, characteristic);

                TextView x = (TextView) findViewById(R.id.x_axis);
                TextView y = (TextView) findViewById(R.id.y_axis);
                TextView z = (TextView) findViewById(R.id.z_axis);

                x.setText(characteristic.getValue().toString());
                y.setText(characteristic.getValue().toString());
                z.setText(characteristic.getValue().toString());
            }


        });

    }
}

}

我有一个错误,这个 UUID“6E400003B5A3F393E0A9E50E24DCCA9E”格式不正确。无论如何,我不知道这是否是订阅特征并接收通知的方式。

【问题讨论】:

  • 检查 androdi BLE documentation。试试 sn-p,如果您遇到问题,请提出问题。

标签: android bluetooth-lowenergy uuid gatt characteristics


【解决方案1】:

查看setCharacteristicNotification 的文档只会发现一个构造函数

boolean setCharacteristicNotification (BluetoothGattCharacteristic characteristic, 
                boolean enable)

所以,你需要先从你的 UUID 创建一个BluetoothGattCharacteristic,例如:

public static final UUID SERIAL_SERVICE = UUID.fromString("0000ffe0-0000-1000-8000-00805f9b34fb");
public static final UUID SERIAL_VALUE  = UUID.fromString("0000ffe1-0000-1000-8000-00805f9b34fb");
BluetoothGattCharacteristic characteristic = gatt.getService(SERIAL_SERVICE).getCharacteristic(SERIAL_VALUE);

然后设置通知

gatt.setCharacteristicNotification(characteristic,true); 

最后,设置客户端特征配置描述符以允许服务器启动更新

public static final UUID CONFIG_DESCRIPTOR = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb");
BluetoothGattDescriptor desc = characteristic.getDescriptor(CONFIG_DESCRIPTOR);
desc.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
gatt.writeDescriptor(desc);

最后一部分使您能够接收来自设备的通知。 CCCD 的 UUID 始终相同。

【讨论】:

  • 但是我想得到关于这个特性的通知,不想阅读它。这个想法是它在收到通知时执行一部分代码。
  • 是的,这就是我试图在上面的代码中向您展示的内容。为澄清而编辑。
  • GattDescriptor 的 UUID 是什么?您示例中的 SerialService 和 SerialValue 的 UUID 是随机的,不是吗?因为该链接中的链接不同:lancaster-university.github.io/microbit-docs/ble/uart-service
  • 见上面的编辑。描述符 UUID 始终相同。我推荐阅读 BLE 协议here
  • 我会看看这个链接。反正UUID CONFIG_DESCRIPTOR 哪里可以找到?
猜你喜欢
  • 1970-01-01
  • 2013-07-28
  • 1970-01-01
  • 2015-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多