Android关闭蓝牙套接字/线程

时间:2016-06-24 00:31:08

标签: android multithreading sockets bluetooth stream

我通过蓝牙连接2台设备,当我按下一台设备上的注销按钮时,我想向另一台设备发送消息(告诉对方同时注销),关闭蓝牙连接,然后关闭当前活动(即返回我的登录活动)

问题是我一直得到这个例外,这让我觉得我没有正确关闭我的连接:

java.io.IOException: bt socket closed, read return: -1
    at android.bluetooth.BluetoothSocket.read(BluetoothSocket.java:517)
    at android.bluetooth.BluetoothInputStream.read(BluetoothInputStream.java:96)
    at java.io.InputStream.read(InputStream.java:162)
    at com.example.BTService$ConnectedThread.run(BTService.java:269)

BTService.java:269connectedThread从输入流中读取的地方

按下注销时,我基本上会销毁MainActivity,而在onDestroy()我停止了我的蓝牙服务:

this.stopService(new Intent(this, BTService.class))

应该拨打onDestroy()我的服务stopConnect()

public static void stopConnect(){
        if (connectThread != null) {
            connectThread.cancel();
            connectThread = null;
        }

        if (connectedThread != null) {
            connectedThread.cancel();
            connectedThread = null;
        }
    }

因此,只要通过单击注销销毁服务,就应调用cancel()的{​​{1}}方法:

connectedThread

我认为我是通过首先关闭输入和输出流,然后关闭套接字,然后关闭线程来正确执行此操作。我从这里得到了这个答案:Disconnect a bluetooth socket in Android

然而,一旦按下logout,我就会得到ioexception,因为它仍在尝试从输入流中读取。我还尝试将输入/输出/套接字关闭代码放在public static class ConnectedThread extends Thread { private BluetoothSocket mmSocket; private InputStream mmInStream; private OutputStream mmOutStream; public ConnectedThread(BluetoothSocket socket) { mmSocket = socket; InputStream tmpIn = null; OutputStream tmpOut = null; // Get the input and output streams, using temp objects because // member streams are final try { tmpIn = socket.getInputStream(); tmpOut = socket.getOutputStream(); } catch (IOException e) { e.printStackTrace(); } mmInStream = tmpIn; mmOutStream = tmpOut; //Tell other phone that we have connected write("connected".getBytes()); } public void run() { byte[] buffer = new byte[1024]; // buffer store for the stream int bytes; // bytes returned from read() // Keep listening to the InputStream until an exception occurs while (!this.isInterrupted()) { try { // Read from the InputStream bytes = mmInStream.read(buffer); // Send the obtained bytes to the UI activity mHandler.obtainMessage(Constants.MESSAGE_READ, bytes, -1, buffer) .sendToTarget(); } catch (IOException e) { e.printStackTrace(); break; } } } /* Call this from the main activity to send data to the remote device */ public void write(byte[] bytes) { try { mmOutStream.write(bytes); } catch (IOException e) { e.printStackTrace(); } } /* Call this from the main activity to shutdown the connection */ public void cancel() { if (mmInStream != null) { try {mmInStream.close();} catch (Exception e) {} mmInStream = null; } if (mmOutStream != null) { try {mmOutStream.close();} catch (Exception e) {} mmOutStream = null; } if (mmSocket != null) { try {mmSocket.close();} catch (Exception e) {} mmSocket = null; } this.interrupt(); } } 类之外的方法中,但这会产生同样的问题

1 个答案:

答案 0 :(得分:0)

有示例项目&#android; android bluetooth chat',看起来你从那里使用了轻微修改过的代码。但是,在该示例中,所有方法都是同步的,并且蓝牙服务对于当前状态具有可变性。也许,这就是问题 - 蓝牙套接字已经关闭,但线程仍然存在