タイトルで述べたように、私の Bluetooth ソケットは、デバッガーで実行された場合にのみ接続を確立します。にブレークポイントを設定するとconnect()
、接続に約 1 秒かかってステップ オーバーすると接続されます。リリースで実行しようとすると、ソケットからの読み取りが失敗したことを示す IOexception がすぐにスローされます。私はAndroid 5.1.1で実行しています
接続を試みるコード
BtCommThread(BluetoothDevice btDevice, BluetoothInterface btInterface) throws IOException {//let caller handle Exception with constructor
callbacks = btInterface;
device = btDevice;
socket = device.createRfcommSocketToServiceRecord(BluetoothService.uuid);
socket.connect();//attempt connection
input = new BufferedReader(new InputStreamReader(socket.getInputStream()));//get input
output = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));//get output
}
Bluetooth ソケットがデバッガでのみ機能する原因は何ですか? BtCommThread の構築とソケットの接続が最大 10 回失敗した場合に再試行するサービスを実装しました。また、タイミングの問題である可能性があると考えて、接続の前に単純なスレッドスリープを追加しようとしました。私は本当に途方に暮れています。
何が役立つか考えている人はいますか?
ハードウェア情報
Android フォン: Google Nexus 5
Linux システム: Debian armhf を実行する Udoo MCU