[英]How to detect whenever my android device gets connected/disconnected from paired bluetooth device
[英]Detect bluetooth device disconnected
我试图找出是否可以可靠地检测到与(非音频)设备的蓝牙连接丢失的时间。 Android SDK提供了一个蓝牙聊天示例,该示例使用以下代码段:
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>
我已经在很短的时间内测试了这两种方法,并且都可以使用。 如前所述,当电话长时间闲置时,我不确定这些是否可靠。
谁能阐明这一点?
蓝牙中的每个活动都有一个与之关联的超时。 随设备而异。
只要启用了蓝牙服务,它就会在后台运行。 通常将ACL [异步连接较少]数据包用于传输与语音无关的数据。 ACL就像两个蓝牙设备之间的连接通道一样。 这可能会由于不活动计时器或超时值而断开连接。
您可以在调用HCI createConnection API时读取连接超时设置。可以通过执行hcidump工具获得此信息。http://www.bluez.org/download/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.