Server¶
Now, an Android server can be built to implement Bluetooth communication between two Androids. (Currently only supports single device connection)
Build instance¶
val serialPortServer = SerialPortServerBuilder
.setServerName("SerialPortServer")
.setServerUUID("00001101-0000-1000-8000-00805F9B34FB")
.setServerReceivedDataCallback {
}
.setServerConnectStatusCallback { status, bluetoothDevice ->
}
.build(this)
setServerName
Set server namesetServerUUID
Set the UUID of the server, the UUID of the traditional device needs to be set to the same as this when the client connectssetServerReceivedDataCallback
The server receives message monitoringsetServerConnectStatusCallback
Server connection status monitoringstatus
Connection StatusbluetoothDevice
Connected device, null whenstatus
is false
Open server¶
Only after the service is opened, the client can connect to the server.
serialPortServer.openServer()
Close server¶
serialPortServer.closeServer()
Set the server discoverable state¶
By default, if the server is turned on, it will be automatically set to be discoverable, and if the server is turned off, it will be set to be invisible.
setServerDiscoverable(status)
status
is of typeBoolean
, indicating the discoverable status
Send¶
serialPortServer.sendData("Hello")