【发布时间】: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>
我在短时间内测试了这两种方法,并且都有效。 如前所述,我不确定这些在手机长时间闲置时是否可靠。
谁能解释一下?
【问题讨论】: