有没有办法查明非 Android 蓝牙设备是否已连接到我的 Android 设备?

发布于 2024-12-19 06:10:41 字数 276 浏览 0 评论 0原文

我有一部 Android 手机和一个非 Android 设备,例如蓝牙拨号器。

据我所知,

使用像 ACL_CONNECTED 这样的侦听器,如果蓝牙设备在应用程序运行时连接,我们可以捕获蓝牙设备的连接。

但是,有什么方法可以在应用程序启动之前知道设备是否已连接到手机,并且我的应用程序可以通过它进行连接?

换句话说,

如果在启动应用程序之前设备已连接,我会收到 ACL_CONNECTED 或其他接收器吗?

谢谢。

I have an android phone and a non-android device like a bluetooth dialer with me.

As Far As I Know

With listeners like ACL_CONNECTED we can catch connecting of a bluetooth device if it's connecting while application is running.

But is there any way to know if a device is connected to phone before the app is started and my app can connect through it?

In Other Words

will I recieve ACL_CONNECTED or other recievers if the device is already connected before I start my application?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

好倦 2024-12-26 06:10:41

使用 BroadcastReceiver 在连接设备时接收广播,并使用 SharedPreference 存储布尔值等值。

这可能是接收器的代码:

@Override
public void onReceive(Context context, Intent intent) {

    if (intent.getAction().contentEquals("android.bluetooth.device.action.ACL_CONNECTED")) {
         //DO SOMETHING, like using a SharedPreference
    } else {
        if (intent.getAction().contentEquals("android.bluetooth.device.action.ACL_DISCONNECT")) {
         //DO SOMETHING
        }
    }

您还应该在设备连接后启动一项服务,该服务监视蓝牙是否关闭,然后将 SharedPreference 设置为 false,因为您不会收到 <代码>ACL_DISCONNECT 广播。

这可能看起来像这样:

public class BTService extends Service {

@Override
public void onStart(Intent intent, int startId) {
    super.onStart(intent, startId);
        IntentFilter filter2 = new IntentFilter();
        filter2.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
     registerReceiver(rec_bt_change, filter2);
}




private final BroadcastReceiver rec_bt_change = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1) == BluetoothAdapter.STATE_OFF) {
            //SettingsClass.log_me(tag, "BT turned off, stopping");
            //DO SOMETHING
            stopSelf();
        }
    }
};
}

Use a BroadcastReceiver to receive the Broadcast when a device is connected and a SharedPreference to store a value like a Boolean.

This could be a code for the Receiver:

@Override
public void onReceive(Context context, Intent intent) {

    if (intent.getAction().contentEquals("android.bluetooth.device.action.ACL_CONNECTED")) {
         //DO SOMETHING, like using a SharedPreference
    } else {
        if (intent.getAction().contentEquals("android.bluetooth.device.action.ACL_DISCONNECT")) {
         //DO SOMETHING
        }
    }

You should also start a service once the device is connected, that watches if Bluetooth is turned off and then sets the SharedPreference to false, as you would not receive a ACL_DISCONNECT Broadcast.

That could look like this:

public class BTService extends Service {

@Override
public void onStart(Intent intent, int startId) {
    super.onStart(intent, startId);
        IntentFilter filter2 = new IntentFilter();
        filter2.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
     registerReceiver(rec_bt_change, filter2);
}




private final BroadcastReceiver rec_bt_change = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1) == BluetoothAdapter.STATE_OFF) {
            //SettingsClass.log_me(tag, "BT turned off, stopping");
            //DO SOMETHING
            stopSelf();
        }
    }
};
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文