// Keep listening until exception occurs or a socket is returned while (true) { try { System.out.println(mmServerSocket); socket = mmServerSocket.accept(); } catch (IOException e) { e.printStackTrace(); finish(); break; } // If a connection was accepted if (socket != null) { try { 怎么解决呢 各位大牛们!
public void onStart() {
super.onStart();
//启动服务器的线程
if (mAcceptThread == null) {
//点击打印出的错误信息,光标会移动到这里
mAcceptThread = new AcceptThread();
mAcceptThread.start();
}
} public Handler getHandler(){
return this.handler;
} private class AcceptThread extends Thread {
private final BluetoothServerSocket mmServerSocket;
private InputStream mmInStream;
public AcceptThread() {
final String SPP_UUID = "00001101-0000-1000-8000-00805F9B34FB";
UUID uuid = UUID.fromString(SPP_UUID);
BluetoothServerSocket tmp = null;
try {
tmp = mBluetoothAdapter.listenUsingRfcommWithServiceRecord(Name,uuid);
} catch (IOException e) {
//这里报出异常
System.out.println("Rfcomm出错"); e.printStackTrace();
}
mmServerSocket = tmp;
} public void run() {
BluetoothSocket socket = null;
// Keep listening until exception occurs or a socket is returned
while (true) {
try {
System.out.println(mmServerSocket);
socket = mmServerSocket.accept();
} catch (IOException e) {
e.printStackTrace();
finish();
break;
}
// If a connection was accepted
if (socket != null) {
try {
怎么解决呢 各位大牛们!
发送端
//建立socket连接
BluetoothSocket tmp = null;
mdevice = device;
try {
Method m = mdevice.getClass().getMethod("createRfcommSocket", new Class[]{int.class});
try{ tmp = (BluetoothSocket)m.invoke(mdevice, Integer.valueOf(29)); }catch(Exception e){
System.out.println("Exception111");
e.printStackTrace();
} }catch(NoSuchMethodException e){
System.out.println("NoSuchMethodException");
e.printStackTrace();
}
msocket = tmp;接收端
Method listenMethod = null;
try {
listenMethod = mBluetoothAdapter.getClass().getMethod("listenUsingRfcommOn", new Class[]{int.class});
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
tmp = ( BluetoothServerSocket) listenMethod.invoke(mBluetoothAdapter, new Object[]{ 29});
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mmServerSocket = tmp;结果就好了
最后29代表啥呀??没弄懂求大神指导一下