2017-01-11 2 views
0

블루투스 연결 기능을위한 클래스를 생성합니다. 연결 한 후에 장치를 연결할 위치에 도달 할 때까지 모든 것이 올바르게 작동하는 것처럼 보입니다. socket.connect를 호출하려고하면 IOException이 발생합니다. 여기안드로이드에서 소켓을 연결할 때 IOException이 발생합니다.

mSocket.connect(); 

를 호출 내가 무엇입니까 스택 트레이스 때

오류가 발생합니다.

나는이 유래 질문

Getting java.io.IOException: read failed, socket might closed or timeout, read ret: -1 while printing via bluetooth printer

java.io.IOException: read failed, socket might closed or timeout, read ret: -1 on Android 5.0.1 Lollipop versionBluetooth Connection failed "java.io.IOException: read failed, socket might closed or timeout, read ret: -1"하지만 여전히 내 문제를 해결하는 데 실패 살펴 보았다. 내 코드 전체를 확인해 볼 수 있습니다 HERE 뭔가를 놓친 것 같습니다.

여기에 스택 트레이스가 있습니다.

01-11 18:38:37.399 14502-14502/adc.com.samplebluetooth W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback 
01-11 18:38:37.409 14502-14502/adc.com.samplebluetooth D/BluetoothSocket: connect(), SocketState: INIT, mPfd: {ParcelFileDescriptor: FileDescriptor[60]} 
01-11 18:38:38.629 14502-14502/adc.com.samplebluetooth W/System.err: java.io.IOException: read failed, socket might closed or timeout, read ret: -1 
01-11 18:38:38.629 14502-14502/adc.com.samplebluetooth W/System.err:  at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:505) 
01-11 18:38:38.629 14502-14502/adc.com.samplebluetooth W/System.err:  at android.bluetooth.BluetoothSocket.readInt(BluetoothSocket.java:516) 
01-11 18:38:38.629 14502-14502/adc.com.samplebluetooth W/System.err:  at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:320) 
01-11 18:38:38.639 14502-14502/adc.com.samplebluetooth W/System.err:  at adc.com.samplebluetooth.Bluetooth.connectToPaired(Bluetooth.java:248) 
01-11 18:38:38.639 14502-14502/adc.com.samplebluetooth W/System.err:  at adc.com.samplebluetooth.MainActivity$OnClick.onClick(MainActivity.java:90) 
01-11 18:38:38.639 14502-14502/adc.com.samplebluetooth W/System.err:  at android.view.View.performClick(View.java:4443) 
01-11 18:38:38.639 14502-14502/adc.com.samplebluetooth W/System.err:  at android.view.View$PerformClick.run(View.java:18443) 
01-11 18:38:38.639 14502-14502/adc.com.samplebluetooth W/System.err:  at android.os.Handler.handleCallback(Handler.java:733) 
01-11 18:38:38.639 14502-14502/adc.com.samplebluetooth W/System.err:  at android.os.Handler.dispatchMessage(Handler.java:95) 
01-11 18:38:38.639 14502-14502/adc.com.samplebluetooth W/System.err:  at android.os.Looper.loop(Looper.java:136) 
01-11 18:38:38.639 14502-14502/adc.com.samplebluetooth W/System.err:  at android.app.ActivityThread.main(ActivityThread.java:5017) 
01-11 18:38:38.639 14502-14502/adc.com.samplebluetooth W/System.err:  at java.lang.reflect.Method.invokeNative(Native Method) 
01-11 18:38:38.639 14502-14502/adc.com.samplebluetooth W/System.err:  at java.lang.reflect.Method.invoke(Method.java:515) 
01-11 18:38:38.639 14502-14502/adc.com.samplebluetooth W/System.err:  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
01-11 18:38:38.639 14502-14502/adc.com.samplebluetooth W/System.err:  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
01-11 18:38:38.639 14502-14502/adc.com.samplebluetooth W/System.err:  at dalvik.system.NativeStart.main(Native Method) 

답변

0

내 붙여 넣기 저장소 (내 btw!보다 멋진 코드)를 확인했습니다. 나는 socket.connect()에서 같은 문제를 겪었으므로 다음 코드로 해결했다. 행운을 빕니다.

public void connect_bt(String deviceAddress, String deviceName) { 

    BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter(); 
    device = btAdapter.getRemoteDevice(deviceAddress); 
    UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); 

    try { 
     socket = device.createRfcommSocketToServiceRecord(uuid); 
    } catch (Exception e) { 
     Log.d(TAG,"Error creating socket"); 
    } 

    try { 
     socket.connect(); 

     Log.d(TAG,"1st Attempt, Connected"); 

     connect_obd2(); 

    } catch (IOException e) { 
     Log.d(TAG, "1st Attempt: failed: " + e.getMessage()); 

     try { 
      Log.d(TAG,"2nd Attempt: trying fallback..."); 

      socket =(BluetoothSocket) device.getClass().getMethod("createRfcommSocket", new Class[] {int.class}).invoke(device,1); 
      socket.connect(); 

      Log.d(TAG,"2nd Attempt: Connected"); 

      connect_obd2(); 
     } 
     catch (Exception e) { 
      Log.d(TAG, "2nd Attempt: Couldn't establish Bluetooth connection!"); 
     } 
    } 
} 
관련 문제