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)
setServerNameSet server namesetServerUUIDSet the UUID of the server, the UUID of the traditional device needs to be set to the same as this when the client connectssetServerReceivedDataCallbackThe server receives message monitoringsetServerConnectStatusCallbackServer connection status monitoringstatusConnection StatusbluetoothDeviceConnected device, null whenstatusis 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)
statusis of typeBoolean, indicating the discoverable status
Send¶
serialPortServer.sendData("Hello")