private class ConnectThread extends Thread {
private final BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;
public ConnectThread(BluetoothDevice device){
Log.i(TAG, "ConnectThread");
Log.i("wang",device + "12345");
mmDevice = device;
BluetoothSocket tmp = null;
try {
//建立于蓝牙设备通道
//tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
tmp = (BluetoothSocket) (device.getClass().getMethod("createRfcommSocket",
new Class[] {int.class})).invoke(device, 1);
// tmp = device.createRfcommSocketToServiceRecord(
// MY_UUID);
} catch (Exception e) {
e.printStackTrace();
}
mmSocket = tmp;
Log.i(TAG, "mmSocket "+(mmSocket==null)+"");
} public void run() {
// Log.i(tag, "++ConnectThread.run()++"); // mAdapter.cancelDiscovery(); try {
Log.i(TAG, "正在连接服务器");
mmSocket.connect();
Log.i(TAG, "连接服务器成功");
} catch (IOException e) {
Log.i(TAG, "连接失败信息是"+e.getMessage());
try {
mmSocket.close();
} catch (IOException e2) {
e2.printStackTrace();
}
connectionFailed();
return;
}
synchronized (BluetoothService.this) {
mConnectThread = null;
} connected(mmSocket, mmDevice);
} public void cancel() {
// Log.i(tag, "++ConnectThread.cancel()++");
try {
mmSocket.close();
} catch (IOException e) {
// Log.e(tag, "ConnectThread.cancel()\n" + e.getMessage());
}
}
}
private final BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;
public ConnectThread(BluetoothDevice device){
Log.i(TAG, "ConnectThread");
Log.i("wang",device + "12345");
mmDevice = device;
BluetoothSocket tmp = null;
try {
//建立于蓝牙设备通道
//tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
tmp = (BluetoothSocket) (device.getClass().getMethod("createRfcommSocket",
new Class[] {int.class})).invoke(device, 1);
// tmp = device.createRfcommSocketToServiceRecord(
// MY_UUID);
} catch (Exception e) {
e.printStackTrace();
}
mmSocket = tmp;
Log.i(TAG, "mmSocket "+(mmSocket==null)+"");
} public void run() {
// Log.i(tag, "++ConnectThread.run()++"); // mAdapter.cancelDiscovery(); try {
Log.i(TAG, "正在连接服务器");
mmSocket.connect();
Log.i(TAG, "连接服务器成功");
} catch (IOException e) {
Log.i(TAG, "连接失败信息是"+e.getMessage());
try {
mmSocket.close();
} catch (IOException e2) {
e2.printStackTrace();
}
connectionFailed();
return;
}
synchronized (BluetoothService.this) {
mConnectThread = null;
} connected(mmSocket, mmDevice);
} public void cancel() {
// Log.i(tag, "++ConnectThread.cancel()++");
try {
mmSocket.close();
} catch (IOException e) {
// Log.e(tag, "ConnectThread.cancel()\n" + e.getMessage());
}
}
}
,他们轮着来,头疼啊