2011-02-24 23 views
12

मैं इस प्रकार एक ब्लूटूथ कनेक्शन सेट करने का प्रयास करें: त्रुटि के बिनाएंड्रॉयड ब्लूटूथ कनेक्शन मुद्दा

DEBUG/BluetoothConnectionService(3439): **connect to: 00:18:E4:21:8B:5E** 
INFO/BluetoothConnectionService(3439): createRfcommSocket 
INFO/BluetoothConnectionService(3439): Bluetooth socket initialised 
INFO/BluetoothConnectionService(3439): BEGIN mConnectThread on Device : 00:18:E4:21:8B:5E 
INFO/BluetoothConnectionService(3439): **start Bluetooth socket connection** 
INFO/BluetoothConnectionService(3439): setState() 1 -> 2 
INFO/ProtectionService(3439): MESSAGE_STATE_CHANGE: 1 
DEBUG/ProtectionService(3439): STATE_CHANGED to STATE_LISTEN 
INFO/ProtectionService(3439): MESSAGE_STATE_CHANGE: 2 
DEBUG/abtfilt(2772): **Conn complete** 
INFO/bluetoothd(2776): link_key_request (sba=24:21:AB:F4:69:25, dba=00:18:E4:21:8B:5E) 
INFO/bluetoothd(2776): link_key_request (sba=24:21:AB:F4:69:25, dba=00:18:E4:21:8B:5E) 
INFO/BluetoothConnectionService(3439): **end of Bluetooth socket connection** 
INFO/BluetoothConnectionService(3439): **The device is considered as connected, call connected thread** 

BluetoothSocket.connect() वापसी:

private class ConnectThread extends Thread { 
    private final BluetoothSocket mmSocket; 
    private final BluetoothDevice mmDevice; 
    BluetoothSocket connection = null; 

    public ConnectThread(BluetoothDevice device) { 
     mmDevice = device; 
     // Get a BluetoothSocket for a connection with the 
     // given BluetoothDevice    
     try { 


      if(D) Log.i(TAG, "createRfcommSocket"); 
      Method m = mmDevice.getClass().getMethod("createRfcommSocket",new Class[] { int.class }); 
      connection = (BluetoothSocket) m.invoke(mmDevice, 1); 

      Utils.pause(100); 
     } catch (Exception e) { 
      Log.e(TAG, "create() failed", e); 
     } 
     if(D) Log.i(TAG,"Bluetooth socket initialised"); 
     mmSocket = connection; 
    } 

    public void run() { 
     if(D) Log.i(TAG, "BEGIN mConnectThread on Device : " + mmDevice); 
     setName("ConnectThread"); 

     if (mmSocket != null) { 
      // Always cancel discovery because it will slow down a connection 
      if(mAdapter.isDiscovering()) { 
       if(D) Log.i(TAG, "stop discovering before trying to connect"); 
       mAdapter.cancelDiscovery(); 
      } 

      // Make a connection to the BluetoothSocket 
      try { 
       // This is a blocking call and will only return on a 
       // successful connection or an exception 
       if(D) Log.i(TAG, "start Bluetooth socket connection"); 
       mmSocket.connect(); 
       if(D) Log.i(TAG, "end of Bluetooth socket connection"); 
      } catch (Exception e1) { 
       Log.e(TAG, "connect failed", e1); 
       connectionFailed(); 
       // Reset the socket 
       resetConnection(mmSocket); 
       // Start the service over to restart listening mode 
       if(D) Log.i(TAG, "restart connection"); 
       BluetoothConnectionService.this.start(); 
       if(D) Log.i(TAG, "return"); 
       return; 
      } 

      // Start the connected thread 
      if(D) Log.i(TAG, "The device is considered as connected, call connected thread"); 
      connected(mmSocket, mmDevice); 
     } else { 
      if(D) Log.i(TAG, "connection fail"); 
      connectionFailed(); 
      BluetoothConnectionService.this.start(); 
     } 
    } 

    public void cancel() { 
     if(D) Log.i(TAG,"connect thread cancel"); 
     resetConnection(mmSocket); 
    } 
} 

कनेक्शन ठीक हो रहा है, मैं इस लॉग मिलता है।

तब मैं इस के संबंध प्रबंधन:

