问题
I am trying to communicate with an IoT device by BlueTooth connectivity. first,
- I have paired the device using BlueTooth.
- Then prepared the Hexadecimal value using AES encryption
- Send a request to the device using characteristic write
- I tried to read the response from the device using characteristic Read but I got the status as 1 and data is null.
These are the steps which I have done and the code I mentioned below.
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()));
}
};
In the document, they have explained all the communication will be in 16 bit but when I send a request I got one 32 bit value but I don't think that is the value I am expecting. can anyone help me to solve the issue? I have to communicate with my device using BlueTooth.
来源:https://stackoverflow.com/questions/59099008/trying-to-connect-with-iot-device-by-bluetooth-connectivitygatt