我目前正在尝试连接此设备Micro:bit。该设备具有多种服务和特性。问题是一旦连接,设备只返回1个服务和0个特征。我错过了什么?
public class MainActivity extends Activity {
private BluetoothAdapter bleAdapter;
private BluetoothGattCharacteristic UART;
private BluetoothGattDescriptor descriptor;
private TextView connectionState;
private final UUID SERVICE = UUID.fromString("6E400001-B5A3-F393-E0A9-E50E24DCCA9E");
private final UUID CHARACTERISTIC = UUID.fromString("6E400003-B5A3-F393-E0A9-E50E24DCCA9E");
private final UUID CONFIG_DESCRIPTOR = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Check if the system supports Bluetooth Low Energy
if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
Toast.makeText(this, "BLE Not Supported", Toast.LENGTH_SHORT).show();
finish();
}
// Take the system BLE adapter
bleAdapter = ((BluetoothManager) getSystemService(BLUETOOTH_SERVICE)).getAdapter();
// Enable Bluetooth in case it's off.
if (bleAdapter == null || !bleAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
}
// Get paired devices.
Set<BluetoothDevice> pairedDevices = bleAdapter.getBondedDevices();
for (BluetoothDevice device : pairedDevices) {
// Connect to Micro:Bit (the only one paired device)
device.connectGatt(this, true, new BluetoothGattCallback() {
// Check if it connects or disconnects from the Micro:Bit
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
super.onConnectionStateChange(gatt, status, newState);
switch (newState) {
case BluetoothProfile.STATE_CONNECTED:
gatt.discoverServices();
runOnUiThread(new Runnable() {
public void run() {
connectionState = findViewById(R.id.state);
connectionState.setText("Connected = True");
connectionState.setTextColor(Color.GREEN);
}
});
break;
case BluetoothProfile.STATE_DISCONNECTED:
gatt.disconnect();
runOnUiThread(new Runnable() {
public void run() {
connectionState = findViewById(R.id.state);
connectionState.setText("Connected = False");
connectionState.setTextColor(Color.RED);
}
});
}
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
super.onServicesDiscovered(gatt, status);
List<BluetoothGattService> list = new ArrayList<BluetoothGattService>();
list = gatt.getServices();
for (BluetoothGattService elem : list){
Log.i("Service", elem.getUuid().toString());
List<BluetoothGattCharacteristic> list2 = new ArrayList<BluetoothGattCharacteristic>();
list2 = elem.getCharacteristics();
for (BluetoothGattCharacteristic elem2: list2) {
Log.i("Characteristic", elem2.getUuid().toString());
}
}
// Try to set notifications
UART = gatt.getService(SERVICE).getCharacteristic(CHARACTERISTIC);
gatt.setCharacteristicNotification(UART,true);
descriptor = UART.getDescriptor(CONFIG_DESCRIPTOR);
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
gatt.writeDescriptor(descriptor);
}
// Activate every time UART Characteristic is updated
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
super.onCharacteristicChanged(gatt, characteristic);
runOnUiThread(new Runnable() {
public void run() {
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("HELLO");
y.setText("IT ");
z.setText("WORKS!");
}
});
}
});
}
}
}
来自变量SERVICE和CHARACTERISTIC的UUID来自here。我尝试连接到该UART服务,并在此设备更新TX特性时收到通知。但我只是从onServicesDiscovered()获得了一个循环上的服务,如果我尝试gatt.getService(SERVICE).getCharacteristic(CHARACTERISTIC),我会得到空指针。我做错了什么?