2016-07-12 4 views
0

Android 애플리케이션에서 블루투스 장치에 연결하려고하는데 문제가 있습니다. 첫 시도에서 블루투스 장치에 연결할 수없는 것 같습니다. 첫 번째 시도에서 Android 블루투스 연결을 설정할 수 없습니다.

i를 BluetoothConnectThread에 다음 코드가 있습니다

public class BluetoothConnectThread extends Thread { 
    private BluetoothSocket mmSocket; 
    private BluetoothDevice mmDevice; 
    private Context context; 
    private BluetoothManager manager; 

    public BluetoothConnectThread(BluetoothDevice mmDevice, UUID uuid, Context context, BluetoothManager manager) { 
     this.context = context; 
     this.manager = manager; 
     this.mmDevice = mmDevice; 
     this.uuid = uuid; 
    } 

    public void run() { 
     try { 
      System.out.println("Try to connect"); 
      mmSocket = (BluetoothSocket) mmDevice.getClass().getMethod("createRfcommSocket", new Class[]{int.class}).invoke(mmDevice, Integer.valueOf(1)); 
      mmSocket.connect(); 
     } catch (Exception connectException) { 
      connectException.printStackTrace(); 
      try { 
       mmSocket.close(); 
       System.out.println("Couldn't establish Bluetooth connection! (1)"); 
      } catch (IOException closeException) { 
       closeException.printStackTrace(); 
       System.out.println("Couldn't establish Bluetooth connection! (2)"); 
      } 

      try { 
       System.out.println("Try to connect again"); 
       mmSocket = (BluetoothSocket) mmDevice.getClass().getMethod("createRfcommSocket", new Class[]{int.class}).invoke(mmDevice, Integer.valueOf(1)); 
       mmSocket.connect(); 
      } catch (Exception connectException2) { 
       connectException.printStackTrace(); 
       try { 
        mmSocket.close(); 
        System.out.println("Couldn't establish Bluetooth connection! (3)"); 
       } catch (IOException closeException) { 
        closeException.printStackTrace(); 
        System.out.println("Couldn't establish Bluetooth connection! (4)"); 
       } 
      } 
     } 

     if(mmSocket.isConnected()) { 
      if(mmSocket.isConnected()) { 
       System.out.println("Connected"); 
    //Do something with the connected socket 

run 메소드 호출 할 때 나는 다음과 같은 로그가 나타납니다

07-12 14:17:10.906 9941-10518/com.example.niekdewit.test I/System.out: Try to connect 
07-12 14:17:10.910 9941-10518/com.example.niekdewit.test W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback 
07-12 14:17:13.025 9941-10518/com.example.niekdewit.test W/System.err: java.io.IOException: read failed, socket might closed or timeout, read ret: -1 
07-12 14:17:13.025 9941-10518/com.example.niekdewit.test W/System.err:  at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:573) 
07-12 14:17:13.025 9941-10518/com.example.niekdewit.test W/System.err:  at android.bluetooth.BluetoothSocket.waitSocketSignal(BluetoothSocket.java:550) 
07-12 14:17:13.025 9941-10518/com.example.niekdewit.test W/System.err:  at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:325) 
07-12 14:17:13.025 9941-10518/com.example.niekdewit.test W/System.err:  at com.example.niekdewit.test.BluetoothConnectThread.run(BluetoothConnectThread.java:33) 
07-12 14:17:13.025 9941-10518/com.example.niekdewit.test I/System.out: Couldn't establish Bluetooth connection! (1) 
07-12 14:17:13.025 9941-10518/com.example.niekdewit.test I/System.out: Try to connect again 
07-12 14:17:13.025 9941-10518/com.example.niekdewit.test W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback 
07-12 14:17:13.147 9941-9941/com.example.niekdewit.test I/Timeline: Timeline: Activity_idle id: [email protected] time:762005544 
07-12 14:17:16.503 9941-9941/com.example.niekdewit.test I/Timeline: Timeline: Activity_idle id: [email protected] time:762008901 
07-12 14:17:17.279 9941-10518/com.example.niekdewit.test I/System.out: Connected 

나는 2 선

mmSocket = (BluetoothSocket) mmDevice.getClass().getMethod("createRfcommSocket", new Class[]{int.class}).invoke(mmDevice, Integer.valueOf(1)); 
를 교체 시도를

mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid); 

하지만 작동하지 않습니다, 그것은 다른 로그를 생성합니다. 이 방법을 사용하면 두 번째 시도에서도 연결할 수 없습니다. 내가 처음이 줄

mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid); 

연결 그리고 두 번째 시도에 시도 할 때

