|
@@ -18,19 +18,15 @@ import android.os.Bundle
|
|
import android.util.Log
|
|
import android.util.Log
|
|
import android.view.View
|
|
import android.view.View
|
|
import android.widget.Button
|
|
import android.widget.Button
|
|
-import android.widget.ListView
|
|
|
|
import android.widget.ProgressBar
|
|
import android.widget.ProgressBar
|
|
import android.widget.TextView
|
|
import android.widget.TextView
|
|
import androidx.activity.result.contract.ActivityResultContracts
|
|
import androidx.activity.result.contract.ActivityResultContracts
|
|
import androidx.appcompat.app.AppCompatActivity
|
|
import androidx.appcompat.app.AppCompatActivity
|
|
import androidx.core.app.ActivityCompat
|
|
import androidx.core.app.ActivityCompat
|
|
|
|
+import androidx.recyclerview.widget.RecyclerView
|
|
|
|
|
|
|
|
|
|
class XDScanBLEActivity : AppCompatActivity() {
|
|
class XDScanBLEActivity : AppCompatActivity() {
|
|
- companion object {
|
|
|
|
- const val REQUEST_ENABLE_BT = 10086
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
private val permissions: Array<String> by lazy {
|
|
private val permissions: Array<String> by lazy {
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
|
|
arrayOf(
|
|
arrayOf(
|
|
@@ -47,7 +43,7 @@ class XDScanBLEActivity : AppCompatActivity() {
|
|
|
|
|
|
private lateinit var txtMsg: TextView
|
|
private lateinit var txtMsg: TextView
|
|
private lateinit var btnScan: Button
|
|
private lateinit var btnScan: Button
|
|
- private lateinit var listview: ListView
|
|
|
|
|
|
+ private lateinit var listview: RecyclerView
|
|
private lateinit var progress: ProgressBar
|
|
private lateinit var progress: ProgressBar
|
|
|
|
|
|
private val broadcastReceiver by lazy {
|
|
private val broadcastReceiver by lazy {
|
|
@@ -89,21 +85,14 @@ class XDScanBLEActivity : AppCompatActivity() {
|
|
|
|
|
|
private var bluetoothAdapter: BluetoothAdapter? = null
|
|
private var bluetoothAdapter: BluetoothAdapter? = null
|
|
|
|
|
|
-
|
|
|
|
- private val requestMultiplePermissionLauncher = registerForActivityResult(
|
|
|
|
- ActivityResultContracts.RequestMultiplePermissions()
|
|
|
|
- ) { permissions: Map<String, Boolean> ->
|
|
|
|
- val noGrantedPermissions = ArrayList<String>()
|
|
|
|
- permissions.entries.forEach {
|
|
|
|
- if (!it.value) {
|
|
|
|
- noGrantedPermissions.add(it.key)
|
|
|
|
|
|
+ private val requestMultiplePermissionLauncher by lazy {
|
|
|
|
+ registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) {
|
|
|
|
+ if (it.all { permission -> permission.value }) {
|
|
|
|
+ maybeSetupBluetooth()
|
|
|
|
+ } else {
|
|
|
|
+ setStatus(XDScanBLEStatus.Permission)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- if (noGrantedPermissions.isEmpty()) {
|
|
|
|
- maybeSetupBluetooth()
|
|
|
|
- } else {
|
|
|
|
- setStatus(XDScanBLEStatus.Permission)
|
|
|
|
- }
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -126,11 +115,11 @@ class XDScanBLEActivity : AppCompatActivity() {
|
|
val deviceAddress = device.address
|
|
val deviceAddress = device.address
|
|
val rssi = result.rssi
|
|
val rssi = result.rssi
|
|
|
|
|
|
- Log.d(
|
|
|
|
- "XDScanBLEService",
|
|
|
|
- "发现BLE设备: 名称 - $deviceName, 地址 - $deviceAddress, 信号强度 - $rssi"
|
|
|
|
- )
|
|
|
|
-
|
|
|
|
|
|
+ // Log.d(
|
|
|
|
+ // "XDScanBLEService",
|
|
|
|
+ // "发现BLE设备: 名称 - $deviceName, 地址 - $deviceAddress, 信号强度 - $rssi"
|
|
|
|
+ // )
|
|
|
|
+ adapter.addValue(result)
|
|
}
|
|
}
|
|
|
|
|
|
override fun onScanFailed(errorCode: Int) {
|
|
override fun onScanFailed(errorCode: Int) {
|
|
@@ -144,18 +133,6 @@ class XDScanBLEActivity : AppCompatActivity() {
|
|
super.onCreate(savedInstanceState)
|
|
super.onCreate(savedInstanceState)
|
|
setContentView(R.layout.activity_scanble)
|
|
setContentView(R.layout.activity_scanble)
|
|
|
|
|
|
- txtMsg = findViewById(R.id.scan_message)
|
|
|
|
- btnScan = findViewById(R.id.scan_button)
|
|
|
|
- listview = findViewById(R.id.scan_list)
|
|
|
|
- progress = findViewById(R.id.scan_progress)
|
|
|
|
-
|
|
|
|
- btnScan.setOnClickListener {
|
|
|
|
- setStatus(XDScanBLEStatus.Scanning)
|
|
|
|
- startScanBle()
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- setStatus(XDScanBLEStatus.Idle)
|
|
|
|
- checkBluetoothPermission()
|
|
|
|
registerReceiver(broadcastReceiver, IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED))
|
|
registerReceiver(broadcastReceiver, IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED))
|
|
}
|
|
}
|
|
|
|
|
|
@@ -197,7 +174,6 @@ class XDScanBLEActivity : AppCompatActivity() {
|
|
|
|
|
|
@SuppressLint("MissingPermission")
|
|
@SuppressLint("MissingPermission")
|
|
private fun startScanBle() {
|
|
private fun startScanBle() {
|
|
-
|
|
|
|
val settings =
|
|
val settings =
|
|
ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY) // 或选择其他适合的扫描模式
|
|
ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY) // 或选择其他适合的扫描模式
|
|
.build()
|
|
.build()
|