Commit 87b45acb authored by Anon's avatar Anon

Add scan activity

parent 0d1ac4b0
......@@ -16,6 +16,8 @@
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".ScanActivity" />
</application>
<uses-permission android:name="android.permission.BLUETOOTH"/>
......
package com.github.antweb.donkey
import android.bluetooth.BluetoothDevice
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.TextView
import kotlinx.android.synthetic.main.activity_main.view.*
class DeviceListAdapter(context: Context, private val list: ArrayList<BluetoothDevice> = ArrayList()) :
ArrayAdapter<BluetoothDevice>(context, android.R.layout.simple_list_item_1, list) {
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
val view = if (convertView == null) {
LayoutInflater.from(context).inflate(android.R.layout.simple_list_item_1, parent, false)
} else {
convertView
}
val device = list[position]
val label = "${device.name} (${device.address})"
view.findViewById<TextView>(android.R.id.text1).text = label
return view
}
}
\ No newline at end of file
......@@ -2,6 +2,7 @@ package com.github.antweb.donkey
import android.bluetooth.*
import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
......@@ -10,9 +11,9 @@ import android.widget.Button
import android.widget.TextView
import java.lang.NullPointerException
class MainActivity : AppCompatActivity() {
private const val TAG = "MainActivity"
private val TAG = "MainActivity"
class MainActivity : AppCompatActivity() {
private val targetDeviceName = "card10"
private val mtu = 128
......@@ -47,15 +48,12 @@ class MainActivity : AppCompatActivity() {
buttonConnect = findViewById(R.id.button_connect)
buttonConnect.setOnClickListener {
scanLeDevice()
}
}
// scanLeDevice()
private fun checkPermissions() {
// if (bluetoothAdapter == null || !(bluetoothAdapter?.isEnabled)) {
// val enableBtIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
// startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT)
// }
val intent = Intent(this, ScanActivity::class.java)
startActivity(intent)
}
}
private fun scanLeDevice() {
......
package com.github.antweb.donkey
import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothDevice
import android.bluetooth.BluetoothManager
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.widget.ListView
import androidx.appcompat.app.AppCompatActivity
private const val TAG = "ScanActivity"
class ScanActivity : AppCompatActivity() {
private lateinit var listView: ListView
private lateinit var listAdapter: DeviceListAdapter
private var mScanning = false
private val bluetoothAdapter: BluetoothAdapter? by lazy(LazyThreadSafetyMode.NONE) {
val bluetoothManager = getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
bluetoothManager.adapter
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_scan)
listView = findViewById(R.id.list_view_devices)
listAdapter = DeviceListAdapter(applicationContext)
listView.adapter = listAdapter
checkPermissions()
scan()
}
private fun checkPermissions() {
bluetoothAdapter?.takeIf { !it.isEnabled }?.apply {
val enableBtIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
startActivity(enableBtIntent)
}
}
private fun scan() {
val foundDevices = mutableSetOf<BluetoothDevice>()
val leScanCallback = BluetoothAdapter.LeScanCallback { device, _, _ ->
// if (device.name == targetDeviceName) {
// if (!connected) {
// connected = true
// bluetoothGatt = device.connectGatt(this, true, gattCallback, BluetoothDevice.TRANSPORT_LE)
// }
// }
if (!foundDevices.contains(device)) {
foundDevices.add(device)
listAdapter.add(device)
}
}
// Stops scanning after a pre-defined period
Handler().postDelayed({
mScanning = false
bluetoothAdapter?.stopLeScan(leScanCallback)
}, 5000)
mScanning = true
bluetoothAdapter?.startLeScan(leScanCallback)
}
}
\ No newline at end of file
......@@ -4,4 +4,9 @@
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/list_view_devices"
/>
</LinearLayout>
\ No newline at end of file
Markdown is supported
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