[英]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.