(非オーディオ) デバイスとの Bluetooth 接続がいつ失われたかを確実に検出できるかどうかを調べようとしています。Android SDK には、次のスニペットを使用する Bluetooth チャットの例が用意されています。
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>
短期間で両方の方法をテストしましたが、どちらも機能します。前に述べたように、電話が長時間アイドル状態の場合、これらが信頼できるかどうかはわかりません.
誰がこれに光を当てることができますか?