简体   繁体   English

BLE从特征接收GATT通知

[英]BLE receiving GATT notifications from a characteristic

I want to receive notifications when this characteristic is changed Micro:Bit . 当此特性更改为Micro:Bit时,我想接收通知。

What I'm doing is basically the following: 我正在做的基本上是以下内容:

1) Check if the system is compatible with BLE 1)检查系统是否与BLE兼容

2) Enable bluetooth in case it's disabled 2)在禁用的情况下启用蓝牙

3) Connect to the only one paired device (Micro:Bit) 3)连接到唯一一个配对的设备(Micro:Bit)

4) Activate this code when connectivity changes (¿Connected/Disconnected?) 4)当连接更改时(¿Connected / Disconnected?)激活此代码

5) Activate this code when characteristic is updated ¿? 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());
            }


        });

    }
}

} }

I have an error that this UUID "6E400003B5A3F393E0A9E50E24DCCA9E" is malformed. 我有一个错误,该UUID“ 6E400003B5A3F393E0A9E50E24DCCA9E”格式错误。 Anyway, I don't know if this is how to subscribe to a characteristic and receive the notifications. 无论如何,我不知道这是如何订阅特征并接收通知。

Looking at the documentation for setCharacteristicNotification reveals only one constructor 查看setCharacteristicNotification只会发现一个构造函数

boolean setCharacteristicNotification (BluetoothGattCharacteristic characteristic, 
                boolean enable)

So, you need to first create a BluetoothGattCharacteristic from your UUID, for example : 因此,您首先需要根据您的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);

Then set notifications 然后设置通知

gatt.setCharacteristicNotification(characteristic,true); 

Finally, set the Client Characteristic Config Descriptor to allow server initiated updates 最后,设置客户端特征配置描述符以允许服务器启动的更新

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

The last part enables you to receive notifications from the device. 最后一部分使您能够从设备接收通知。 UUID of the CCCD is always the same. CCCD的UUID始终相同。

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

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