BroadcastReceiver 和 ACTION_BOND_STATE_CHANGED 在 Android 9.0 中部分工作

Mat*_* M. 8 android android-bluetooth

我试图通过广播接收器捕获与 android 的配对过程中的事件。看起来,这BluetoothDevice.BOND_BONDING是可行的,但BluetoothDevice.BOND_BONDED不是。

在旧的 Android 版本中,此方法有效(尝试过 Android 6 和 7),但在较新的 Android 版本中(尝试过 Android 9,多种设备),此方法不起作用。为了重现这个问题,我做了一个简单的程序:

Java文件:


package com.example.bluetoothtest;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends AppCompatActivity {

    BroadcastReceiver receiver;
    BluetoothDevice mDevice;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
                if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)){
                    mDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                    if (mDevice.getBondState() == BluetoothDevice.BOND_BONDED) {
                        //means device paired
                        Log.d("bt", "bonded");
                    }
                    else if(mDevice.getBondState() == BluetoothDevice.BOND_BONDING) {
                        Log.d("bt", "bonding");
                    }
                }
            }
        };
    }

    @Override
    protected void onStart() {
        super.onStart();
        IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
        registerReceiver(receiver, filter);
    }

    @Override
    protected void onStop() {
        super.onStop();
        unregisterReceiver(receiver);
    }
}

Run Code Online (Sandbox Code Playgroud)

显现:


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.bluetoothtest">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

</manifest>
Run Code Online (Sandbox Code Playgroud)

还有其他人注意到这个问题吗?我是否缺少权限?在网上找不到任何相关内容。

Gok*_*kul 1

我也遇到过类似的问题。我注意到耳机、鼠标或键盘等设备的一件事是,当您配对这些设备时,它会立即连接到我们的 Android 设备。所以android发送给我们

android.bluetooth.device.action.ACL_CONNECTED

立即连接广播。如果我们收到此广播,则可以安全地假设蓝牙设备已经配对。

我建议为 Android 12 及更高版本的设备添加此权限以收听连接的广播。

android.permission.BLUETOOTH_CONNECT