Commit 36061730 authored by Andreas Schildbach's avatar Andreas Schildbach
Browse files

ScannerFragment.kt: Status bar, showing bonding state.

parent 3c5580b1
......@@ -35,6 +35,8 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.LinearLayoutManager
import de.ccc.events.badge.card10.CARD10_BLUETOOTH_MAC_PREFIX
import de.ccc.events.badge.card10.R
......@@ -44,6 +46,7 @@ class ScannerFragment : Fragment() {
lateinit var bluetoothAdapter: BluetoothAdapter
val listAdapter = ScannerListAdapter({ device: Device -> deviceClicked(device) })
val viewModel: ScannerViewModel by lazy { ViewModelProviders.of(this).get(ScannerViewModel::class.java) }
val callback = object : ScanCallback() {
override fun onScanResult(callbackType: Int, result: ScanResult) {
......@@ -67,13 +70,18 @@ class ScannerFragment : Fragment() {
override fun onReceive(context: Context, intent: Intent) {
val bondState = intent.extras!!.getInt(BluetoothDevice.EXTRA_BOND_STATE)
val device = intent.extras!!.getParcelable(BluetoothDevice.EXTRA_DEVICE) as BluetoothDevice?
System.out.println("=== onReceive ${intent.action} ${if (bondState == BluetoothDevice.BOND_NONE) "NONE" else if (bondState == BluetoothDevice.BOND_BONDING) "BONDING" else if (bondState == BluetoothDevice.BOND_BONDED) "BONDED" else bondState} ${device}")
viewModel.status.value =
if (bondState == BluetoothDevice.BOND_NONE) "NONE" else if (bondState == BluetoothDevice.BOND_BONDING) "BONDING" else if (bondState == BluetoothDevice.BOND_BONDED) "BONDED" else null
System.out.println("=== onReceive ${intent.action} ${viewModel.status.value} ${device}")
listAdapter.notifyDataSetChanged()
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
viewModel.status.observe(
this,
Observer { text -> scanner_status.text = text })
activity?.registerReceiver(receiver, IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED))
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
for (device in bluetoothAdapter.bondedDevices.filter {
......
/*
* Copyright by the original author or authors.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package de.ccc.events.badge.card10.scanner
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
class ScannerViewModel : ViewModel() {
val status = MutableLiveData<String>()
}
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/scanner_device_list"
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"/>
android:orientation="vertical"
android:layout_gravity="center_horizontal"
android:padding="16dp">
<androidx.recyclerview.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scanner_device_list"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0px"/>
<TextView android:id="@+id/scanner_status"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment