BLE 从特征接收 GATT 通知

Lec*_*ico 1 uuid android characteristics bluetooth-lowenergy gatt

我希望在该特性发生更改时收到通知Micro:Bit。

\n\n

我正在做的事情基本上如下:

\n\n

1)检查系统是否兼容BLE

\n\n

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

\n\n

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

\n\n

4) 当连接发生变化时激活此代码(\xc2\xbfConnected/Disconnected?)

\n\n

5) 当特性更新时激活此代码\xc2\xbf?

\n\n
public class MainActivity extends Activity {\n\nBluetoothAdapter bleAdapter;\n\n@Override\nprotected void onCreate(Bundle savedInstanceState) {\n    super.onCreate(savedInstanceState);\n    setContentView(R.layout.activity_main);\n\n    **(1)**\n\n    if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {\n        Toast.makeText(this, "BLE Not Supported", Toast.LENGTH_SHORT).show();\n        finish();\n    }\n\n    **(2)**\n\n    bleAdapter = ((BluetoothManager) getSystemService(BLUETOOTH_SERVICE)).getAdapter();\n\n    if (bleAdapter == null || !bleAdapter.isEnabled()) {\n        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);\n        startActivityForResult(enableBtIntent, 1);\n    }\n\n    Set<BluetoothDevice> pairedDevices = bleAdapter.getBondedDevices();\n\n    for (BluetoothDevice device : pairedDevices) {\n\n        **(3)**\n\n        device.connectGatt(this, true, new BluetoothGattCallback() {\n\n            **(4)**\n\n            @Override\n            public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {\n                super.onConnectionStateChange(gatt, status, newState);\n                switch (newState) {\n                    case BluetoothProfile.STATE_CONNECTED:\n                        gatt.setCharacteristicNotification("6E400003B5A3F393E0A9E50E24DCCA9E", true); // This doesn\'t work\n                        break;\n                }\n            }\n\n            **5**\n\n           \xc2\xa0@Override\n            public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {\n                super.onCharacteristicChanged(gatt, characteristic);\n\n                TextView x = (TextView) findViewById(R.id.x_axis);\n                TextView y = (TextView) findViewById(R.id.y_axis);\n                TextView z = (TextView) findViewById(R.id.z_axis);\n\n                x.setText(characteristic.getValue().toString());\n                y.setText(characteristic.getValue().toString());\n                z.setText(characteristic.getValue().toString());\n            }\n\n\n        });\n\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

}

\n\n

我遇到错误,此 UUID“6E400003B5A3F393E0A9E50E24DCCA9E”格式错误。无论如何,我不知道这是否是订阅特征并接收通知的方法。

\n

San*_*and 6

查看文档 setCharacteristicNotification仅显示一个构造函数

boolean setCharacteristicNotification (BluetoothGattCharacteristic characteristic, 
                boolean enable)
Run Code Online (Sandbox Code Playgroud)

因此,您需要首先BluetoothGattCharacteristic从您的 UUID 创建一个,例如:

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);
Run Code Online (Sandbox Code Playgroud)

然后设置通知

gatt.setCharacteristicNotification(characteristic,true); 
Run Code Online (Sandbox Code Playgroud)

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

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);
Run Code Online (Sandbox Code Playgroud)

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