2016-01-13 122 views
0

是我的代码:Android的蓝牙插座.connect()错误

Thread connectThread = new Thread(new Runnable() { 

      @Override 
      public void run() { 
       try { 
        boolean gotuuid = btDevices.getItem(position) 
          .fetchUuidsWithSdp(); 
        if (gotuuid){ 
         UUID uuid = btDevices.getItem(position).getUuids()[0] 
           .getUuid(); 
         mbtSocket = btDevices.getItem(position) 
           .createRfcommSocketToServiceRecord(uuid); 

         mbtSocket.connect(); 
        } else { 
         Log.e("ID22", "There is no uuid"); 
        } 


       } catch (IOException ex) { 
        runOnUiThread(socketErrorRunnable); 
        try { 
         mbtSocket.close(); 
        } catch (IOException e) { 
// e.printStackTrace(); 
        } 
        mbtSocket = null; 
        return; 
       } finally { 
        runOnUiThread(new Runnable() { 

         @Override 
         public void run() { 
          finish(); 

         } 
        }); 
       } 
      } 
     }); 

     connectThread.start(); 
    } 

当我尝试使用mbtSocket.connect();连接到蓝牙soccket停止并抛出socketErrorRunnable例外。你有什么想法如何解决这个问题?我搜索了一下,但没有为我工作。

+0

您可以张贴在此异常详细的错误日志? – Mackovich

+0

java.io.IOException:读取失败,套接字可能已关闭或超时,请阅读ret:-1 其未打印任何文件,只要进入catch语句 – Tito

+0

您尝试连接的设备不在范围内或关闭或无法访问。在最后一种情况下,主要是因为在RFCOMM连接中使用的UUID不正确,或者与目标设备的蓝牙内部的注册服务不对应。顺便说一句,目标设备是什么? – Mackovich

回答

0

在你有困难的情况下在这里是从官方Android Bluetooth Chat例如一些代码:

服务器端:

public class BluetoothServer { 
    private final static String TAG = "BluetoothServer"; 
    private final BluetoothServerSocket serverSocket; 

    // I would recommand defining a secure connexion so forget the unsecore UUID 
    private static final UUID MY_UUID_SECURE = 
      UUID.fromString("fa87c0d0-afac-11de-8a39-0800200c9a66"); // choose another UUID go to a site that generates a random UUI ;-) 
    private static final UUID MY_UUID_INSECURE = 
      UUID.fromString("8ce255c0-200a-11e0-ac64-0800200c9a66"); // choose another UUID go to a site that generates a random UUI ;-) 
    // BUT THIS WILL BE THE UUID YOU NEED TO CONNECT TO IN YOUR CLIENT APP -> createRfcommSocketToServiceRecord(UUID.fromString("0800200c9a66")); 
    // Name for the SDP record when creating server socket 
    private static final String NAME_SECURE = "BluetoothChatSecure"; 
    private static final String NAME_INSECURE = "BluetoothChatInsecure"; 

    public BluetoothServer(BluetoothAdapter adapter, boolean secure) { 
     try { 
      if (secure) { 
       this.serverSocket = adapter.listenUsingRfcommWithServiceRecord(NAME_SECURE, 
         MY_UUID_SECURE); 
      } else { 
       this.serverSocket = adapter.listenUsingInsecureRfcommWithServiceRecord(
         NAME_INSECURE, MY_UUID_INSECURE); 
      } 
     } catch (IOException e) { 
      Log.e(TAG, "Socket Type: " + (secure ? "secure" : "insecure") + "listen() failed", e); 
     } 
    } 

    public void asyncStartServer() { 
     new Thread(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        final BluetoothSocket socket = serverSocket.accept(); // blocks/wait until your phone connects 
        // at this point, your phone is connected 
        final InputStream is = socket.getInputStream(); 
        // and now start reading 
       } catch (IOException ioe) { 
        Log.getStackTraceString(ioe); 
       } 
      } 
     }).start(); 
    } 
}