简体   繁体   English

BroadcastReceiver 和 ACTION_BOND_STATE_CHANGED 在 Android 9.0 中部分工作

[英]BroadcastReceiver and ACTION_BOND_STATE_CHANGED working partialy in Android 9.0

I am trying to catch the event from the pairing process with the android via Broadcast receiver.我正在尝试通过广播接收器从与 android 的配对过程中捕获事件。 As it seems, the BluetoothDevice.BOND_BONDING is works, but the BluetoothDevice.BOND_BONDED not.看起来, BluetoothDevice.BOND_BONDING有效,但BluetoothDevice.BOND_BONDED无效。

In the old android versions this worked (tried with Android 6 and 7), however with the newer ones (tried Android 9, several devices) this does not work.在旧的 android 版本中这有效(尝试使用 Android 6 和 7),但是对于较新的版本(尝试使用 Android 9,几个设备)这不起作用。 In order to reproduce the problem I've made a simple program:为了重现这个问题,我做了一个简单的程序:

Java file: 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);
    }
}

Manifest:显现:


<?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>

Did anybody else notice this problem?有没有其他人注意到这个问题? Am I missing a permission?我错过了许可吗? Couldn't find anything relevant on the net.在网上找不到任何相关的东西。

您应该尝试像这样检索EXTRA_BOND_STATE

val state = intent.extras?.get(BluetoothDevice.EXTRA_BOND_STATE) as Int

I have faced a similar problem.我也遇到过类似的问题。 I have noticed one thing with devices such as HeadPhone, mouse or keyboard is that when you pair such devices, it is immediately connected to our Android device.我注意到耳机、鼠标或键盘等设备的一件事是,当您配对此类设备时,它会立即连接到我们的 Android 设备。 So android send us所以android发送给我们

android.bluetooth.device.action.ACL_CONNECTED android.bluetooth.device.action.ACL_CONNECTED

connected broadcast immediately.立即连接广播。 In case we receive this broadcast, it is safe to assume that Bluetooth device is already paired.如果我们收到这个广播,可以安全地假设蓝牙设备已经配对。

I would recommend adding this permission for Android 12 and above devices to listen to the connected broadcast.我建议为 Android 12 及更高版本的设备添加此权限以收听连接的广播。

android.permission.BLUETOOTH_CONNECT android.permission.BLUETOOTH_CONNECT

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 设备绑定时,Android 不会广播 ACTION_BOND_STATE_CHANGED - Android is not broadcasting ACTION_BOND_STATE_CHANGED when the device gets bonded 如何使用SUPPLICANT_STATE_CHANGED_ACTION WiFi BroadcastReceiver-android - How to use SUPPLICANT_STATE_CHANGED_ACTION WiFi BroadcastReceiver - android Android BroadcastReceiver android.intent.action.BATTERY_CHANGED - Android BroadcastReceiver android.intent.action.BATTERY_CHANGED Android BroadcastReceiver 无法通过单击通知操作来工作 - Android BroadcastReceiver not working by clicking at notification action Android BroadCastReceiver和android.intent.action.PHONE_STATE事件 - Android BroadCastReceiver and android.intent.action.PHONE_STATE Event registerReceiver BluetoothAdapter.ACTION_STATE_CHANGED不起作用 - registerReceiver BluetoothAdapter.ACTION_STATE_CHANGED not working Wifi P2P 应用:BroadcastReceiver STATE_CHANGED_ACTION 被持续触发 - Wifi P2P app: BroadcastReceiver STATE_CHANGED_ACTION is being triggered continuously 在Android 9.0中无法接收TYPE_NOTIFICATION_STATE_CHANGED事件 - Can't receive the TYPE_NOTIFICATION_STATE_CHANGED event in Android 9.0 广播接收器上的BluetoothDevice.ACTION_NAME_CHANGED - BluetoothDevice.ACTION_NAME_CHANGED on a BroadcastReceiver 我在注册 BroadcastReceiver 后立即收到“android.net.wifi.WIFI_STATE_CHANGED” - I am receiving "android.net.wifi.WIFI_STATE_CHANGED" as soon as Registering BroadcastReceiver
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM