I want to receive notifications when this characteristic is changed Micro:Bit.
What I'm doing is basically the following:
1) Check if the system is compatible with BLE
2) Enable bluetooth in case it's disabled
3) Connect to the only one paired device (Micro:Bit)
4) Activate this code when connectivity changes (¿Connected/Disconnected?)
5) Activate this code when characteristic is updated ¿?
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. Anyway, I don't know if this is how to subscribe to a characteristic and receive the notifications.