mirror of
https://github.com/meshtastic/Meshtastic-Android.git
synced 2026-04-20 22:23:37 +00:00
we now allow changing BLE connection to different meshtastic devices on the fly
This commit is contained in:
parent
468e43afc4
commit
69e6b285c6
6 changed files with 53 additions and 36 deletions
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
)
|
||||
|
||||
|
||||
|
||||
*/
|
||||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_margin="4dp"
|
||||
|
|
@ -18,7 +19,8 @@
|
|||
android:layout_marginTop="8dp"
|
||||
android:text="@string/some_username"
|
||||
app:chipIcon="@drawable/ic_twotone_person_24"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="@+id/messageTime"
|
||||
app:layout_constraintStart_toStartOf="@+id/messageTime"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
|
||||
<TextView
|
||||
|
|
@ -33,6 +35,14 @@
|
|||
app:layout_constraintStart_toEndOf="@+id/username"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/messageTime"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="3 min"
|
||||
app:layout_constraintTop_toBottomOf="@+id/username"
|
||||
tools:layout_editor_absoluteX="25dp" />
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
|
||||
</LinearLayout>
|
||||
|
|
@ -12,7 +12,7 @@
|
|||
<string name="user_avatar">User avatar</string>
|
||||
<string name="sample_distance" translatable="false">2.13 km</string>
|
||||
<string name="sample_message">hey I found the cache, it is over here next to the big tiger. I\'m kinda scared.</string>
|
||||
<string name="some_username">Some Username</string>
|
||||
<string name="some_username" translatable="false">SKH</string>
|
||||
<string name="send_text">Send Text</string>
|
||||
<string name="warning_not_paired">You haven\'t yet paired a Meshtastic compatible radio with this phone. Please pair a device and set your username.\n\nThis open-source application is in alpha-testing, if you find problems please post on our website chat.\n\nFor more information see our web page - www.meshtastic.org.</string>
|
||||
<string name="username_unset">Username unset</string>
|
||||
|
|
@ -23,4 +23,6 @@
|
|||
<string name="test_devname2" translatable="false">Meshtastic_1267</string>
|
||||
<string name="requires_bluetooth">This application requires bluetooth access. Please grant access in android settings.</string>
|
||||
<string name="error_bluetooth">Error - this app requires bluetooth</string>
|
||||
<string name="starting_pairing">Starting pairing</string>
|
||||
<string name="pairing_failed">Pairing failed</string>
|
||||
</resources>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue