为什么我30秒的发送下数据给外部蓝牙,程序调试到 mmOutStream.write(data),就出现socket.close();
private class GetDataThread extends Thread {
private final BluetoothSocket iSocket;
private InputStream mmInStream;
public GetDataThread(BluetoothSocket socket) {
iSocket = socket;
} public void run() {
try {
mmInStream = iSocket.getInputStream();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
return;
}
if(!mBTConnect)
return;
{
try {
byte[] data = {1,2,3,4,5,6}
mmOutStream.write(data);
} catch (IOException e) {
Log.e("print", "Exception during write", e);
}
}
Log.e(TAG, "BEGIN mConnectedThread");
int Err = ConstDefine.enumNetPromptMsg.ErrOK;
final int len = 9;
byte b[] = new byte[len];
Calendar now = Calendar.getInstance();
mBeginMillsecond = now.getTimeInMillis();
while (readflag) {
if(!mBTConnect)
{
return;
}
try {
if(mmInStream.read(b) != -1){
}
}catch (IOException e) {
Log.e(TAG, "disconnected", e);
cancel();
break;
}
}
}
public void cancel() {
try {
if(mmOutStream != null){mmOutStream.close();}
if(mmInStream != null){mmInStream.close();}
// if(mmSocket != null){mmSocket.close();}
} catch (IOException e) {
//Log.e(TAG, "close() of connect socket failed", e);
}
}
}
private class GetDataThread extends Thread {
private final BluetoothSocket iSocket;
private InputStream mmInStream;
public GetDataThread(BluetoothSocket socket) {
iSocket = socket;
} public void run() {
try {
mmInStream = iSocket.getInputStream();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
return;
}
if(!mBTConnect)
return;
{
try {
byte[] data = {1,2,3,4,5,6}
mmOutStream.write(data);
} catch (IOException e) {
Log.e("print", "Exception during write", e);
}
}
Log.e(TAG, "BEGIN mConnectedThread");
int Err = ConstDefine.enumNetPromptMsg.ErrOK;
final int len = 9;
byte b[] = new byte[len];
Calendar now = Calendar.getInstance();
mBeginMillsecond = now.getTimeInMillis();
while (readflag) {
if(!mBTConnect)
{
return;
}
try {
if(mmInStream.read(b) != -1){
}
}catch (IOException e) {
Log.e(TAG, "disconnected", e);
cancel();
break;
}
}
}
public void cancel() {
try {
if(mmOutStream != null){mmOutStream.close();}
if(mmInStream != null){mmInStream.close();}
// if(mmSocket != null){mmSocket.close();}
} catch (IOException e) {
//Log.e(TAG, "close() of connect socket failed", e);
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货