2017-01-11 153 views
0

Bluetooth接続機能用のクラスを作成していますが、ペアになった後でデバイスを接続する場所に到達するまでうまくいっているようです。 socket.connectを呼び出そうとするとIOExceptionが発生します。ここでBluetoothでソケットを接続するとIOExceptionが発生する

mSocket.connect(); 

を呼び出して、私は取得していますスタックトレースとき

エラーが発生します。

私はこれらのstackoverflowの質問

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!"); 
     } 
    } 
} 
関連する問題