BluetoothDevice.ACTION_ACL_DISCONNECTED 尽管蓝牙套接字已关闭,但从未调用过
BluetoothDevice.ACTION_ACL_DISCONNECTED never called despite the bluetoothsocket is closed
我现在有两台设备通过蓝牙连接。之后,我在Client设备上断开了蓝牙连接,这个Client设备中的广播接收器可以检测到断开连接,然后切换回之前的activity。像这样:
private BroadcastReceiver myReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Message msg = Message.obtain();
String action = intent.getAction();
if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
try {
Log.i("Disconnecting3", "Disconectinggg....");
Intent intent1 = new Intent(Main3Activity.this, MainActivity.class);
startActivity(intent1);
} catch (Exception e) {
e.printStackTrace();
}
}
}
};
无论如何,在我的另一台服务器设备上,尽管蓝牙套接字已关闭,但此设备无法检测到断开连接!服务器设备中的广播接收器无法检测到断开连接。仅供参考,下面的代码将显示当客户端设备断开连接时我如何关闭服务器设备上的蓝牙套接字。
private boolean CONTINUE_READ_WRITE;
CONTINUE_READ_WRITE = true;
public void run() {
try {
while (CONTINUE_READ_WRITE) {
try {
// Read from the InputStream.
numBytes = mmInStream.read(mmBuffer);
// Send the obtained bytes to the UI activity.
Message readMsg = handleSeacrh.obtainMessage(MessageConstants.MESSAGE_READ, numBytes, -1, mmBuffer);
readMsg.sendToTarget();
} catch (IOException e) {
//nothing();
CloseConnection closeConnection = new CloseConnection();
closeConnection.start();
break;
}
}
} catch (Exception e) {
// Log.d(TAG, "Input stream was disconnected", e);
}
}
public void cancel() {
try {
Log.i("TAG", "Trying to close the socket");
CONTINUE_READ_WRITE = false;
mBluetoothSocket.close();
mmBluetoothSocket.close();
Log.i("TAG", "I thinked its still closing");
} catch (IOException e) {
Log.e("TAG", "Could not close the connect socket", e);
}
}
所以当客户端设备断开连接时,while(CONTINUE_READ_WRITE)..loop 将打破循环并启动一个新线程。像这样:
private class CloseConnection extends Thread {
public void run(){
Log.i("Running","Runinnggggg");
try {
mmInStream.close();
mmOutStream.close();
bluetoothDataTransmission.cancel();
Log.i("Interrupted","InteruppteDDDD");
} catch (IOException e) {
e.printStackTrace();
}
}
}
好的,我找到了解决方案,只需要添加这行代码
intentFilter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
我现在有两台设备通过蓝牙连接。之后,我在Client设备上断开了蓝牙连接,这个Client设备中的广播接收器可以检测到断开连接,然后切换回之前的activity。像这样:
private BroadcastReceiver myReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Message msg = Message.obtain();
String action = intent.getAction();
if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
try {
Log.i("Disconnecting3", "Disconectinggg....");
Intent intent1 = new Intent(Main3Activity.this, MainActivity.class);
startActivity(intent1);
} catch (Exception e) {
e.printStackTrace();
}
}
}
};
无论如何,在我的另一台服务器设备上,尽管蓝牙套接字已关闭,但此设备无法检测到断开连接!服务器设备中的广播接收器无法检测到断开连接。仅供参考,下面的代码将显示当客户端设备断开连接时我如何关闭服务器设备上的蓝牙套接字。
private boolean CONTINUE_READ_WRITE;
CONTINUE_READ_WRITE = true;
public void run() {
try {
while (CONTINUE_READ_WRITE) {
try {
// Read from the InputStream.
numBytes = mmInStream.read(mmBuffer);
// Send the obtained bytes to the UI activity.
Message readMsg = handleSeacrh.obtainMessage(MessageConstants.MESSAGE_READ, numBytes, -1, mmBuffer);
readMsg.sendToTarget();
} catch (IOException e) {
//nothing();
CloseConnection closeConnection = new CloseConnection();
closeConnection.start();
break;
}
}
} catch (Exception e) {
// Log.d(TAG, "Input stream was disconnected", e);
}
}
public void cancel() {
try {
Log.i("TAG", "Trying to close the socket");
CONTINUE_READ_WRITE = false;
mBluetoothSocket.close();
mmBluetoothSocket.close();
Log.i("TAG", "I thinked its still closing");
} catch (IOException e) {
Log.e("TAG", "Could not close the connect socket", e);
}
}
所以当客户端设备断开连接时,while(CONTINUE_READ_WRITE)..loop 将打破循环并启动一个新线程。像这样:
private class CloseConnection extends Thread {
public void run(){
Log.i("Running","Runinnggggg");
try {
mmInStream.close();
mmOutStream.close();
bluetoothDataTransmission.cancel();
Log.i("Interrupted","InteruppteDDDD");
} catch (IOException e) {
e.printStackTrace();
}
}
}
好的,我找到了解决方案,只需要添加这行代码
intentFilter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);