kopia lustrzana https://github.com/meshtastic/Meshtastic-Android
wait for bonding complete before telling service to connect
rodzic
8220e1210f
commit
7d76f99731
|
@ -4,6 +4,7 @@ import android.annotation.SuppressLint
|
||||||
import android.app.Application
|
import android.app.Application
|
||||||
import android.bluetooth.BluetoothDevice
|
import android.bluetooth.BluetoothDevice
|
||||||
import android.bluetooth.BluetoothDevice.BOND_BONDED
|
import android.bluetooth.BluetoothDevice.BOND_BONDED
|
||||||
|
import android.bluetooth.BluetoothDevice.BOND_BONDING
|
||||||
import android.bluetooth.BluetoothManager
|
import android.bluetooth.BluetoothManager
|
||||||
import android.bluetooth.le.*
|
import android.bluetooth.le.*
|
||||||
import android.companion.AssociationRequest
|
import android.companion.AssociationRequest
|
||||||
|
@ -48,7 +49,7 @@ fun changeDeviceSelection(context: MainActivity, newAddr: String?) {
|
||||||
private fun requestBonding(
|
private fun requestBonding(
|
||||||
activity: MainActivity,
|
activity: MainActivity,
|
||||||
device: BluetoothDevice,
|
device: BluetoothDevice,
|
||||||
onSuccess: () -> Unit
|
onComplete: (Int) -> Unit
|
||||||
) {
|
) {
|
||||||
SLogging.info("Starting bonding for $device")
|
SLogging.info("Starting bonding for $device")
|
||||||
|
|
||||||
|
@ -65,10 +66,13 @@ private fun requestBonding(
|
||||||
-1
|
-1
|
||||||
)
|
)
|
||||||
SLogging.debug("Received bond state changed $state")
|
SLogging.debug("Received bond state changed $state")
|
||||||
context.unregisterReceiver(this)
|
|
||||||
if (state == BluetoothDevice.BOND_BONDED || state == BluetoothDevice.BOND_BONDING) {
|
if (state != BOND_BONDING) {
|
||||||
SLogging.debug("Bonding completed, connecting service")
|
context.unregisterReceiver(this) // we stay registered until bonding completes (either with BONDED or NONE)
|
||||||
onSuccess()
|
if (state == BOND_BONDED) {
|
||||||
|
SLogging.debug("Bonding completed, state=$state")
|
||||||
|
onComplete(state)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -252,11 +256,16 @@ class BTScanModel(app: Application) : AndroidViewModel(app), Logging {
|
||||||
// We ignore missing BT adapters, because it lets us run on the emulator
|
// We ignore missing BT adapters, because it lets us run on the emulator
|
||||||
bluetoothAdapter
|
bluetoothAdapter
|
||||||
?.getRemoteDevice(it.macAddress)?.let { device ->
|
?.getRemoteDevice(it.macAddress)?.let { device ->
|
||||||
requestBonding(activity, device) {
|
requestBonding(activity, device) { state ->
|
||||||
changeScanSelection(
|
if (state == BOND_BONDED) {
|
||||||
activity,
|
errorText.value = activity.getString(R.string.pairing_completed)
|
||||||
device.address
|
changeScanSelection(
|
||||||
)
|
activity,
|
||||||
|
device.address
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
errorText.value = activity.getString(R.string.pairing_failed_try_again)
|
||||||
|
}
|
||||||
|
|
||||||
// Force the GUI to redraw
|
// Force the GUI to redraw
|
||||||
devices.value = devices.value
|
devices.value = devices.value
|
||||||
|
@ -356,7 +365,7 @@ class SettingsFragment : ScreenFragment("Settings"), Logging {
|
||||||
scanModel.onSelected(requireActivity() as MainActivity, device)
|
scanModel.onSelected(requireActivity() as MainActivity, device)
|
||||||
|
|
||||||
if (!b.isSelected)
|
if (!b.isSelected)
|
||||||
scanStatusText.setText(R.string.pairing_failed)
|
scanStatusText.setText(getString(R.string.please_pair))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -45,4 +45,7 @@
|
||||||
<string name="current_pair">You are currently paired with radio %s</string>
|
<string name="current_pair">You are currently paired with radio %s</string>
|
||||||
<string name="not_paired_yet">You have not paired a radio yet.</string>
|
<string name="not_paired_yet">You have not paired a radio yet.</string>
|
||||||
<string name="change_radio">Change radio</string>
|
<string name="change_radio">Change radio</string>
|
||||||
|
<string name="please_pair">Please pair device in Android Settings.</string>
|
||||||
|
<string name="pairing_completed">Pairing completed, starting service</string>
|
||||||
|
<string name="pairing_failed_try_again">Pairing failed, please select again</string>
|
||||||
</resources>
|
</resources>
|
||||||
|
|
Ładowanie…
Reference in New Issue