07-12 14:27:15.968 15135-15622/com.example.niekdewit.test I/System.out: Try to connect 
07-12 14:27:15.969 15135-15622/com.example.niekdewit.test W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback 
07-12 14:27:18.205 15135-15622/com.example.niekdewit.test W/System.err: java.io.IOException: read failed, socket might closed or timeout, read ret: -1 
07-12 14:27:18.205 15135-15622/com.example.niekdewit.test W/System.err:  at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:573) 
07-12 14:27:18.205 15135-15622/com.example.niekdewit.test W/System.err:  at android.bluetooth.BluetoothSocket.waitSocketSignal(BluetoothSocket.java:550) 
07-12 14:27:18.205 15135-15622/com.example.niekdewit.test W/System.err:  at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:325) 
07-12 14:27:18.205 15135-15622/com.example.niekdewit.test W/System.err:  at com.example.niekdewit.test.BluetoothConnectThread.run(BluetoothConnectThread.java:36) 
07-12 14:27:18.205 15135-15622/com.example.niekdewit.test I/System.out: Couldn't establish Bluetooth connection! (1) 
07-12 14:27:18.206 15135-15622/com.example.niekdewit.test I/System.out: Try to connect again 
07-12 14:27:18.206 15135-15622/com.example.niekdewit.test W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback 
07-12 14:27:18.319 15135-15135/com.example.niekdewit.test I/Timeline: Timeline: Activity_idle id: [email protected] time:762610717 
07-12 14:27:20.669 15135-15622/com.example.niekdewit.test W/System.err: java.io.IOException: read failed, socket might closed or timeout, read ret: -1 
07-12 14:27:20.669 15135-15622/com.example.niekdewit.test W/System.err:  at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:573) 
07-12 14:27:20.669 15135-15622/com.example.niekdewit.test W/System.err:  at android.bluetooth.BluetoothSocket.waitSocketSignal(BluetoothSocket.java:550) 
07-12 14:27:20.669 15135-15622/com.example.niekdewit.test W/System.err:  at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:325) 
07-12 14:27:20.669 15135-15622/com.example.niekdewit.test W/System.err:  at com.example.niekdewit.test.BluetoothConnectThread.run(BluetoothConnectThread.java:36) 
07-12 14:27:20.669 15135-15622/com.example.niekdewit.test I/System.out: Couldn't establish Bluetooth connection! (3) 
07-12 14:27:20.854 15135-15135/com.example.niekdewit.test I/Timeline: Timeline: Activity_idle id: [email protected] time:762613252 

가 발생

mmSocket = (BluetoothSocket) mmDevice.getClass().getMethod("createRfcommSocket", new Class[]{int.class}).invoke(mmDevice, Integer.valueOf(1)); 

그런 다음이 라인 ("다시 연결을 시도합니다") 첨부 된 첫 번째 로그와 동일한 로그

나는 무슨 일이 일어나고 있는지 전혀 모른다? 여러분 중 누군가 내 코드에 이상이 있는지 보거나 올바른 방향으로 나를 가리켜 주길 바랍니다.

EDIT : 기기가 이미 페어링 된 경우 첫 번째 기기에 연결할 수 있으므로 두 방법을 모두 사용해보세요.

EDIT2 :

또한
mmSocket = mmDevice.createInsecureRfcommSocketToServiceRecord(uuid); 

작동하지 않습니다

답변

0
나는이

// BluetoothDevice device= BluetoothAdapter.getDefaultAdapter().getRemoteDevice("00:1C:4D:02:A6:55"); 
    // You already have a device so capture the method then invoke a socket 
    Method m = device.getClass().getMethod("createRfcommSocket", new Class[] { int.class }); 
    socket = (BluetoothSocket)m.invoke(device, Integer.valueOf(1)); 
    socket.connect(); 
    // Do some cool stuff 

mmSocket = (BluetoothSocket) mmDevice.getClass().getMethod("createRfcommSocket", new Class[]{int.class}).invoke(mmDevice, Integer.valueOf(1)); 

를이 난 항상 장치에 연결하는 방법입니다 변경하려고 할 것이다

+0

답변 주셔서 감사합니다. 그러나 문제가 해결되지 않았습니다. 출력 로그는 내가 내 게시물에 첨부 한 첫 번째 로그와 동일합니다. ( – Niek

+0

블루투스 관리자가 정의되어 있지만 사용하지 않았습니다. 'W/BluetoothAdapter : BluetoothManagerCallback없이 호출 된 getBluetoothService() –

+0

블루투스 관리자는 블루투스 연결 등을 만들기 위해 액티비티의 인터페이스 역할을하는 자신이 쓴 클래스입니다. 나는 이것을 스레드 클래스에서 관리자에게 연결에 알리기 위해이 클래스를 사용합니다 만들어졌습니다 : – Niek

관련 문제