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

Bluetooth Pairing by clicking on device in scanner.

parent a8d69a8e
......@@ -16,4 +16,19 @@
package com.example.card10companion.scanner;
data class Device(val btMac: String, val name: String?)
import java.util.*
data class Device(val btMac: String, val name: String?, val paired: Boolean) {
override fun equals(other: Any?): Boolean{
if (this === other) return true
if (other?.javaClass != javaClass) return false
other as Device
if (!Objects.equals(btMac, other.btMac)) return false
return true
}
override fun hashCode(): Int{
return Objects.hashCode(btMac)
}
}
......@@ -16,13 +16,15 @@
package com.example.card10companion.scanner
import android.graphics.Typeface
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.card10companion.R
class DeviceViewHolder(inflater: LayoutInflater, parent: ViewGroup) : RecyclerView.ViewHolder(inflater.inflate(R.layout.scanner_list_item, parent, false)) {
class DeviceViewHolder(inflater: LayoutInflater, parent: ViewGroup) :
RecyclerView.ViewHolder(inflater.inflate(R.layout.scanner_list_item, parent, false)) {
private var nameView: TextView? = null
private var btMacView: TextView? = null
......@@ -32,8 +34,12 @@ class DeviceViewHolder(inflater: LayoutInflater, parent: ViewGroup) : RecyclerVi
btMacView = itemView.findViewById(R.id.scanner_list_item_btmac)
}
fun bind(device: Device) {
fun bind(device: Device, clickListener: (Device) -> Unit) {
nameView?.text = device.name
btMacView?.text = device.btMac
btMacView?.typeface = if (device.paired) Typeface.DEFAULT_BOLD else Typeface.DEFAULT
itemView.setOnClickListener {
clickListener(device)
}
}
}
......@@ -17,8 +17,13 @@
package com.example.card10companion.scanner
import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothDevice
import android.bluetooth.le.ScanCallback
import android.bluetooth.le.ScanResult
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
......@@ -31,17 +36,32 @@ import kotlinx.android.synthetic.main.scanner_fragment.*
class ScannerFragment : Fragment() {
lateinit var bluetoothAdapter: BluetoothAdapter
val listAdapter = ScannerListAdapter()
val listAdapter = ScannerListAdapter({ device: Device -> deviceClicked(device) })
val callback = object:ScanCallback() {
val callback = object : ScanCallback() {
override fun onScanResult(callbackType: Int, result: ScanResult) {
System.out.println("===== onScanResult " + result)
listAdapter.maybeAdd(Device(btMac = result.device.address, name = result.device.name))
val device = result.device
listAdapter.maybeAdd(
Device(
btMac = device.address,
name = device.name,
paired = device.bondState == BluetoothDevice.BOND_BONDED
)
)
}
}
val receiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
System.out.println("===== onReceive " + intent)
listAdapter.notifyDataSetChanged()
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
activity?.registerReceiver(receiver, IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED))
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
bluetoothAdapter.bluetoothLeScanner.startScan(callback)
}
......@@ -60,6 +80,13 @@ class ScannerFragment : Fragment() {
override fun onDestroy() {
bluetoothAdapter.bluetoothLeScanner.stopScan(callback)
activity?.unregisterReceiver(receiver)
super.onDestroy()
}
fun deviceClicked(device: Device) {
System.out.println("===== deviceClicked " + device)
bluetoothAdapter.bluetoothLeScanner.stopScan(callback)
bluetoothAdapter.getRemoteDevice(device.btMac).createBond();
}
}
......@@ -21,7 +21,7 @@ import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import java.util.*
class ScannerListAdapter() : RecyclerView.Adapter<DeviceViewHolder>() {
class ScannerListAdapter(val clickListener: (Device) -> Unit) : RecyclerView.Adapter<DeviceViewHolder>() {
private val list = LinkedList<Device>()
......@@ -32,7 +32,7 @@ class ScannerListAdapter() : RecyclerView.Adapter<DeviceViewHolder>() {
override fun onBindViewHolder(holder: DeviceViewHolder, position: Int) {
val device: Device = list[position]
holder.bind(device)
holder.bind(device, clickListener)
}
override fun getItemCount(): Int = list.size
......
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