代码之家  ›  专栏  ›  技术社区  ›  Lechucico

BLE从特性接收GATT通知

  •  1
  • Lechucico  · 技术社区  · 7 年前

    我希望在更改此特性时收到通知 Micro:Bit .

    我所做的基本上是:

    1) 检查系统是否与BLE兼容

    2) 如果蓝牙已禁用,请启用蓝牙

    3) 仅连接到一个配对设备(Micro:位)

    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“6E400003B5A3F393E0A9E50E24DCA9E”的格式不正确。不管怎样,我不知道这是否是订阅一个特性并接收通知的方式。

    1 回复  |  直到 7 年前
        1
  •  4
  •   Sanchit Anand    7 年前

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

    boolean setCharacteristicNotification (BluetoothGattCharacteristic characteristic, 
                    boolean enable)
    

    因此,您需要首先创建 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);
    

    然后设置通知

    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始终相同。