做个循环,当错误时重新执行过,直到无错误后退出循环,这个应该是UUID识别的问题。

解决方案 »

  1.   

    楼上的我试了一下,还是不太行……我在错误信息中发现如下行报错 @Override
        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 {
                     怎么解决呢 各位大牛们!
      

  2.   

    我换了种写法就参考网上说的
    发送端
    //建立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;结果就好了
      

  3.   

    这句话中( BluetoothServerSocket) listenMethod.invoke(mBluetoothAdapter, new Object[]{ 29});
    最后29代表啥呀??没弄懂求大神指导一下