gpt4 book ai didi

java - 蓝牙设备连接问题

转载 作者:行者123 更新时间:2023-12-02 06:38:38 29 4
gpt4 key购买 nike

我正在开发一个 Android 应用程序,其中我正在检查两个设备是否通过蓝牙连接

我正在使用以下代码注册广播接收器。

    IntentFilter filter1 = new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED);
IntentFilter filter2 = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED);

this.registerReceiver(mReceiver, filter1);
this.registerReceiver(mReceiver, filter2);

广播接收器看起来像这样。

    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// When discovery finds a device

if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action))
{
Log.e("bluetooth connected","bluetooth connected");
}
else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action))
{
Log.e("bluetooth not connected","bluetooth not connected");
}
}
};

然而这不起作用。不确定我哪里出错了。请帮忙!谢谢!

最佳答案

您的 list 中有蓝牙权限吗?

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

您也可以这样做,而不是注册两次接收器并使用两个过滤器

IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);

this.registerReceiver(mReceiver, filter);

关于java - 蓝牙设备连接问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19380685/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com