diff --git a/app/src/main/java/de/ccc/events/badge/card10/scanner/ScannerFragment.kt b/app/src/main/java/de/ccc/events/badge/card10/scanner/ScannerFragment.kt index 48ca50e3152fed0c0e834d669dc00217493a781d..6cf743f8a98ef98caae815936d0871b2c3bd77ff 100644 --- a/app/src/main/java/de/ccc/events/badge/card10/scanner/ScannerFragment.kt +++ b/app/src/main/java/de/ccc/events/badge/card10/scanner/ScannerFragment.kt @@ -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 { diff --git a/app/src/main/java/de/ccc/events/badge/card10/scanner/ScannerViewModel.kt b/app/src/main/java/de/ccc/events/badge/card10/scanner/ScannerViewModel.kt new file mode 100644 index 0000000000000000000000000000000000000000..8e54e05c8d78d9f50cdabd15184e3709f06ccabc --- /dev/null +++ b/app/src/main/java/de/ccc/events/badge/card10/scanner/ScannerViewModel.kt @@ -0,0 +1,30 @@ +/* + * 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 diff --git a/app/src/main/res/layout/scanner_fragment.xml b/app/src/main/res/layout/scanner_fragment.xml index 18510309b3b675d3d6343ad393949ca529e03fbf..60c06373b2adf1b9beabdc01bda5d798b2d8bb94 100644 --- a/app/src/main/res/layout/scanner_fragment.xml +++ b/app/src/main/res/layout/scanner_fragment.xml @@ -1,6 +1,18 @@ <?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>