Received and send¶
Set data format¶
Use the methods setReadDataType(type)
and setSendDataType(type)
to format the technique data:
Set the received message format¶
//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
val serialPort = SerialPortBuilder
.setReadDataType(SerialPort.READ_HEX)
.build(this)
Set the send data format¶
//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
val serialPort = SerialPortBuilder
.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
Receive message¶
string and hex¶
Use the method setReceivedDataCallback(receivedDataCallback)
to set up a received message listener:
serialPort.setReceivedDataCallback { data ->
}
In addition to this, you can also configure the listener when building the instance:
val serialPort = SerialPortBuilder
.setReceivedDataCallback { data ->
}
.build(this)
Byte array¶
When receiving a message, you can also choose to obtain a byte array as follows:
serialPort.setReceivedBytesCallback { bytes ->
}
In addition to this, you can also configure the listener when building the instance:
val serialPort = SerialPortBuilder
.setReceivedBytesCallback { bytes ->
}
.build(this)