本文介绍了检测断开连接的蓝牙设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出是否当与(非音频)设备的蓝牙连接已丢失我可以可靠地检测。 Android SDK提供了蓝牙聊天的例子,它使用这段代码:

I'm trying to find out if I can reliably detect when the bluetooth connection with a (non-audio) device has been lost. The Android SDK provides a Bluetooth Chat example, which uses this snippet:

public void run() {
    Log.i(TAG, "BEGIN mConnectedThread");
    byte[] buffer = new byte[1024];
    int bytes;

    // Keep listening to the InputStream while connected
    while (true) {
        try {
            // Read from the InputStream
            bytes = mmInStream.read(buffer);

            // Send the obtained bytes to the UI Activity
            mHandler.obtainMessage(BluetoothChat.MESSAGE_READ, bytes, -1, buffer)
                    .sendToTarget();
        } catch (IOException e) {
            Log.e(TAG, "disconnected", e);
            connectionLost();
            break;
        }
    }
}

如何可靠当我启动线程和12小时后断开该设备不使用的应用程序(该服务在后台运行)是这个方法。该服务总是可以ofcourse被杀?

How reliable is this method when I start the thread and disconnect the device 12 hours later without using the app (The service runs in background). The service could always be killed ofcourse?

然后还有广播。请问我的应用始终接收这些节目?当它不会呢?我试过实现一个由AndroidManifest使用此:

Then there are broadcasts. Will my app always receive these broadcasts? When will it not? I've tried implementing that by using this in the AndroidManifest:

    <receiver android:name="MyReceiver" >
        <intent-filter>
            <action android:name="android.bluetooth.device.action.ACL_CONNECTED" />
            <action android:name="android.bluetooth.device.action.ACL_DISCONNECT_REQUESTED" />
            <action android:name="android.bluetooth.device.action.ACL_DISCONNECTED" />
        </intent-filter>
    </receiver>

我测试这两种方法在很短的时间内,都工作。
正如之前所说的,我不知道如果这些都是可靠的当手机闲置了很长一段时间。

I've tested both methods in a short time period, and both work.As said before, I'm not sure if these are reliable when the phone is idle for a long time.

谁能在这个流下了光?

推荐答案

蓝牙中的每个活动都有一个与之关联的超时。它从相同也可以不同。

Every activity within bluetooth has a timeout associated with it. It varies from device to device.

蓝牙服务会在后台,只要你启用运行它。 ACL [异步连接更少]分组通常应用于用于传输非语音相关的数据。 ACL只是两个蓝牙设备之间的连接通道。这可能基于不活动定时器或超时值断开连接。

Bluetooth service will run in the background as long as you have enabled it. ACL [Asynchronous Connection Less] packet is generally applied for transferring non-voice related data. ACL is just as connection channel between two bluetooth device. This may get disconnected based on inactivity timer or timeout value.

您可以阅读连接超时集时HCI的createConnection API是可以通过执行hcidump tool.http获得called.This信息://www.bluez.org/download/

you can read the connection timeout set when HCI createConnection API is called.This info can be obtained by executing hcidump tool.http://www.bluez.org/download/

这篇关于检测断开连接的蓝牙设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 07:03
查看更多