[+] Store config on scan complete
This commit is contained in:
@@ -4,6 +4,7 @@ import android.annotation.SuppressLint
|
|||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.os.Handler
|
import android.os.Handler
|
||||||
import android.os.Looper
|
import android.os.Looper
|
||||||
|
import android.view.View
|
||||||
import android.widget.ArrayAdapter
|
import android.widget.ArrayAdapter
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import com.welie.blessed.BluetoothPeripheral
|
import com.welie.blessed.BluetoothPeripheral
|
||||||
@@ -14,11 +15,13 @@ import java.util.*
|
|||||||
|
|
||||||
class ActivityScan : AppCompatActivity() {
|
class ActivityScan : AppCompatActivity() {
|
||||||
lateinit var binding: ActivityScanBinding
|
lateinit var binding: ActivityScanBinding
|
||||||
|
lateinit var view: View
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
binding = ActivityScanBinding.inflate(layoutInflater)
|
binding = ActivityScanBinding.inflate(layoutInflater)
|
||||||
setContentView(binding.root)
|
setContentView(binding.root)
|
||||||
|
view = binding.root
|
||||||
|
|
||||||
initBluetoothHandler()
|
initBluetoothHandler()
|
||||||
}
|
}
|
||||||
@@ -27,6 +30,12 @@ class ActivityScan : AppCompatActivity() {
|
|||||||
private fun initBluetoothHandler() {
|
private fun initBluetoothHandler() {
|
||||||
println("OnCreate called, Initializing...")
|
println("OnCreate called, Initializing...")
|
||||||
|
|
||||||
|
// Device exists
|
||||||
|
chosenDevice?.let { addr ->
|
||||||
|
ble.connectAddress(addr) { connected(addr) }
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// List bonded device addresses
|
// List bonded device addresses
|
||||||
val pairedDevices = blueMan().adapter.bondedDevices.toList()
|
val pairedDevices = blueMan().adapter.bondedDevices.toList()
|
||||||
val pairedAddresses = pairedDevices.map { it.address }.toSet()
|
val pairedAddresses = pairedDevices.map { it.address }.toSet()
|
||||||
@@ -51,7 +60,7 @@ class ActivityScan : AppCompatActivity() {
|
|||||||
binding.lvScanned.setOnItemClickListener { parent, view, position, id ->
|
binding.lvScanned.setOnItemClickListener { parent, view, position, id ->
|
||||||
ble.central.stopScan()
|
ble.central.stopScan()
|
||||||
ble.connectPeripheral(scannedDevices[position]) {
|
ble.connectPeripheral(scannedDevices[position]) {
|
||||||
view.snack("✅ Connected.")
|
connected(scannedDevices[position].address)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -69,8 +78,15 @@ class ActivityScan : AppCompatActivity() {
|
|||||||
|
|
||||||
// Scan for the device with the MAC address
|
// Scan for the device with the MAC address
|
||||||
ble.connectAddress(dev.address) {
|
ble.connectAddress(dev.address) {
|
||||||
view.snack("✅ Connected.")
|
connected(dev.address)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun connected(address: String)
|
||||||
|
{
|
||||||
|
view.snack("✅ Connected.")
|
||||||
|
chosenDevice = address
|
||||||
|
finish()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,12 +1,20 @@
|
|||||||
package org.hydev.wearsync
|
package org.hydev.wearsync
|
||||||
|
|
||||||
|
import android.app.Activity
|
||||||
import android.bluetooth.BluetoothManager
|
import android.bluetooth.BluetoothManager
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
|
import android.content.Intent
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
|
import androidx.core.content.edit
|
||||||
import com.google.android.material.snackbar.Snackbar
|
import com.google.android.material.snackbar.Snackbar
|
||||||
|
|
||||||
fun View.snack(msg: String) = Snackbar.make(this, msg, Snackbar.LENGTH_LONG)
|
fun View.snack(msg: String) = Snackbar.make(this, msg, Snackbar.LENGTH_LONG)
|
||||||
.setAction("Action", null).show()
|
.setAction("Action", null).show()
|
||||||
|
|
||||||
fun Context.blueMan() = getSystemService(AppCompatActivity.BLUETOOTH_SERVICE) as BluetoothManager
|
fun Context.blueMan() = getSystemService(AppCompatActivity.BLUETOOTH_SERVICE) as BluetoothManager
|
||||||
|
val Context.pref get() = getSharedPreferences("settings", Context.MODE_PRIVATE)
|
||||||
|
var Context.chosenDevice: String?
|
||||||
|
get() = pref.getString("mac", null)
|
||||||
|
set(value) = pref.edit { putString("mac", value) }
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user