程序概述:server搜索client,
结束搜索,
对搜到的设备进行connect,
client accept到后向server发送信息,
server收到,
处理,
发回结果。
问题:client发送是没有异常;
server接收时异常(connection reset by peer);
server发回结果时异常(socket closed);
client接收结果时异常(socket closed)Android版本:2.1 update1相关代码:server连接client是用到的两个类 /**
* This thread runs while connecting with other devices.
* It runs until each device is processed with one ConnectedThread.
* Then go to start().
*/
private class ConnectThread extends Thread{
@Override
public void run() {
if (D) Log.d(TAG, "BEGIN ConnectThread");
setName("Connect Thread");
setStatus(ShopTransmit.STATUS_TRANSMIT);
for(BluetoothDevice device:possibleDevices){
// Cancel any thread that connected to another device
if (connected != null) { connected.cancel(); connected = null; }
// Try to connect to a certain device
connected = new ConnectedThread(device);
connected.start();
// With time out 5 seconds
try {
connected.join(5000);
} catch (InterruptedException e) {
if (D) Log.d(TAG, "connected thread interrupted in timeout", e);
}
if (connected.isAlive()) {
if (D) Log.d(TAG, "connected thread timeout");
try {
connected.interrupt();
} catch(Exception e) {
Log.d(TAG, "connected interrupt failed", e);
}
}
}
// Restart to search
transmitStart();
} public void cancel(){
// Clear the list of possible devices for next search session
possibleDevices.clear();
}
}
/**
* This thread runs while connecting with one devices.
* It runs until any exception happens, process finished
* or time out from ConnectThread.
*/
private class ConnectedThread extends Thread{
private final BluetoothSocket socket;
private InputStream in;
private OutputStream out;
public ConnectedThread(BluetoothDevice device){
BluetoothSocket tmpSocket = null;
try {
tmpSocket = device.createRfcommSocketToServiceRecord(uuid);
// Make a connection to the BluetoothSocket, if socket exists
tmpSocket.connect();
} catch (IOException e) {
if (D) Log.d(TAG, "UUID failed ", e);
} socket = tmpSocket;
in = null;
out = null;
}
@SuppressWarnings("unchecked")
@Override
public void run() {
if (D) Log.d(TAG, "connected");
setName("Connected Thread");
// Always cancel discovery because it will slow down a connection
bluetooth.cancelDiscovery(); //try {
// Make a connection to the BluetoothSocket, if socket exists
//socket.connect();
// Get the BluetoothSocket object input stream, if socket existed
if (socket != null) {
try {
in = socket.getInputStream();
} catch (IOException e) {
Log.d(TAG, "temp streams not created ", e);
}
}
// Prepare parameters for receive from BluetoothSocket
String received = "";
int bytesRead = -1;
byte[] bufferRead = new byte[1024];
// Read string from BluetoothSocket, if InputStream is created
try {
if (in != null) {
while (true) {
bytesRead = in.read(bufferRead);
if (bytesRead != -1) {
while((bytesRead == 1024) && (bufferRead[bufferRead.length-1] != '@')) {
received.concat(new String(bufferRead, 0, bytesRead));
bytesRead = in.read(bufferRead);
}
received.concat(new String(bufferRead, 0, bytesRead-1));
}
}
}
} catch(IOException e) {
Log.d(TAG, "read failed", e);
}
// Convert information received into object of ArrayList
ArrayList<String> tmpList = infoIn(received);
// Close the BluetoothSocket object input stream
if (in != null) {
try {
in.close();
} catch (IOException e) {
Log.d(TAG, "object input stream close failed", e);
}
}
// Get the BluetoothSocket output stream, if socket existed
if (socket != null) {
try {
out = socket.getOutputStream();
} catch (IOException e) {
Log.d(TAG, "temp streams not created ", e);
}
}
//Prepare parameters for transmit to BluetoothSocket
byte[] bufferWrite = (infoOut(process.returnChoice(tmpList)) + " ").getBytes();
bufferWrite[bufferWrite.length-1] = '@';
// Process list received and send result back to client,
// if the receive succeeds AND ObjectOutputStream is created
try {
if (tmpList != null && out != null) {
out.write(bufferWrite);
}
} catch(IOException e) {
Log.d(TAG, "write failed", e);
}
//Close the BluetoothSocket object output stream
if (out != null) {
try {
out.close();
} catch (IOException e) {
Log.d(TAG, "object output stream close failed", e);
}
}
//} catch (IOException e) {
// Log.d(TAG, "connected failed ", e);
//}
}
public void cancel(){
try {
socket.close();
} catch (IOException e) {
Log.e(TAG, "close() of connect socket failed ", e);
}
}
}
结束搜索,
对搜到的设备进行connect,
client accept到后向server发送信息,
server收到,
处理,
发回结果。
问题:client发送是没有异常;
server接收时异常(connection reset by peer);
server发回结果时异常(socket closed);
client接收结果时异常(socket closed)Android版本:2.1 update1相关代码:server连接client是用到的两个类 /**
* This thread runs while connecting with other devices.
* It runs until each device is processed with one ConnectedThread.
* Then go to start().
*/
private class ConnectThread extends Thread{
@Override
public void run() {
if (D) Log.d(TAG, "BEGIN ConnectThread");
setName("Connect Thread");
setStatus(ShopTransmit.STATUS_TRANSMIT);
for(BluetoothDevice device:possibleDevices){
// Cancel any thread that connected to another device
if (connected != null) { connected.cancel(); connected = null; }
// Try to connect to a certain device
connected = new ConnectedThread(device);
connected.start();
// With time out 5 seconds
try {
connected.join(5000);
} catch (InterruptedException e) {
if (D) Log.d(TAG, "connected thread interrupted in timeout", e);
}
if (connected.isAlive()) {
if (D) Log.d(TAG, "connected thread timeout");
try {
connected.interrupt();
} catch(Exception e) {
Log.d(TAG, "connected interrupt failed", e);
}
}
}
// Restart to search
transmitStart();
} public void cancel(){
// Clear the list of possible devices for next search session
possibleDevices.clear();
}
}
/**
* This thread runs while connecting with one devices.
* It runs until any exception happens, process finished
* or time out from ConnectThread.
*/
private class ConnectedThread extends Thread{
private final BluetoothSocket socket;
private InputStream in;
private OutputStream out;
public ConnectedThread(BluetoothDevice device){
BluetoothSocket tmpSocket = null;
try {
tmpSocket = device.createRfcommSocketToServiceRecord(uuid);
// Make a connection to the BluetoothSocket, if socket exists
tmpSocket.connect();
} catch (IOException e) {
if (D) Log.d(TAG, "UUID failed ", e);
} socket = tmpSocket;
in = null;
out = null;
}
@SuppressWarnings("unchecked")
@Override
public void run() {
if (D) Log.d(TAG, "connected");
setName("Connected Thread");
// Always cancel discovery because it will slow down a connection
bluetooth.cancelDiscovery(); //try {
// Make a connection to the BluetoothSocket, if socket exists
//socket.connect();
// Get the BluetoothSocket object input stream, if socket existed
if (socket != null) {
try {
in = socket.getInputStream();
} catch (IOException e) {
Log.d(TAG, "temp streams not created ", e);
}
}
// Prepare parameters for receive from BluetoothSocket
String received = "";
int bytesRead = -1;
byte[] bufferRead = new byte[1024];
// Read string from BluetoothSocket, if InputStream is created
try {
if (in != null) {
while (true) {
bytesRead = in.read(bufferRead);
if (bytesRead != -1) {
while((bytesRead == 1024) && (bufferRead[bufferRead.length-1] != '@')) {
received.concat(new String(bufferRead, 0, bytesRead));
bytesRead = in.read(bufferRead);
}
received.concat(new String(bufferRead, 0, bytesRead-1));
}
}
}
} catch(IOException e) {
Log.d(TAG, "read failed", e);
}
// Convert information received into object of ArrayList
ArrayList<String> tmpList = infoIn(received);
// Close the BluetoothSocket object input stream
if (in != null) {
try {
in.close();
} catch (IOException e) {
Log.d(TAG, "object input stream close failed", e);
}
}
// Get the BluetoothSocket output stream, if socket existed
if (socket != null) {
try {
out = socket.getOutputStream();
} catch (IOException e) {
Log.d(TAG, "temp streams not created ", e);
}
}
//Prepare parameters for transmit to BluetoothSocket
byte[] bufferWrite = (infoOut(process.returnChoice(tmpList)) + " ").getBytes();
bufferWrite[bufferWrite.length-1] = '@';
// Process list received and send result back to client,
// if the receive succeeds AND ObjectOutputStream is created
try {
if (tmpList != null && out != null) {
out.write(bufferWrite);
}
} catch(IOException e) {
Log.d(TAG, "write failed", e);
}
//Close the BluetoothSocket object output stream
if (out != null) {
try {
out.close();
} catch (IOException e) {
Log.d(TAG, "object output stream close failed", e);
}
}
//} catch (IOException e) {
// Log.d(TAG, "connected failed ", e);
//}
}
public void cancel(){
try {
socket.close();
} catch (IOException e) {
Log.e(TAG, "close() of connect socket failed ", e);
}
}
}
解决方案 »
- 打开浏览器之后,在退出浏览器的时候弹出AlertDialog,怎么显示?
- Eclipse调试的时候Source not found,什么情况
- 如何设置当某一View显示在最前面时其他可见控件不可触发?
- 小弟求教一个!
- 两个activity的问题??????????
- 【求助】get方法怎么加参数?如何接收返回的参数?
- anjian.setOnClickListener(new anjianlistener());哪里错了
- 关于Android 机顶盒设置有线网络静态IP地址问题
- 40岁的老人,android终于入门了。发帖纪念一下。
- 安卓图片显示花屏
- 版本问题android
- [求教]activity跟dialog的关系
* This thread runs while one socket accepted from shop server.
* It runs until any exception happens or one information query finished
* or timeout.
*/
private class ConnectTimerThread extends Thread{
private final BluetoothSocket socket;
// Get the BluetoothSocket object input and object output streams
public ConnectTimerThread(BluetoothSocket socket){
this.socket = socket;
}
@Override
public void run() {
if (D) Log.d(TAG, "BEGIN Timeout Thread");
setName("Timeout Thread");
// Create and start the thread which really exchange information
// through BluetoothSocket
ConnectedThread work = new ConnectedThread(socket);
work.start();
// Join work thread within current thread for 2 seconds
try {
work.join(2000);
} catch (InterruptedException e) {
Log.d(TAG, "work thread interrupted in timeout", e);
}
// Interrupt work thread if still alive after timeout
if (work.isAlive()) {
if (D) Log.d(TAG, "work thread timeout"); work.cancel();
try {
work.interrupt();
} catch(Exception e) {
Log.d(TAG, "work thread interrupt failed", e);
}
}
cancel();
}
public void cancel(){
// Close socket before abandoned
try {
socket.close();
} catch (IOException e) {
Log.d(TAG, "socket closed failed ", e);
}
}
}
/**
*
* @author Administrator
*
*/
private class ConnectedThread extends Thread{
private BluetoothSocket socket;
private InputStream in;
private OutputStream out;
public ConnectedThread(BluetoothSocket socket){
this.socket = socket;
in = null;
out = null;
}
@Override
public void run() {
if (D) Log.d(TAG, "BEGIN Connected Thread");
setName("Connected Thread");
// Always cancel discovery because it will slow down a connection
bluetooth.cancelDiscovery(); // Get list of products checked by user
ArrayList<String> tmpList = getProductsChecked(); // Get the BluetoothSocket object output stream, if socket existed
if (socket != null) {
try {
out = socket.getOutputStream();
} catch (IOException e) {
Log.d(TAG, "temp streams not created ", e);
}
}
// Prepare parameters for transmit to BluetoothSocket
byte[] bufferWrite = (infoOut(getProductsChecked()) + " ").getBytes();
bufferWrite[bufferWrite.length-1] = '@';
// Transmit shopping list to shop server,
// if the receive succeeds AND ObjectOutputStream is created
try {
if (tmpList != null && out != null) {
out.write(bufferWrite);
}
} catch(IOException e) {
Log.d(TAG, "write failed", e);
}
//Close the BluetoothSocket object output stream
if (out != null) {
try {
out.close();
} catch (IOException e) {
Log.d(TAG, "object output stream close failed", e);
}
}
// Get the BluetoothSocket object input stream, if socket existed
if (socket != null) {
try {
in = socket.getInputStream();
} catch (IOException e) {
Log.d(TAG, "temp streams not created ", e);
}
}
// Prepare parameters for receive from BluetoothSocket
String received = "";
int bytesRead = -1;
byte[] bufferRead = new byte[1024];
// Read object from BluetoothSocket, if ObjectInputStream is created
try {
if (in != null) {
while (true) {
bytesRead = in.read(bufferRead);
if (bytesRead != -1) {
while((bytesRead == 1024) && (bufferRead[bufferRead.length-1] != '@')) {
received.concat(new String(bufferRead, 0, bytesRead));
bytesRead = in.read(bufferRead);
}
received.concat(new String(bufferRead, 0, bytesRead-1));
}
}
}
} catch(IOException e) {
Log.d(TAG, "read failed", e);
}
// Send update type message back to CusApp handler, if received
if (received.length() != 0) {
Message message = new Message();
message.what = CusApp.UPDATE_TYPE;
message.obj = infoIn(received);
mHandler.sendMessage(message);
}
// Close the BluetoothSocket object input stream
if (in != null) {
try {
in.close();
} catch (IOException e) {
Log.d(TAG, "object input stream close failed", e);
}
}
}
public void cancel(){
// Close socket before abandoned
try {
socket.close();
} catch (IOException e) {
Log.d(TAG, "socket closed failed ", e);
}
}
}