|
|
@ -71,6 +71,31 @@ class IConsole : ActiveDriver() { |
|
|
|
current?.cancelConnection() |
|
|
|
tryingSince = Instant.now() |
|
|
|
|
|
|
|
if (central == null) { |
|
|
|
val cbScan = object : BluetoothCentralManagerCallback() { |
|
|
|
override fun onScanStarted() { |
|
|
|
logger.info("Scanning for $connectionString...") |
|
|
|
} |
|
|
|
|
|
|
|
override fun onScanStopped() { |
|
|
|
logger.info("Scan stopped") |
|
|
|
|
|
|
|
tryingSince?.let { ts -> |
|
|
|
if (ts.isBefore(Instant.now().minusSeconds(20))) { |
|
|
|
central?.stopScan() |
|
|
|
tryingSince = null |
|
|
|
runBlocking { |
|
|
|
output.emit(ErrorOccurred("Connection timeout after 20 seconds")) |
|
|
|
output.emit(Disconnected) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
override fun onDiscoveredPeripheral(peripheral: BluetoothPeripheral, scanResult: ScanResult) { |
|
|
|
logger.info("Connecting to ${peripheral.name} (${peripheral.address})...") |
|
|
|
|
|
|
|
if (tryingSince != null) { |
|
|
|
val cbPeripheral = object : BluetoothPeripheralCallback() { |
|
|
|
override fun onServicesDiscovered(peripheral: BluetoothPeripheral, services: MutableList<BluetoothGattService>) { |
|
|
|
logger.info("Checking device...") |
|
|
@ -165,30 +190,6 @@ class IConsole : ActiveDriver() { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
val cbScan = object : BluetoothCentralManagerCallback() { |
|
|
|
override fun onScanStarted() { |
|
|
|
logger.info("Scanning for $connectionString...") |
|
|
|
} |
|
|
|
|
|
|
|
override fun onScanStopped() { |
|
|
|
logger.info("Scan stopped") |
|
|
|
|
|
|
|
tryingSince?.let { ts -> |
|
|
|
if (ts.isBefore(Instant.now().minusSeconds(20))) { |
|
|
|
central?.stopScan() |
|
|
|
tryingSince = null |
|
|
|
runBlocking { |
|
|
|
output.emit(ErrorOccurred("Connection timeout after 20 seconds")) |
|
|
|
output.emit(Disconnected) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
override fun onDiscoveredPeripheral(peripheral: BluetoothPeripheral, scanResult: ScanResult) { |
|
|
|
logger.info("Connecting to ${peripheral.name} (${peripheral.address})...") |
|
|
|
|
|
|
|
if (tryingSince != null) { |
|
|
|
tryingSince = null |
|
|
|
central?.connectPeripheral(peripheral, cbPeripheral) |
|
|
|
central?.stopScan() |
|
|
@ -222,9 +223,10 @@ class IConsole : ActiveDriver() { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
central = BluetoothCentralManager(cbScan).apply { |
|
|
|
scanForPeripheralsWithAddresses(arrayOf(connectionString)) |
|
|
|
central = BluetoothCentralManager(cbScan) |
|
|
|
} |
|
|
|
|
|
|
|
central?.scanForPeripheralsWithAddresses(arrayOf(connectionString)) |
|
|
|
} |
|
|
|
|
|
|
|
private fun onDisconnect() { |
|
|
|