Use the methods setReadDataType(type)
and setSendDataType(type)
to format the technique data:
//SerialPort.READ_HEX hex
//SerialPort.READ_STRING string
//If not set, the default string form
serialPort.setReadDataType(SerialPort.READ_HEX);
In addition to this, you can also set the received data format when building the instance:
//SerialPort.READ_HEX hex
//SerialPort.READ_STRING string
//If not set, the default string form
SerialPort serialPort = SerialPortBuilder.INSTANCE
.setReadDataType(SerialPort.READ_HEX)
.build(this);
//SerialPort.SEND_HEX hex
//SerialPort.SEND_STRING string
//If not set, the default string form
serialPort.setSendDataType(SerialPort.SEND_HEX);
In addition to this, you can also set the send data format when building the instance:
//SerialPort.SEND_HEX hex
//SerialPort.SEND_STRING string
//If not set, the default string form
SerialPort serialPort = SerialPortBuilder.INSTANCE
.setSendDataType(SerialPort.SEND_HEX)
.build(this);
Currently, the data sending and receiving for BLE devices does not support the setting format, only the string format is supported. If you really need the hexadecimal data format, you can temporarily implement it by referring to the processing method of traditional equipment.
Reference code link: HexStringToString、StringToHex
Use the method setReceivedDataCallback(receivedDataCallback)
to set up a received message listener:
serialPort.setReceivedDataCallback( (data) -> {
return null;
});
In addition to this, you can also configure the listener when building the instance:
SerialPort serialPort = SerialPortBuilder.INSTANCE
.setReceivedDataCallback( (data) -> {
return null;
})
.build(this);
When receiving a message, you can also choose to obtain a byte array as follows:
serialPort.setReceivedBytesCallback( (bytes) -> {
return null;
});
In addition to this, you can also configure the listener when building the instance:
SerialPort serialPort = SerialPortBuilder.INSTANCE
.setReceivedBytesCallback( (bytes) -> {
return null;
})
.build(this);
Send a message using the method sendData(data)
:
serialPort.sendData("Hello World");
serialPort.sendData("0C FF");
All hexadecimals should be two digits, with 0 in front of the less than two digits, case-insensitive.
Now, BLE device support send bytes
serialPort.sendData(bytes);