android - BroadcastReceiver 和 ACTION_BOND_STATE_CHANGED 在 Android 9.0 中部分工作

标签 android android-bluetooth

我正在尝试通过广播接收器从与 android 的配对过程中捕获事件。看起来,BluetoothDevice.BOND_BONDING是有效的,但是 BluetoothDevice.BOND_BONDED不是。

在旧的 android 版本中这有效(尝试使用 Android 6 和 7),但是对于较新的版本(尝试使用 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);
    }
}


显现:


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

有没有其他人注意到这个问题?我错过了许可吗?在网上找不到任何相关的东西。

最佳答案

您应该尝试检索 EXTRA_BOND_STATE像那样 :

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

关于android - BroadcastReceiver 和 ACTION_BOND_STATE_CHANGED 在 Android 9.0 中部分工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57141976/

相关文章:

android - 音轨无法通过三星 S6 中的蓝牙工作

android - 外围设备模式下的 Nexus 9 不接受来自客户端的连接或不响应客户端请求?

java - XML 解析:如何将 XML 文件中的下一个元素放入字符串中? (Java/安卓)

android - 如何从内核代码重启安卓手机

bluetooth-lowenergy - 您知道如何在移动设备上编写 BLE5 功能代码吗?

Android - 开机启动服务

java - 使用ListView切换到不同的Activity

android - 系统刚启动时未收到 FCM 通知

android - 删除 Android fragment 中回收器 View 上方不需要的空白区域

android - 以编程方式连接到蓝牙