Thread {
bluetoothAdapter?.startDiscovery()
val server : BluetoothServerSocket? = bluetoothAdapter?.listenUsingRfcommWithServiceRecord("Bluetooth", uuid)
var loop = true
while (loop) {
Log.d("Server", "Printing")
val bluetoothSocket: BluetoothSocket? = try {
server?.accept()
} catch (e: IOException) {
Log.e("ServerSocket", "Socket's accept() failed", e)
e.printStackTrace()
null
}
bluetoothSocket?.let {
binder.socketConnectedCallback?.onSocketConnected(it)
}
server?.close()
loop = false
}
}.start()
Thread {
val bluetoothSocket = device.device.createRfcommSocketToServiceRecord(uuid)
bluetoothAdapter?.cancelDiscovery()
val succeeded = try {
bluetoothSocket?.connect()
device.socket = bluetoothSocket
true
} catch (e: Exception) {
e.printStackTrace()
false
}
Handler(Looper.getMainLooper()).post {
onFinished.invoke(succeeded)
}
}.start()