You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

55 lines
1.8 KiB

package net.aiterp.git.ykonsole2.infrastructure.iconsole
import com.welie.blessed.*
import net.aiterp.git.ykonsole2.domain.runtime.Connected
import net.aiterp.git.ykonsole2.domain.runtime.Disconnected
import net.aiterp.git.ykonsole2.domain.runtime.ErrorOccurred
import net.aiterp.git.ykonsole2.domain.runtime.Event
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.trySendBlocking
import kotlinx.coroutines.runBlocking
internal class Client(
private val connectionString: String,
private val eventCh: Channel<Event>,
) {
private lateinit var central: BluetoothCentralManager
private var current: BluetoothPeripheral? = null
init {
val cbPeripheral = object : BluetoothPeripheralCallback() {
override fun onCharacteristicUpdate(
peripheral: BluetoothPeripheral,
value: ByteArray,
characteristic: BluetoothGattCharacteristic,
status: BluetoothCommandStatus,
) {
val response = Response.fromBytes(value)
}
}
val cbScan = object : BluetoothCentralManagerCallback() {
override fun onConnectedPeripheral(peripheral: BluetoothPeripheral) {
current = peripheral
runBlocking { eventCh.send(Connected) }
}
override fun onConnectionFailed(peripheral: BluetoothPeripheral, status: BluetoothCommandStatus) {
runBlocking {
eventCh.send(ErrorOccurred("$connectionString: $status"))
}
}
override fun onDiscoveredPeripheral(peripheral: BluetoothPeripheral, scanResult: ScanResult) {
if (peripheral.address != connectionString) return
central.stopScan()
central.connectPeripheral(peripheral, cbPeripheral)
}
}
central = BluetoothCentralManager(cbScan)
central.scanForPeripheralsWithNames(arrayOf())
central.getPeripheral("")
}
}