2015-03-02 132 views
4

我想将android手机连接到一个电子设备,但我想不安全地连接它。但它给了我错误“java.io.IOException:读取失败,套接字可能关闭或超时,读取RET:-1"Android不安全的蓝牙连接

这里是我的代码

包net.londatiga.android.bluetooth;

import java.io.IOException; 
import java.lang.reflect.Method; 
import java.util.UUID; 

import android.annotation.SuppressLint; 
import android.bluetooth.BluetoothDevice; 
import android.bluetooth.BluetoothSocket; 
import android.util.Log; 

public class ConnectThread extends Thread { 
    private final BluetoothSocket mmSocket; 

    private final UUID WELL_KNOWN_UUID = UUID 
      .fromString("00001101-0000-1000-8000-00805F9B34FB"); 

    public ConnectThread(BluetoothDevice device) { 
     // Use a temporary object that is later assigned to mmSocket,because 
     // mmSocket is final 
     BluetoothSocket tmp = null; 

     // Get a BluetoothSocket to connect with the given BluetoothDevice 
     try { 
      // tmp = device.createRfcommSocketToServiceRecord(WELL_KNOWN_UUID); 
      tmp = device 
        .createRfcommSocketToServiceRecord(WELL_KNOWN_UUID); 
      Object[] params = new Object[] {Integer.valueOf(1)}; 
      // This is the trick 
      Method m = device.getClass().getMethod("createInsecureRfcommSocket", 
        new Class[] { int.class }); 
      tmp = (BluetoothSocket) m.invoke(device, params); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     mmSocket = tmp; 
    } 

    @SuppressLint("NewApi") 
    public void run() { 
     // DebugLog.i(TAG, "Trying to connect..."); 
     // Cancel discovery because it will slow down the connection 
     MainActivity.mBluetoothAdapter.cancelDiscovery(); 

     try { 
      // Connect the device through the socket. This will block 
      // until it succeeds or throws an exception 
      mmSocket.connect(); 

      boolean val=mmSocket.isConnected(); 
      Log.v("val", ""+val); 
      // DebugLog.i(TAG, "Connection stablished"); 
     } catch (IOException connectException) { 
      // Unable to connect; close the socket and get out 
      // DebugLog.e(TAG, "Fail to connect!", connectException); 
      try { 
       mmSocket.close(); 
      } catch (IOException closeException) { 
       // DebugLog.e(TAG, "Fail to close connection", closeException); 
      } 
      return; 
     } 
    } 

    /** Will cancel an in-progress connection, and close the socket */ 
    public void cancel() { 
     try { 
      mmSocket.close(); 
     } catch (IOException e) { 
     } 
    } 
} 

回答

0

您必须实现另一个线程,如下图,它总是接收您的呼叫并执行操作。启动应用程序时启动此线程。

class AcceptThread extends 
Thread { 
    private final BluetoothServerSocket serverSocket; 
    private boolean flag = true; 

    public AcceptThread() { 
     BluetoothServerSocket tmp = null; 

     try { 
      tmp = bluetoothAdapter 
        .listenUsingInsecureRfcommWithServiceRecord(
          NAME_UUID, uuid); 

     } catch (IOException e) { 
     } 
     serverSocket = tmp; 
    } 

    public void run() { 
     BluetoothSocket socket = null; 
     while (true) { 
      try { 
       socket = serverSocket.accept(); 
       if (socket.isConnected()) { 
        Log.d(TAG, "run: connection successfull"); 
        Log.d(TAG, "run: " +  socket.getRemoteDevice().getName() + " " + 
          socket.getRemoteDevice().getAddress()); 
       } 

      } catch (IOException e) { 

       try { 
        socket.close(); 
       } catch (Exception eee) { 
        Log.d(TAG, "run: " + eee.getMessage()); 
       } 
       break; 
      } 
     } 
    } 

    public void cancel() { 
     try { 
      serverSocket.close(); 
     } catch (IOException e) { 
     } 
    } 
}