private class ConnectedThread extends Thread { 
    private final BluetoothSocket mmSocket; 
    private final InputStream mmInStream; 
    private final OutputStream mmOutStream; 

    public ConnectedThread(BluetoothSocket socket) { 
     if(D) Log.d(TAG, "create ConnectedThread"); 
     mmSocket = socket; 

     // Get the BluetoothSocket input and output streams 
     try { 
      if(D) Log.i(TAG, "Get input stream"); 
      tmpIn = socket.getInputStream(); 
      if(D) Log.i(TAG, "Get output stream"); 
      tmpOut = socket.getOutputStream(); 
      if(D) Log.i(TAG, "isConnected = true"); 
      isConnected = true; 
     } catch (IOException e) { 
      Log.e(TAG, "Input and Output sockets not created", e); 
      isConnected = false; 
     } 

     mmInStream = tmpIn; 
     mmOutStream = tmpOut; 
    } 

    public void run() { 
     if(D) Log.i(TAG, "BEGIN mConnectedThread"); 
     byte[] buffer = new byte[1024]; //buffer store for the stream 
     int bytes; //bytes returned from read() 

     // Keep listening to the InputStream until an exception occurs 
     while(true) { 
      try { 
       // Read from the InputStream 
       if(D) Log.i(TAG, "start read mmInStream"); 
       bytes = mmInStream.read(buffer); 
       if(D) Log.i(TAG, "Bytes read : "+bytes); 

       // Send the obtained bytes to the UI Activity 
       mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer).sendToTarget(); 
      } catch (IOException e) { 
       Log.e(TAG, "ConnectedThread : disconnected", e); 
       break; 
      } 
     } 
    } 

    /** 
    * Write to the connected OutStream. 
    * 
    * @param buffer 
    *   The bytes to write 
    */ 
    public void write(byte[] buffer) { 
     try { 
      mmOutStream.write(buffer); 

      // Share the sent message back to the UI Activity 
      mHandler.obtainMessage(MESSAGE_WRITE, -1, -1, buffer).sendToTarget(); 
      // mmOutStream.flush(); 
     } catch (IOException e) { 
      Log.e(TAG, "Exception during write", e); 
      connectionFailed(); 
     } 
    } 

    public void cancel() { 
     if(D) Log.i(TAG,"connected Thread cancel"); 
     resetConnection(mmSocket); 

    } 
} 

और मैं यह त्रुटि प्राप्त:

DEBUG/BluetoothConnectionService(3439): create ConnectedThread 
INFO/BluetoothConnectionService(3439): Get input stream 
INFO/BluetoothConnectionService(3439): Get output stream 
INFO/BluetoothConnectionService(3439): isConnected = true 
INFO/BluetoothConnectionService(3439): BEGIN mConnectedThread 
INFO/BluetoothConnectionService(3439): start read mmInStream 
INFO/BluetoothConnectionService(3439): setState() 2 -> 3 
INFO/ProtectionService(3439): MESSAGE_STATE_CHANGE: 3 
DEBUG/ProtectionService(3439): connectionEstablished 
**ERROR/BluetoothConnectionService(3439): ConnectedThread : disconnected 
ERROR/BluetoothConnectionService(3439): java.io.IOException: Software caused connection abort 
ERROR/BluetoothConnectionService(3439):  at android.bluetooth.BluetoothSocket.readNative(Native Method) 
ERROR/BluetoothConnectionService(3439):  at android.bluetooth.BluetoothSocket.read(BluetoothSocket.java:307) 
ERROR/BluetoothConnectionService(3439):  at android.bluetooth.BluetoothInputStream.read(BluetoothInputStream.java:96) 
ERROR/BluetoothConnectionService(3439):  at java.io.InputStream.read(InputStream.java:133)** 
INFO/ProtectionService(3439): Send Bluetooth message 
**ERROR/BluetoothConnectionService(3439): Exception during write 
ERROR/BluetoothConnectionService(3439): java.io.IOException: Transport endpoint is not connected 
ERROR/BluetoothConnectionService(3439):  at android.bluetooth.BluetoothSocket.writeNative(Native Method) 
ERROR/BluetoothConnectionService(3439):  at android.bluetooth.BluetoothSocket.write(BluetoothSocket.java:317)** 

