mirror of
https://github.com/D-Modem/app.git
synced 2026-11-28 17:38:05 -08:00
A Bluetooth modem as the modem (Dial-Up Networking: a Sony BTA-NW1)
A new line, "A phone line, through a Bluetooth modem": the paired modem's DUN service (then unencrypted, then its serial port) is connected in place of D-Modem's modem, its streams on the DTE hub, so the terminal, the internet and fax talk to it with its own AT commands. Its status from its result codes (CONNECT and its speed, NO CARRIER, BUSY...); no tones or adaptive answer of the app's; its connection dropping, connected again once. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0172Hejd12i4aYikYW9vgpZs
This commit is contained in:
10 files changed
+364
-4
No files matched your search
@@ -104,6 +104,18 @@ nothing downloaded into it; a USB 2.0 port). The phone runs the modem; plugged i
|
||||
Line, "A phone line, through a USB modem"; Android asks once whether
|
||||
D-Modem may use it, and offers the app when one is plugged in.
|
||||
|
||||
## A real phone line: a Bluetooth modem
|
||||
|
||||
A modem of its own with Bluetooth's Dial-Up Networking profile, such as
|
||||
Sony's BTA-NW1 (Bluetooth 1.1) on a phone line, can take D-Modem's
|
||||
modem's place: Settings, Line, "A phone line, through a Bluetooth modem",
|
||||
and the paired modem chosen (pair it in Android's Bluetooth settings
|
||||
first; an older one asks for its PIN). The app connects to its DUN
|
||||
service (unencrypted, or its serial port, if need be) and the terminal,
|
||||
the internet and fax talk to it with its own AT commands; the status
|
||||
(online, its speed) comes from its result codes. Its connection
|
||||
dropping, the app connects again once.
|
||||
|
||||
An app can't list the USB devices itself: Android opens the one the user
|
||||
allowed and gives its descriptor. libusb is built for Android in the app
|
||||
(`app/src/main/cpp/usb/libusb.cmake`, D-Modem-RE's pinned release) and
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
// Copyright (C) 2026 Neo-Desktop
|
||||
package com.neodesktop.dmodem.modem
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.bluetooth.BluetoothManager
|
||||
import android.bluetooth.BluetoothSocket
|
||||
import android.content.Context
|
||||
import com.neodesktop.dmodem.NativeModem
|
||||
import java.io.IOException
|
||||
import java.util.UUID
|
||||
|
||||
/**
|
||||
* A modem of its own on Bluetooth (the line LINE_BT_MODEM): a Sony BTA-NW1, say, or any device with
|
||||
* Bluetooth's Dial-Up Networking profile. It takes the app's AT commands and data in place of D-Modem's
|
||||
* own modem: the terminal, the internet and fax talk to it as they would to D-Modem's.
|
||||
*/
|
||||
@SuppressLint("MissingPermission") // (the service checks the permission first)
|
||||
object BluetoothModem {
|
||||
/** Dial-Up Networking's service (DUN), and the serial port's (SPP) that some offer as well or instead */
|
||||
val DUN: UUID = UUID.fromString("00001103-0000-1000-8000-00805F9B34FB")
|
||||
val SPP: UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB")
|
||||
|
||||
/**
|
||||
* Connected to the paired device at [address]: its Dial-Up Networking service, then without
|
||||
* encryption (an older device's pairing may not give one), then its serial port; each try said to [log].
|
||||
*/
|
||||
fun connect(context: Context, address: String, log: (String) -> Unit): BluetoothSocket {
|
||||
val a = context.getSystemService(BluetoothManager::class.java)?.adapter ?: throw IOException("this phone has no Bluetooth")
|
||||
if (!a.isEnabled) throw IOException("Bluetooth is off")
|
||||
// (a search going on slows a connection down)
|
||||
runCatching { a.cancelDiscovery() }
|
||||
val d = a.getRemoteDevice(address)
|
||||
val tries = listOf<Pair<String, () -> BluetoothSocket>>(
|
||||
"Dial-Up Networking" to { d.createRfcommSocketToServiceRecord(DUN) },
|
||||
"Dial-Up Networking, unencrypted" to { d.createInsecureRfcommSocketToServiceRecord(DUN) },
|
||||
"its serial port (SPP)" to { d.createRfcommSocketToServiceRecord(SPP) },
|
||||
)
|
||||
var last: Exception? = null
|
||||
for ((what, make) in tries) {
|
||||
var s: BluetoothSocket? = null
|
||||
try {
|
||||
s = make()
|
||||
s.connect()
|
||||
log("connected to ${d.name ?: address} ($what)")
|
||||
return s
|
||||
} catch (e: IOException) {
|
||||
runCatching { s?.close() }
|
||||
log("$what: ${e.message}")
|
||||
last = e
|
||||
} catch (e: SecurityException) {
|
||||
throw IOException("Bluetooth isn't allowed: allow it in the settings")
|
||||
}
|
||||
}
|
||||
throw IOException("it doesn't answer (on, paired, in reach?): ${last?.message}")
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The status of a modem that says only its result codes (a Bluetooth modem's): online from its CONNECT
|
||||
* (and its speed), idle again from NO CARRIER, BUSY, NO DIALTONE or NO ANSWER. Fed what the modem sends.
|
||||
*/
|
||||
class ResultCodeStatus(private val onStatus: (NativeModem.Status) -> Unit) {
|
||||
private val line = StringBuilder()
|
||||
private var status = ModemState.IDLE
|
||||
|
||||
fun feed(data: ByteArray, length: Int) {
|
||||
for (i in 0 until length) {
|
||||
val c = data[i].toInt() and 0xff
|
||||
if (c == '\r'.code || c == '\n'.code) {
|
||||
result(line.toString().trim())
|
||||
line.setLength(0)
|
||||
} else if (line.length < MAX) {
|
||||
line.append(c.toChar())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun result(r: String) {
|
||||
val next = when {
|
||||
r.startsWith("CONNECT") -> {
|
||||
// (CONNECT 26400, CONNECT 26400/ARQ/V90: its speed, if it says)
|
||||
val bps = Regex("""^CONNECT\s+(\d+)""").find(r)?.groupValues?.get(1)?.toIntOrNull() ?: 0
|
||||
NativeModem.Status(NativeModem.Link.ONLINE, true, bps, bps)
|
||||
}
|
||||
r in ENDS -> ModemState.IDLE
|
||||
else -> return
|
||||
}
|
||||
if (next != status) {
|
||||
status = next
|
||||
onStatus(next)
|
||||
}
|
||||
}
|
||||
|
||||
private companion object {
|
||||
/** longer lines aren't result codes (data online) */
|
||||
const val MAX = 40
|
||||
val ENDS = setOf("NO CARRIER", "BUSY", "NO DIALTONE", "NO DIAL TONE", "NO ANSWER")
|
||||
}
|
||||
}
|
||||
@@ -32,6 +32,7 @@ import kotlinx.coroutines.asCoroutineDispatcher
|
||||
import kotlinx.coroutines.cancel
|
||||
import kotlinx.coroutines.flow.combine
|
||||
import kotlinx.coroutines.flow.distinctUntilChanged
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.runBlocking
|
||||
@@ -90,6 +91,13 @@ class ModemService : Service() {
|
||||
/** the USB modem that's the line (LINE_USB), open while the modem runs */
|
||||
private var usbModem: UsbModems.Opened.Device? = null
|
||||
|
||||
/** the Bluetooth modem's connection (LINE_BT_MODEM), in place of D-Modem's modem while it runs */
|
||||
@Volatile
|
||||
private var btSocket: android.bluetooth.BluetoothSocket? = null
|
||||
private var btWatch: kotlinx.coroutines.Job? = null
|
||||
private val btStatus = ResultCodeStatus { ModemState.setStatus(it) }
|
||||
private val btResults = com.neodesktop.dmodem.terminal.TerminalLink.Listener { d, n -> btStatus.feed(d, n) }
|
||||
|
||||
/** the network changed during a call: start again once on hook */
|
||||
@Volatile
|
||||
private var restartPending = false
|
||||
@@ -186,6 +194,16 @@ class ModemService : Service() {
|
||||
fail("the line is Bluetooth, which isn't allowed: allow it in the settings")
|
||||
return
|
||||
}
|
||||
if (settings.btModem) {
|
||||
if (!com.neodesktop.dmodem.bridge.BluetoothBridge.hasPermission(this)) {
|
||||
fail("the modem is a Bluetooth modem, and Bluetooth isn't allowed: allow it in the settings")
|
||||
return
|
||||
}
|
||||
if (settings.btModemAddress.isBlank()) {
|
||||
fail("no Bluetooth modem chosen: choose the paired modem in the settings")
|
||||
return
|
||||
}
|
||||
}
|
||||
ModemState.setError(null)
|
||||
log(com.neodesktop.dmodem.BuildInfo.described)
|
||||
// (not a failure: the SIP server may be on the internet, which needs no leave)
|
||||
@@ -216,6 +234,7 @@ class ModemService : Service() {
|
||||
|
||||
/** the native modem on the default network now, its DTE on the hub, the init commands sent */
|
||||
private fun startNative(): Boolean {
|
||||
if (settings.btModem) return startBtModem()
|
||||
val cm = getSystemService(ConnectivityManager::class.java)
|
||||
val net = cm.activeNetwork
|
||||
val lp = net?.let { cm.getLinkProperties(it) }
|
||||
@@ -319,9 +338,50 @@ class ModemService : Service() {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The Bluetooth modem connected in place of D-Modem's modem: what's written to the modem goes to it,
|
||||
* what it says to the terminal, the internet and fax; the status from its result codes. Its line's
|
||||
* own tones: no tones of the app's. Its connection dropping: connected again once, else the modem stops.
|
||||
*/
|
||||
private fun startBtModem(): Boolean {
|
||||
stopping = false
|
||||
val name = settings.btModemName.ifBlank { settings.btModemAddress }
|
||||
log("the modem: the Bluetooth modem $name")
|
||||
val s = try {
|
||||
BluetoothModem.connect(this, settings.btModemAddress) { log("Bluetooth modem: $it") }
|
||||
} catch (e: java.io.IOException) {
|
||||
ModemState.setError("the Bluetooth modem $name: ${e.message}")
|
||||
return false
|
||||
}
|
||||
btSocket = s
|
||||
ModemState.dte.attach(s.inputStream, s.outputStream)
|
||||
ModemState.setStatus(ModemState.IDLE)
|
||||
ModemState.speaker.enabled = false
|
||||
ModemState.tones.enabled = false
|
||||
ModemState.line = settings.line
|
||||
watchProgress()
|
||||
ModemState.dte.removeListener(btResults)
|
||||
ModemState.dte.addListener(btResults)
|
||||
// (its own commands: not D-Modem's adaptive answer, AT+FAA)
|
||||
ModemState.adaptiveAnswer = false
|
||||
val start = settings.startCommands()
|
||||
if (start.isNotBlank()) ModemState.dte.write(start + "\r")
|
||||
btWatch?.cancel()
|
||||
btWatch = scope.launch {
|
||||
// (the reader's end: the connection gone, not by our stop)
|
||||
ModemState.dte.state.first { it is com.neodesktop.dmodem.terminal.TerminalLink.State.Closed && !stopping }
|
||||
log("the Bluetooth modem's connection dropped: connecting again")
|
||||
restart()
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
/** the native modem stopped: the call hung up, SIP gone, the DTE's socket closed */
|
||||
private fun stopNative() {
|
||||
stopping = true
|
||||
btWatch?.cancel()
|
||||
btWatch = null
|
||||
ModemState.dte.removeListener(btResults)
|
||||
progressJob?.cancel()
|
||||
progressJob = null
|
||||
ModemState.dte.removeListener(progressText)
|
||||
@@ -330,7 +390,9 @@ class ModemService : Service() {
|
||||
callNotification(null)
|
||||
ModemState.speaker.setLevel(0)
|
||||
com.neodesktop.dmodem.peer.Peers.stop()
|
||||
NativeModem.stop()
|
||||
// (a Bluetooth modem's connection, or D-Modem's modem)
|
||||
btSocket?.let { s -> runCatching { s.close() } } ?: NativeModem.stop()
|
||||
btSocket = null
|
||||
// (the device let go once the modem has)
|
||||
usbModem?.connection?.close()
|
||||
usbModem = null
|
||||
@@ -493,6 +555,7 @@ class ModemService : Service() {
|
||||
ModemSettings.LINE_CELL -> " (line: mobile calls)"
|
||||
ModemSettings.LINE_NONE -> " (no line)"
|
||||
ModemSettings.LINE_USB -> " (line: the ${usbModem?.kind?.label ?: "USB modem"})"
|
||||
ModemSettings.LINE_BT_MODEM -> " (the Bluetooth modem ${settings.btModemName.ifBlank { settings.btModemAddress }})"
|
||||
else -> ""
|
||||
}
|
||||
}
|
||||
@@ -519,7 +582,7 @@ class ModemService : Service() {
|
||||
private fun callerOfLine(): String = when (settings.line) {
|
||||
ModemSettings.LINE_WIFI, ModemSettings.LINE_BLUETOOTH -> "The other phone"
|
||||
ModemSettings.LINE_CELL -> "A mobile call"
|
||||
ModemSettings.LINE_USB -> "A call on the phone line"
|
||||
ModemSettings.LINE_USB, ModemSettings.LINE_BT_MODEM -> "A call on the phone line"
|
||||
else -> "A SIP call"
|
||||
}
|
||||
|
||||
|
||||
@@ -60,9 +60,12 @@ data class ModemSettings(
|
||||
* through AMR 12.2, to another D-Modem; V.22 or V.21 to other modems), V21 (300 bit/s), V22, V22B
|
||||
*/
|
||||
val cellSpeed: String = CELL_VCVM,
|
||||
/** the Bluetooth modem's (LINE_BT_MODEM): the paired device's address, its name to show */
|
||||
val btModemAddress: String = "",
|
||||
val btModemName: String = "",
|
||||
) {
|
||||
/** the line is another phone (or this phone's mobile calls, or none), not SIP: the app carries its samples */
|
||||
val peer: Boolean get() = line != LINE_SIP && line != LINE_USB
|
||||
val peer: Boolean get() = line != LINE_SIP && line != LINE_USB && line != LINE_BT_MODEM
|
||||
|
||||
/** the line is a SIP account's */
|
||||
val sip: Boolean get() = line == LINE_SIP
|
||||
@@ -70,6 +73,9 @@ data class ModemSettings(
|
||||
/** the line is a phone line, through a USB modem (UsbModems): the modem dials it itself */
|
||||
val usb: Boolean get() = line == LINE_USB
|
||||
|
||||
/** a modem of its own on Bluetooth (BluetoothModem) in place of D-Modem's: it has the line */
|
||||
val btModem: Boolean get() = line == LINE_BT_MODEM
|
||||
|
||||
/** no line at all: the modem on its own (AT commands, the terminal, the bridges); a dial gets NO DIALTONE */
|
||||
val noLine: Boolean get() = line == LINE_NONE
|
||||
|
||||
@@ -96,6 +102,7 @@ data class ModemSettings(
|
||||
const val LINE_CELL = "cell"
|
||||
const val LINE_NONE = "none"
|
||||
const val LINE_USB = "usb"
|
||||
const val LINE_BT_MODEM = "bt_modem"
|
||||
/** the mobile-call line's V.cvm (D-Modem-RE's +MS=V_CVM) */
|
||||
const val CELL_VCVM = "V_CVM"
|
||||
|
||||
@@ -141,6 +148,8 @@ class SettingsStore(private val context: Context) {
|
||||
val callTones = booleanPreferencesKey("callTones")
|
||||
val adaptiveAnswer = booleanPreferencesKey("adaptiveAnswer")
|
||||
val cellSpeed = stringPreferencesKey("cell_speed")
|
||||
val btModemAddress = stringPreferencesKey("bt_modem_address")
|
||||
val btModemName = stringPreferencesKey("bt_modem_name")
|
||||
}
|
||||
|
||||
val settings: Flow<ModemSettings> = context.dataStore.data.map { p ->
|
||||
@@ -166,6 +175,8 @@ class SettingsStore(private val context: Context) {
|
||||
callTones = p[Keys.callTones] ?: d.callTones,
|
||||
adaptiveAnswer = p[Keys.adaptiveAnswer] ?: d.adaptiveAnswer,
|
||||
cellSpeed = p[Keys.cellSpeed] ?: d.cellSpeed,
|
||||
btModemAddress = p[Keys.btModemAddress] ?: d.btModemAddress,
|
||||
btModemName = p[Keys.btModemName] ?: d.btModemName,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -193,6 +204,8 @@ class SettingsStore(private val context: Context) {
|
||||
p[Keys.callTones] = s.callTones
|
||||
p[Keys.adaptiveAnswer] = s.adaptiveAnswer
|
||||
p[Keys.cellSpeed] = s.cellSpeed
|
||||
p[Keys.btModemAddress] = s.btModemAddress
|
||||
p[Keys.btModemName] = s.btModemName
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -307,6 +307,14 @@ private fun LineCard(s: ModemSettings, run: ModemState.Run, network: String?, on
|
||||
PeerLineRows(s, run, network)
|
||||
return@Column
|
||||
}
|
||||
if (s.btModem) {
|
||||
InfoRow("Via", "a phone line, through a Bluetooth modem")
|
||||
InfoRow("Modem", s.btModemName.ifBlank { s.btModemAddress }.ifBlank { "none chosen" })
|
||||
if (run == ModemState.Run.RUNNING) {
|
||||
Text("Its own AT commands: calls ring in the terminal (RING)", style = MaterialTheme.typography.bodySmall)
|
||||
}
|
||||
return@Column
|
||||
}
|
||||
if (s.usb) {
|
||||
InfoRow("Via", "a phone line, through a USB modem")
|
||||
InfoRow("Plugged in", rememberUsbModemsPlugged())
|
||||
|
||||
@@ -196,7 +196,7 @@ class MainActivity : ComponentActivity() {
|
||||
private fun callerOfLine(): String = when (com.neodesktop.dmodem.modem.ModemState.line) {
|
||||
com.neodesktop.dmodem.settings.ModemSettings.LINE_WIFI, com.neodesktop.dmodem.settings.ModemSettings.LINE_BLUETOOTH -> "The other phone"
|
||||
com.neodesktop.dmodem.settings.ModemSettings.LINE_CELL -> "A mobile call"
|
||||
com.neodesktop.dmodem.settings.ModemSettings.LINE_USB -> "A call on the phone line"
|
||||
com.neodesktop.dmodem.settings.ModemSettings.LINE_USB, com.neodesktop.dmodem.settings.ModemSettings.LINE_BT_MODEM -> "A call on the phone line"
|
||||
else -> "A SIP call"
|
||||
}
|
||||
|
||||
|
||||
@@ -48,6 +48,9 @@ fun LineChoice(s: ModemSettings, onChange: (ModemSettings) -> Unit) {
|
||||
val askBt = rememberLauncherForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) {
|
||||
if (BluetoothBridge.hasPermission(context)) onChange(s.copy(line = ModemSettings.LINE_BLUETOOTH))
|
||||
}
|
||||
val askBtModem = rememberLauncherForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) {
|
||||
if (BluetoothBridge.hasPermission(context)) onChange(s.copy(line = ModemSettings.LINE_BT_MODEM))
|
||||
}
|
||||
Text("Line", style = MaterialTheme.typography.titleMedium)
|
||||
val lines = listOf(
|
||||
ModemSettings.LINE_SIP to "A SIP account (calls over the internet)",
|
||||
@@ -55,6 +58,7 @@ fun LineChoice(s: ModemSettings, onChange: (ModemSettings) -> Unit) {
|
||||
ModemSettings.LINE_BLUETOOTH to "Another phone with D-Modem, on Bluetooth",
|
||||
ModemSettings.LINE_CELL to "This phone's mobile calls (slow; root)",
|
||||
ModemSettings.LINE_USB to "A phone line, through a USB modem",
|
||||
ModemSettings.LINE_BT_MODEM to "A phone line, through a Bluetooth modem (Dial-Up Networking)",
|
||||
ModemSettings.LINE_NONE to "No line (the modem on its own)",
|
||||
)
|
||||
var open by remember { mutableStateOf(false) }
|
||||
@@ -80,6 +84,11 @@ fun LineChoice(s: ModemSettings, onChange: (ModemSettings) -> Unit) {
|
||||
ModemSettings.LINE_WIFI -> onChange(
|
||||
s.copy(line = line, peerAddress = if (s.line == line) s.peerAddress else "", peerName = ""),
|
||||
)
|
||||
ModemSettings.LINE_BT_MODEM -> if (!BluetoothBridge.hasPermission(context)) {
|
||||
askBtModem.launch(BluetoothBridge.permissions())
|
||||
} else {
|
||||
onChange(s.copy(line = line))
|
||||
}
|
||||
ModemSettings.LINE_BLUETOOTH -> if (!BluetoothBridge.hasPermission(context)) {
|
||||
askBt.launch(BluetoothBridge.permissions())
|
||||
} else {
|
||||
@@ -260,6 +269,42 @@ private fun BluetoothPeer(s: ModemSettings, onChange: (ModemSettings) -> Unit, c
|
||||
}
|
||||
}
|
||||
|
||||
/** A modem of its own on Bluetooth (a Sony BTA-NW1, say): which paired one, how to pair it */
|
||||
@Composable
|
||||
fun BluetoothModemFields(s: ModemSettings, onChange: (ModemSettings) -> Unit) {
|
||||
val context = LocalContext.current
|
||||
var picking by remember { mutableStateOf(false) }
|
||||
val paired = remember(picking) { BluetoothBridge.paired(context) }
|
||||
Text(
|
||||
"A modem with Bluetooth's Dial-Up Networking (a Sony BTA-NW1, say) on a phone line: it dials and answers, " +
|
||||
"and the terminal, the internet and fax talk to it in place of D-Modem's own modem (its own AT commands; " +
|
||||
"fax if it does Class 1). Pair it first in Android's Bluetooth settings: an older one asks for its PIN " +
|
||||
"(often 0000, or printed on it).",
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
)
|
||||
Row(Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically) {
|
||||
Text("Modem: " + (s.btModemName.ifBlank { s.btModemAddress }.ifBlank { "none chosen" }), Modifier.weight(1f))
|
||||
TextButton(onClick = { picking = !picking }) { Text("Choose") }
|
||||
}
|
||||
if (picking) {
|
||||
Column(Modifier.padding(start = 16.dp)) {
|
||||
if (paired.isEmpty()) Text("No paired devices", style = MaterialTheme.typography.bodySmall)
|
||||
for ((name, address) in paired) {
|
||||
Text(
|
||||
"$name ($address)",
|
||||
Modifier.fillMaxWidth().clickable {
|
||||
picking = false
|
||||
onChange(s.copy(btModemAddress = address, btModemName = name))
|
||||
}.padding(vertical = 8.dp),
|
||||
)
|
||||
}
|
||||
TextButton(onClick = {
|
||||
runCatching { context.startActivity(android.content.Intent(android.provider.Settings.ACTION_BLUETOOTH_SETTINGS)) }
|
||||
}) { Text("Pair a modem…") }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun LineRadio(label: String, selected: Boolean, onClick: () -> Unit) {
|
||||
Row(Modifier.fillMaxWidth().clickable(onClick = onClick), verticalAlignment = Alignment.CenterVertically) {
|
||||
|
||||
@@ -157,6 +157,8 @@ fun SettingsScreen(modifier: Modifier = Modifier, onOpenTerminal: () -> Unit = {
|
||||
PeerLineFields(s) { s = it }
|
||||
} else if (s.usb) {
|
||||
UsbLineFields()
|
||||
} else if (s.btModem) {
|
||||
BluetoothModemFields(s) { s = it }
|
||||
} else {
|
||||
Text("SIP account", style = MaterialTheme.typography.titleSmall)
|
||||
OutlinedTextField(
|
||||
@@ -277,6 +279,7 @@ private fun lineSummary(s: ModemSettings): String = when (s.line) {
|
||||
ModemSettings.LINE_CELL -> "This phone's mobile calls"
|
||||
ModemSettings.LINE_NONE -> "No line: the modem on its own"
|
||||
ModemSettings.LINE_USB -> "A phone line, through a USB modem"
|
||||
ModemSettings.LINE_BT_MODEM -> "A Bluetooth modem" + s.btModemName.let { if (it.isBlank()) "" else ": $it" }
|
||||
else -> if (s.complete) "SIP: ${s.user}@${s.domain}" + (if (s.listen) ", taking calls" else "") else "SIP: no account set up yet"
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
// Copyright (C) 2026 Neo-Desktop
|
||||
package com.neodesktop.dmodem.modem
|
||||
|
||||
import android.Manifest
|
||||
import android.app.Application
|
||||
import android.bluetooth.BluetoothManager
|
||||
import android.content.Context
|
||||
import androidx.test.core.app.ApplicationProvider
|
||||
import com.neodesktop.dmodem.terminal.TerminalLink
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Assert.fail
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.Shadows.shadowOf
|
||||
import org.robolectric.annotation.Config
|
||||
import java.io.IOException
|
||||
|
||||
/** a paired Bluetooth modem connected (Robolectric's Bluetooth) and its AT commands through the modem's DTE hub */
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
@Config(sdk = [35])
|
||||
class BluetoothModemTest {
|
||||
private val ctx: Context = ApplicationProvider.getApplicationContext()
|
||||
private val adapter get() = ctx.getSystemService(BluetoothManager::class.java).adapter
|
||||
|
||||
private fun waitUntil(what: String, ok: () -> Boolean) {
|
||||
val end = System.currentTimeMillis() + 5000
|
||||
while (!ok() && System.currentTimeMillis() < end) Thread.sleep(20)
|
||||
assertTrue(what, ok())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun theTerminalTalksToIt() {
|
||||
shadowOf(ctx as Application).grantPermissions(Manifest.permission.BLUETOOTH_CONNECT)
|
||||
shadowOf(adapter).setEnabled(true)
|
||||
val bta = adapter.getRemoteDevice("08:00:46:12:34:56")
|
||||
shadowOf(adapter).setBondedDevices(setOf(bta))
|
||||
val log = mutableListOf<String>()
|
||||
val s = BluetoothModem.connect(ctx, "08:00:46:12:34:56") { log += it }
|
||||
assertTrue(log.toString(), log.any { it.contains("(Dial-Up Networking)") })
|
||||
|
||||
val hub = DteHub()
|
||||
val heard = StringBuffer()
|
||||
hub.addListener(TerminalLink.Listener { d, n -> heard.append(String(d, 0, n, Charsets.ISO_8859_1)) })
|
||||
hub.attach(s.inputStream, s.outputStream)
|
||||
assertTrue(hub.write("ATZ\r".toByteArray(), 0, 4))
|
||||
val sink = shadowOf(s).outputStreamSink
|
||||
val got = ByteArray(4)
|
||||
var n = 0
|
||||
while (n < 4) n += sink.read(got, n, 4 - n)
|
||||
assertEquals("ATZ\r", String(got))
|
||||
shadowOf(s).inputStreamFeeder.apply { write("\r\nOK\r\n".toByteArray()); flush() }
|
||||
waitUntil("its OK heard") { heard.contains("OK") }
|
||||
hub.detach()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun offSaysSo() {
|
||||
shadowOf(ctx as Application).grantPermissions(Manifest.permission.BLUETOOTH_CONNECT)
|
||||
shadowOf(adapter).setEnabled(false)
|
||||
try {
|
||||
BluetoothModem.connect(ctx, "08:00:46:12:34:56") {}
|
||||
fail("connected with Bluetooth off")
|
||||
} catch (e: IOException) {
|
||||
assertEquals("Bluetooth is off", e.message)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
// Copyright (C) 2026 Neo-Desktop
|
||||
package com.neodesktop.dmodem.modem
|
||||
|
||||
import com.neodesktop.dmodem.NativeModem
|
||||
import com.neodesktop.dmodem.settings.ModemSettings
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
|
||||
/** a Bluetooth modem's status, from what it says */
|
||||
class ResultCodeStatusTest {
|
||||
private val seen = mutableListOf<NativeModem.Status>()
|
||||
private val r = ResultCodeStatus { seen += it }
|
||||
|
||||
private fun say(s: String) = s.toByteArray(Charsets.ISO_8859_1).let { r.feed(it, it.size) }
|
||||
|
||||
@Test
|
||||
fun onlineFromConnectIdleFromItsEnd() {
|
||||
say("ATDT5551234\r\r\nOK\r\n")
|
||||
assertTrue(seen.isEmpty())
|
||||
// (in pieces, as a socket gives them)
|
||||
say("\r\nCONN")
|
||||
say("ECT 26400/ARQ/V90\r\n")
|
||||
assertEquals(NativeModem.Status(NativeModem.Link.ONLINE, true, 26400, 26400), seen.last())
|
||||
say("some data, CONNECT in it isn't a line\r\n")
|
||||
assertEquals(1, seen.size)
|
||||
say("\r\nNO CARRIER\r\n")
|
||||
assertEquals(ModemState.IDLE, seen.last())
|
||||
say("\r\nCONNECT\r\n")
|
||||
assertEquals(NativeModem.Status(NativeModem.Link.ONLINE, true, 0, 0), seen.last())
|
||||
say("\r\nBUSY\r\n")
|
||||
assertEquals(4, seen.size)
|
||||
assertEquals(ModemState.IDLE, seen.last())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun theLine() {
|
||||
val s = ModemSettings(line = ModemSettings.LINE_BT_MODEM, btModemAddress = "00:11:22:33:44:55")
|
||||
assertTrue(s.btModem)
|
||||
assertFalse(s.peer)
|
||||
assertFalse(s.sip)
|
||||
assertTrue(s.complete)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user