Android 蓝牙:已连接?

发布于 2024-09-14 22:47:27 字数 181 浏览 3 评论 0原文

有没有办法确定您是否已连接到蓝牙设备?

我的应用程序连接、发送/接收都很好。但是,我需要一种方法来查看我是否仍然处于连接状态,比如说……如果我走出范围并走回范围。

我注意到蓝牙套接字中没有 isConnected 函数,就像 TCP 中的函数一样...有没有办法查看您是否已连接,或者是否与您应该连接的设备进行通信?

Is there a way to figure out if you are connected to a Bluetooth Device?

I have my app connecting, sending/receiving just fine. However I need a way to see if I'm still connected say.. if I walk out of range and walk back into range.

I noticed there isnt an isConnected function in the Bluetooth Socket, like there is in the TCP stuff... is there a way to see if you are connected, or communicating to the device you're supposed to be connected with?

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

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

发布评论

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

评论(3

魄砕の薆 2024-09-21 22:47:27

我能够解决这个问题的唯一方法是每秒发送一次“心跳”消息。如果没有通过,那么我认为蓝牙已断开连接。

The only way I was able to get around this was to send a "heartbeat" message once per second. If it doesn't go through, then I assume Bluetooth is disconnected.

夏雨凉 2024-09-21 22:47:27

发送尽可能少的数据,看看是否得到响应。如果你不这样做,那么你就没有联系。

Send the smallest amount of data that you can and see if you get a response. If you don't, well, you don't have a connection.

ぺ禁宫浮华殁 2024-09-21 22:47:27

以下广播接收器值应该告诉您何时任何 BT 设备断开连接:

intentFilter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED); // API 5
intentFilter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED); // API 5

如果您对特定设备感兴趣,您可能应该实现一个 BluetoothProfile.ServiceListener 代理侦听器:

private class MyBluetoothHeadsetListener //
                implements BluetoothProfile.ServiceListener
{
    @Override
    public void onServiceDisconnected(int profile)
    {
    }

    @Override
    public void onServiceConnected(int profile, BluetoothProfile proxy)
    {
        if (profile == BluetoothProfile.A2DP)
        {
            BluetoothA2dp bluetoothA2dp = (BluetoothA2dp) proxy;
            mDevicesA2dp = bluetoothA2dp.getConnectedDevices();
            for (BluetoothDevice deviceA2dp : mDevicesA2dp)
            {
                boolean isA2dpPlaying = bluetoothA2dp.isA2dpPlaying(deviceA2dp);
            }
            return;
        }

        if (profile == BluetoothProfile.HEADSET)
        {
            BluetoothHeadset bluetoothHeadset = (BluetoothHeadset) proxy;
            mDevicesNonA2dp = bluetoothHeadset.getConnectedDevices();
            if (mDevicesNonA2dp.size() > 0)
            {
                for (BluetoothDevice deviceNonA2dp : mDevicesNonA2dp)
                {
                    BluetoothClass bluetoothClass = deviceNonA2dp.getBluetoothClass();
                    String bluetoothDeviceClass = bluetoothClassToString(bluetoothClass);
                    boolean isAudioConnected = bluetoothHeadset.isAudioConnected(deviceNonA2dp);
                }
            }
            return;
        }
    }
}

...

private MyBluetoothHeadsetListener mProfileListener = new MyBluetoothHeadsetListener();

...

BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
adapter.getProfileProxy(mApp, mProfileListener, BluetoothProfile.HEADSET);
adapter.getProfileProxy(mApp, mProfileListener, BluetoothProfile.A2DP);

The following broadcast receiver values should tell you when any BT device is disconnected:

intentFilter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED); // API 5
intentFilter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED); // API 5

You should probably implement a BluetoothProfile.ServiceListener proxy listener if you are interested in a specific device:

private class MyBluetoothHeadsetListener //
                implements BluetoothProfile.ServiceListener
{
    @Override
    public void onServiceDisconnected(int profile)
    {
    }

    @Override
    public void onServiceConnected(int profile, BluetoothProfile proxy)
    {
        if (profile == BluetoothProfile.A2DP)
        {
            BluetoothA2dp bluetoothA2dp = (BluetoothA2dp) proxy;
            mDevicesA2dp = bluetoothA2dp.getConnectedDevices();
            for (BluetoothDevice deviceA2dp : mDevicesA2dp)
            {
                boolean isA2dpPlaying = bluetoothA2dp.isA2dpPlaying(deviceA2dp);
            }
            return;
        }

        if (profile == BluetoothProfile.HEADSET)
        {
            BluetoothHeadset bluetoothHeadset = (BluetoothHeadset) proxy;
            mDevicesNonA2dp = bluetoothHeadset.getConnectedDevices();
            if (mDevicesNonA2dp.size() > 0)
            {
                for (BluetoothDevice deviceNonA2dp : mDevicesNonA2dp)
                {
                    BluetoothClass bluetoothClass = deviceNonA2dp.getBluetoothClass();
                    String bluetoothDeviceClass = bluetoothClassToString(bluetoothClass);
                    boolean isAudioConnected = bluetoothHeadset.isAudioConnected(deviceNonA2dp);
                }
            }
            return;
        }
    }
}

...

private MyBluetoothHeadsetListener mProfileListener = new MyBluetoothHeadsetListener();

...

BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
adapter.getProfileProxy(mApp, mProfileListener, BluetoothProfile.HEADSET);
adapter.getProfileProxy(mApp, mProfileListener, BluetoothProfile.A2DP);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文