第一种方法:
mbluetoothsocket = mbluetoothdevice.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
mbluetoothsocket.connect();
第二种方法:
{
Method m;
try {
m = mbluetoothdevice.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
try {
mbluetoothsocket = (BluetoothSocket) m.invoke(mbluetoothdevice, 1);
//_bluetooth.cancelDiscovery();
try {
mbluetoothsocket.connect();
} catch (IOException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
Message msg = new Message();
msg.obj = "连接服务端异常!断开连接重新试一试。";
msg.what = BLUETOOTH_SHOW_MSG;
mHandler.sendMessage(msg);
}
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
Message msg = new Message();
msg.obj = "创建socket失败IllegalArgumentException";
msg.what = BLUETOOTH_SHOW_MSG;
mHandler.sendMessage(msg);
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
Message msg = new Message();
msg.obj = "创建socket失败IllegalAccessException";
msg.what = BLUETOOTH_SHOW_MSG;
mHandler.sendMessage(msg);
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
Message msg = new Message();
msg.obj = "创建socket失败InvocationTargetException";
msg.what = BLUETOOTH_SHOW_MSG;
mHandler.sendMessage(msg);
}
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
第一种方法,会出现连接异常;第二种方法.连接成功的概率比较大。