त्रुटि स्पष्ट रूप से आते हैं जब मैं यह कर रहा हूं:

bytes = mmInStream.read(buffer); 

क्या क्या मैं गलत कर रहा हूँ? मैं एक सप्ताह के लिए इस त्रुटि पर काम कर रहा हूं और मैं नहीं जा सकता ...

किसी भी मदद की बहुत सराहना की जाएगी!

+0

आप इस मुद्दे में मदद कर सकते हैं http://stackoverflow.com/प्रश्न/40559549/एंड्रॉइड-ब्लूटूथ-जावा-आईओ-ioexception-bt-socket-off-read-return-1? noredirect = 1 # comment68358423_40559549 – Achin

उत्तर

17

मैं एक बहुत ही इसी तरह की समस्या कुछ दिन पहले था ...

प्रयास करें कि आपके कोड में यह जाँच जोड़ने:

if(mmInStream.available() > 0){ 
    bytes = mmInStream.read(buffer); 
    mHandler.obtainMessage(MESSAGE_WRITE, -1, -1, buffer).sendToTarget(); 
} 

यह मेरी समस्या तय ...

गुड लक;)

+0

धन्यवाद, यह काम करता है! = डी – Laurent

+0

अच्छा, बिल्कुल समस्या जो मैं कर रहा था। –

+5

असल में, यह कोड अपवाद को रोकता है, लेकिन वास्तव में कनेक्शन खोने के मामले में, यह थ्रेड को पुनरारंभ करने और ठीक होने से रोकता है। इसलिए जब मैं जवाब से सहमत हूं, तो मैंने अंततः इसका उपयोग न करने का फैसला किया। –

-1

इस प्रकार मैंने इस मुद्दे को हल किया: कुछ प्रकार के टाइमआउट को लागू करके। मेरे मामले में 3 सेकंड। कोड मेरा नहीं है, लेकिन यह बहुत अच्छा काम करता है। मूल रूप से ऐसा होता है कि इनपुटस्ट्रीम में कुछ पाने के लिए प्रतीक्षा करने वाला दूसरा लूप होता है। जब इनपुटस्ट्रीम में कुछ होता है, तो यह आगे बढ़ता है और इसे पढ़ता है। इस तरह आपको mmInputstream.read() विधि में अवरुद्ध होने से बचना चाहिए।

मुझे लगता है कि यह एक अच्छा समाधान है। यह मेरे लिए अच्छा काम करता है: मैं लेखक नहीं हूं।

ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
       baos.reset(); 

       while (true) { 
       treshHold = 0; 
        try { 
         try { 
          while (mmInStream.available() == 0 && treshHold < 3000) { // if there is something in the inputStream or after 3 seconds 
           Thread.sleep(1); 
           treshHold++; 
          } 
         } catch (IOException e1) { 
          e1.printStackTrace(); 
         } catch (InterruptedException e1) { 
          e1.printStackTrace(); 
         } 
         if (treshHold < 3000) { // if less than 3 seconds timeout 
          treshHold = 0; 
          b = mmInStream.read(); 
          readBytes++; 
         } else { 
          break; 
         } 

         baos.write(b); 
         Thread.sleep(1); 

         if (b == 255) { 
          break; // this is how I know I have got the whole response 
         } 
        } catch (IOException e) { 
         e.printStackTrace(); 
         break; 
        } catch (InterruptedException e) { 
         e.printStackTrace(); 
         break; 
        } 
       } 

जबकि (प्राप्त) को आप प्राप्त करने की अपेक्षा के अनुसार बदल सकते हैं। मेरे मामले में जब मैं 0xFF या 255 प्राप्त करता हूं तब से बाहर जाता हूं। उम्मीद है कि इससे मदद मिलती है।

outputStream को लिखने के लिए के रूप में मैं निम्नलिखित प्रयोग किया है:

if(mmOutStream !=null) 
{ 
    try { 
     mmOutStream.flush(); 
    } catch (IOException e1) { 
     // log here ... 
     e1.printStackTrace(); 
    } 
    try { 
     mmOutStream.write(send, 0, sendLength); 
    } catch (IOException e) { 
     // log here ... 
     setState(STATE_NONE); 
    } 
} 

कृपया सही रूप में जवाब निशान अगर यह मदद करता है :)

संबंधित मुद्दे