we now allow changing BLE connection to different meshtastic devices on the fly

This commit is contained in:
geeksville 2020-04-09 13:28:44 -07:00
parent 468e43afc4
commit 69e6b285c6
6 changed files with 53 additions and 36 deletions

View file

@ -361,7 +361,7 @@ class MainActivity : AppCompatActivity(), Logging,
var receiverRegistered = false
private fun registerMeshReceiver() {
logAssert(!receiverRegistered)
unregisterMeshReceiver()
val filter = IntentFilter()
filter.addAction(MeshService.ACTION_MESH_CONNECTED)
filter.addAction(MeshService.ACTION_NODE_CHANGE)
@ -392,7 +392,7 @@ class MainActivity : AppCompatActivity(), Logging,
debug("Getting latest radioconfig from service")
model.radioConfig.value =
MeshProtos.RadioConfig.parseFrom(model.meshService!!.radioConfig)
// Pull down our real node ID
model.nodeDB.myId.value = service.myId
@ -500,7 +500,7 @@ class MainActivity : AppCompatActivity(), Logging,
}
}
private fun bindMeshService() {
fun bindMeshService() {
debug("Binding to mesh service!")
// we bind using the well known name, to make sure 3rd party apps could also
if (model.meshService != null)
@ -512,7 +512,7 @@ class MainActivity : AppCompatActivity(), Logging,
}
}
private fun unbindMeshService() {
fun unbindMeshService() {
// If we have received the service, and hence registered with
// it, then now is the time to unregister.
// if we never connected, do nothing
@ -535,10 +535,8 @@ class MainActivity : AppCompatActivity(), Logging,
val bonded =
RadioInterfaceService.getBondedDeviceAddress(this) != null
/* FIXME - not yet working
if (!bonded)
AppStatus.currentScreen = Screen.settings
*/
pager.currentItem = 5
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {

View file

@ -180,13 +180,14 @@ class RadioInterfaceService : Service(), Logging {
// Force the service to reconnect
val s = runningService
if (s != null) {
info("shutting down old service")
s.setEnabled(false) // nasty, needed to force the next setEnabled call to reconnect
info("Setting enable on the running radio service")
s.setEnabled(addr != null)
} else {
if (addr != null) {
info("We have a device addr now, starting mesh service")
MeshService.startService(context)
}
}
if (addr != null) {
info("We have a device addr now, starting mesh service")
MeshService.startService(context)
}
}
}

View file

@ -16,6 +16,7 @@ import com.geeksville.mesh.model.TextMessage
import com.geeksville.mesh.model.UIViewModel
import kotlinx.android.synthetic.main.adapter_message_layout.view.*
import kotlinx.android.synthetic.main.messages_fragment.*
import java.text.SimpleDateFormat
// Allows usage like email.on(EditorInfo.IME_ACTION_NEXT, { confirm() })
fun EditText.on(actionId: Int, func: () -> Unit) {
@ -38,6 +39,7 @@ class MessagesFragment : ScreenFragment("Messages"), Logging {
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val username = itemView.username
val messageText = itemView.messageText
val messageTime = itemView.messageTime
}
private val messagesAdapter = object : RecyclerView.Adapter<ViewHolder>() {
@ -121,6 +123,8 @@ class MessagesFragment : ScreenFragment("Messages"), Logging {
} else {
holder.messageText.text = msg.text
}
holder.messageTime.text = dateFormat.format(msg.date)
}
private var messages = arrayOf<TextMessage>()
@ -164,21 +168,7 @@ class MessagesFragment : ScreenFragment("Messages"), Logging {
messagesAdapter.onMessagesChanged(it)
})
}
private val dateFormat = SimpleDateFormat("h:mm a")
}
/*
private val dateFormat = SimpleDateFormat("h:mm a")
ProvideEmphasis(emphasis = TimestampEmphasis) {
Text(
text = dateFormat.format(msg.date),
modifier = LayoutPadding(start = 8.dp),
style = MaterialTheme.typography.caption
)
*/

View file

@ -19,8 +19,10 @@ import androidx.fragment.app.activityViewModels
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Observer
import com.geeksville.android.GeeksvilleApplication
import com.geeksville.android.Logging
import com.geeksville.android.hideKeyboard
import com.geeksville.mesh.MainActivity
import com.geeksville.mesh.R
import com.geeksville.mesh.model.UIViewModel
import com.geeksville.mesh.service.RadioInterfaceService
@ -82,7 +84,7 @@ class BTScanModel(app: Application) : AndroidViewModel(app), Logging {
// If nothing was selected, by default select the first thing we see
if (selectedMacAddr == null && entry.bonded)
changeSelection(context, addr)
changeSelection(GeeksvilleApplication.currentActivity as MainActivity, addr)
devices.value = oldDevs + Pair(addr, entry) // trigger gui updates
}
@ -117,7 +119,10 @@ class BTScanModel(app: Application) : AndroidViewModel(app), Logging {
// If nothing was selected, by default select the first thing we see
if (selectedMacAddr == null)
changeSelection(context, testnodes.first().macAddress)
changeSelection(
GeeksvilleApplication.currentActivity as MainActivity,
testnodes.first().macAddress
)
} else {
/// The following call might return null if the user doesn't have bluetooth access permissions
val s: BluetoothLeScanner? = bluetoothAdapter.bluetoothLeScanner
@ -168,7 +173,7 @@ class BTScanModel(app: Application) : AndroidViewModel(app), Logging {
fun onSelected(it: BTScanEntry): Boolean {
// If the device is paired, let user select it, otherwise start the pairing flow
if (it.bonded) {
changeSelection(context, it.macAddress)
changeSelection(GeeksvilleApplication.currentActivity as MainActivity, it.macAddress)
return true
} else {
info("Starting bonding for $it")
@ -190,7 +195,7 @@ class BTScanModel(app: Application) : AndroidViewModel(app), Logging {
if (state == BluetoothDevice.BOND_BONDED || state == BluetoothDevice.BOND_BONDING) {
debug("Bonding completed, connecting service")
changeSelection(
context,
GeeksvilleApplication.currentActivity as MainActivity,
it.macAddress
)
@ -214,10 +219,14 @@ class BTScanModel(app: Application) : AndroidViewModel(app), Logging {
}
/// Change to a new macaddr selection, updating GUI and radio
fun changeSelection(context: Context, newAddr: String) {
fun changeSelection(context: MainActivity, newAddr: String) {
info("Changing BT device to $newAddr")
selectedMacAddr = newAddr
RadioInterfaceService.setBondedDeviceAddress(context, newAddr)
// Super ugly hack. we force the activity to reconnect FIXME, find a cleaner way
context.unbindMeshService()
context.bindMeshService()
}
}
@ -275,7 +284,14 @@ class SettingsFragment : ScreenFragment("Settings"), Logging {
deviceRadioGroup.addView(b)
b.setOnClickListener {
b.isChecked = scanModel.onSelected(device)
scanProgressBar.visibility = View.INVISIBLE
if (!device.bonded)
scanStatusText.setText(R.string.starting_pairing)
b.isSelected = scanModel.onSelected(device)
if (!b.isSelected)
scanStatusText.setText(R.string.pairing_failed)
}
}