【问题标题】:Detect bluetooth device disconnected检测蓝牙设备断开连接
【发布时间】:2013-01-14 10:43:28
【问题描述】:

我正在尝试确定是否能够可靠地检测到与(非音频)设备的蓝牙连接何时丢失。 Android SDK 提供了一个蓝牙聊天示例,它使用了这个 sn-p:

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 小时后断开设备连接时,这种方法有多可靠(服务在后台运行)。当然,服务总是可以被杀死的?

然后是广播。我的应用会一直收到这些广播吗?什么时候不会?我已经尝试通过在 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>

我在短时间内测试了这两种方法,并且都有效。 如前所述,我不确定这些在手机长时间闲置时是否可靠。

谁能解释一下?

【问题讨论】:

    标签: android bluetooth


    【解决方案1】:

    蓝牙中的每个活动都有一个与之关联的超时。它因设备而异。

    只要您启用蓝牙服务,它就会在后台运行。 ACL [Asynchronous Connection Less] 包通常用于传输非语音相关的数据。 ACL 就像两个蓝牙设备之间的连接通道。这可能会根据不活动计时器或超时值断开连接。

    可以读取调用HCI createConnection API时设置的连接超时时间。该信息可以通过执行hcidump工具获取。http://www.bluez.org/download/

    【讨论】:

      猜你喜欢
      • 2018-01-20
      • 1970-01-01
      • 2018-06-25
      • 2015-12-18
      • 2016-01-30
      • 1970-01-01
      • 1970-01-01
      • 2015-10-19
      • 1970-01-01
      相关资源
      最近更新 更多