我想在此特性更改时收到通知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”格式错误。无论如何,我不知道这是否是订阅特征并接收通知的方式。