具有待定意图的 BLE 扫描仪不提供扫描结果

问题描述 投票:0回答:1

尝试使用

startScan
来实现待定意图。广播接收器被调用,但是提供的意图没有任何额外内容(并且意图中没有任何扫描结果)。这段代码在旧的 Android 版本中运行良好。

这是我的扫描仪:

fun ComponentActivity.startScanner() {
    if (!checkForPermission(
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S){
                Manifest.permission.ACCESS_COARSE_LOCATION
            }
            else {
                Manifest.permission.BLUETOOTH_SCAN
            }
    )){
        return
    }

    with(logViewModel) {
        if (!haveBtPermissions) {
            haveBtPermissions = true
            clear()
        }
    }

//    bluetoothAdapter.bluetoothLeScanner.startScan(filters, settings, scanCallback)
    bluetoothAdapter.bluetoothLeScanner.startScan(filters, settings, pendingIntent)
    Log.i(TAG, "Scanner started")
}

private val filters get() = mutableListOf<ScanFilter>().apply {
    val filterShortServiceUUID = ScanFilter.Builder()
        .setServiceUuid(ParcelUuid.fromString ("0000${"180D"}-0000-1000-8000-00805F9B34FB"))
        .build()
    add(filterShortServiceUUID)
}

private val settings get() = ScanSettings.Builder().setScanMode(
    ScanSettings.SCAN_MODE_BALANCED
).build()

private val ComponentActivity.bluetoothAdapter get() = (applicationContext
    .getSystemService(ComponentActivity.BLUETOOTH_SERVICE) as BluetoothManager).adapter

private val ComponentActivity.pendingIntent get() =
    applicationContext.let {appContext ->
        val intent = Intent(appContext, DeviceBroadcastReceiver::class.java)
//            .setAction("com.solvek.bletrigger.ACTION_FOUND")

        PendingIntent.getBroadcast(
            appContext, 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT or FLAG_IMMUTABLE
        )!!
    }

private fun ComponentActivity.checkForPermission(permission: String): Boolean {
    if (ActivityCompat.checkSelfPermission(
            this,
            permission
        ) == PackageManager.PERMISSION_GRANTED
    ) {
        return true
    }

    with(logViewModel) {
        append("Please grant permissions and restart app!")
        haveBtPermissions = false
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S){
        registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()){
            startScanner()
        }.launch(arrayOf(
            Manifest.permission.BLUETOOTH_SCAN,
            Manifest.permission.BLUETOOTH_CONNECT
        ))
    }

    return false
}

接收器

class DeviceBroadcastReceiver: BroadcastReceiver() {
    override fun onReceive(context: Context?, intent: Intent?) {
        Log.i(TAG, "===== A new message received")
        if (context == null){
            Log.e(TAG, "Context is null")
            return
        }

        if (intent == null){
            Log.e(TAG, "Intent is null")
            return
        }

        val bleCallbackType: Int = intent.getIntExtra(BluetoothLeScanner.EXTRA_CALLBACK_TYPE, -1)
        if (bleCallbackType == -1){
            Log.w(TAG, "Callback type not specified")
            return
        }
        Log.i(TAG, "Received callback type $bleCallbackType")

并体现

 <uses-permission
        android:name="android.permission.BLUETOOTH"
        android:maxSdkVersion="30" />
    <uses-permission
        android:name="android.permission.BLUETOOTH_ADMIN"
        android:maxSdkVersion="30" />
    <uses-permission
        android:name="android.permission.ACCESS_FINE_LOCATION"
        android:maxSdkVersion="30" />
    <uses-permission
        android:name="android.permission.ACCESS_BACKGROUND_LOCATION"
        android:maxSdkVersion="30" />
    <uses-permission
        android:name="android.permission.ACCESS_COARSE_LOCATION"
        android:maxSdkVersion="30" />

    <!-- Request Bluetooth permissions for devices on API 31,32. -->
    <uses-permission
        android:name="android.permission.BLUETOOTH_SCAN"
        tools:targetApi="s" />
    <!-- Needed only if your app communicates with already-paired Bluetooth
             devices. -->
    <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
    <!-- Since Bluetooth is a basic requirement for our app -->
    <uses-feature
        android:name="android.hardware.bluetooth"
        android:required="true" />

    <application
...

        <receiver android:name=".DeviceBroadcastReceiver"
            android:exported="false">
<!--            <intent-filter>-->
<!--                <action android:name="com.solvek.bletrigger.ACTION_FOUND" />-->
<!--            </intent-filter>-->
        </receiver>
android bluetooth-lowenergy android-pendingintent
1个回答
0
投票

蓝牙扫描权限是运行时权限,因此必须在运行时请求。检查权限还不够。

© www.soinside.com 2019 - 2024. All rights reserved.