蓝牙连接
BluetoothGattCallback 回调
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| android.bluetooth.BluetoothGattCallback BluetoothGattCallback = new BluetoothGattCallback() { @Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) @Override public void onServicesDiscovered(BluetoothGatt gatt, int status) @Override public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) @Override public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) @Override public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) @Override public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) }
|
连接
1 2 3
| private BluetoothGatt mBluetoothGatt;
mBluetoothGatt = mBluetoothDevice().connectGatt(context, true,BluetoothGattCallback);
|
找到需要的characteristic,descriptor
在onServicesDiscovered回调里遍历所有Services,Characteristics
1 2 3 4 5 6 7 8 9 10 11
| List<BluetoothGattService> serviceList = mBluetoothGatt.getServices();
for (BluetoothGattService gattService : serviceList) { List<BluetoothGattCharacteristic> characteristicList = gattService.getCharacteristics(); for (BluetoothGattCharacteristic characteristic : characteristicList) { } }
|
读写UUID
如何找到读写的UUID号,可以用蓝牙助手,或者查看外围蓝牙设备文档
或者从权限判断。