我正在尝试通过蓝牙连接与IoT设备进行通信。首先,
这些是我已经完成的步骤以及下面提到的代码。
BluetoothGatt gatt;
BluetoothGattCallback gattCallback = new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
super.onConnectionStateChange(gatt, status, newState);
System.out.println("Lock Conn St ---"+status);
System.out.println("Lock Conn new st ---"+newState);
if (newState == 2){
gatt.discoverServices();
}
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
super.onServicesDiscovered(gatt, status);
System.out.println("Lock Conn new serv discov ---"+status);
if (status == BluetoothGatt.GATT_SUCCESS){
System.out.println("gatt Success ---");
String getTockenString;
/*List<BluetoothGattService> ls= gatt.getServices ();
System.out.println("gatt List ---"+ls.toString());*/
//orginal key
final byte AESkey[] = new byte[] {0x20,0x57,0x4f,0x55,0x36,0x4b,0x3f,0x47,0x39,0x50,0x41,0x58,0x91,0x63,0x2d,0x8b};
final byte Src[] = new byte[] {0x06,0x01,0x01,0x01,0x2D,0x1A,0x68,0x3D,0x48,0x27,0x1A,0x18,0x31,0x6E,0x47,0x1A};
try {
getTockenString = bytesToHex(encrypt(Src, AESkey));
BluetoothGattService service = gatt.getService(LOCK_SERVICE_UUID);
BluetoothGattCharacteristic readCharect = service.getCharacteristic(LOCK_READ_DATA_UUID);
BluetoothGattCharacteristic writeCharect = service.getCharacteristic(LOCK_WRITE_DATA_UUID);
gatt.setCharacteristicNotification(readCharect,true);
writeCharect.setValue(getTockenString.getBytes());
gatt.writeCharacteristic(writeCharect);
System.out.println("gatt AES ENC ---"+getTockenString);
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println("gatt end of on descovng ---");
}
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
super.onCharacteristicChanged(gatt, characteristic);
System.out.println("gatt char changed ---"+ Arrays.toString(characteristic.getValue()));
}
@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicWrite(gatt, characteristic, status);
System.out.println("gatt =============== == onCharacteristicWrite happend ---"+status+ Arrays.toString(characteristic.getValue() ));
BluetoothGattService service = gatt.getService(LOCK_SERVICE_UUID);
BluetoothGattCharacteristic readCharect = service.getCharacteristic(LOCK_READ_DATA_UUID);
gatt.readCharacteristic(readCharect);
}
@Override
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicRead(gatt, characteristic, status);
System.out.println("gatt ================ onCharacteristicRead happend ---"+status );
System.out.println("gattonCharacteristicRead value---"+ Arrays.toString(characteristic.getValue()));
}
};
在文档中,他们解释了所有通信都是16位的,但是当我发送请求时,我得到了一个32位的值,但是我认为那不是我期望的值。谁能帮我解决这个问题?我必须使用蓝牙与我的设备进行通讯。