2014-01-25 140 views
4

我試圖開發一個Android應用程序,將圖像從一臺設備傳輸到另一臺。接收到的圖像將顯示在我的應用程序的ImageView中。爲了完成我的任務,我想發送一個位圖的字節數組。我能夠在imageview上獲得第一張圖片。但是,只要我點擊按鈕發送另一個圖像,應用程序就無法發送位圖。它向我展示了一個例外「java.io.IOException:服務fiscovery失敗。」要成功發送任何圖像,我需要在接收/遠程設備上重新啓動我的應用程序。任何人都可以請建議解決畝問題。 logcat也包含在下面。Android藍牙:java.io.IOException:服務發現失敗

代碼建立連接:

 private class StartConnectionThread extends Thread{ 
    private final BluetoothSocket bluetoothSocket; 
    private final BluetoothDevice bluetoothDevice; 
    public StartConnectionThread(BluetoothDevice device){ 
     BluetoothSocket tempBluetoothSocket=null; 
     bluetoothDevice=device; 
     try 
     { 
      System.out.println(uuid); 
      tempBluetoothSocket=device.createRfcommSocketToServiceRecord(uuid); 
     } 
     catch(IOException ioException) 
     { 

     } 
     bluetoothSocket=tempBluetoothSocket; 
    } 
    @Override 
    public void run() { 
     // TODO Auto-generated method stub 
     bluetoothAdapter.cancelDiscovery(); 
     try 
     { 
      try { 
       Thread.sleep(2000); 
      } catch (InterruptedException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      bluetoothSocket.connect(); 

     } 
     catch(IOException ioException) 
     { 
      System.out.println("bluetoothSocketInThread failed"); 
      try 
      { 
       bluetoothSocket.close(); 
      } 
      catch(IOException cancelIOException) 
      { 

      } 
      return; 
     } 
     manageConnectedSocket(bluetoothSocket); 
    } 
    public void cancel() 
    { 
     try 
     { 
      bluetoothSocket.close(); 
     } 
     catch(IOException ioException) 
     { 

     } 
    } 
} 

代碼接受連接:

 private class AcceptConnectionThread extends Thread 
{ 
    private final BluetoothServerSocket bluetoothServerSocket; 
    public AcceptConnectionThread() { 
     // TODO Auto-generated constructor stub 
     System.out.println("constructor"); 
     BluetoothServerSocket tempBluetoothServerSocket=null; 
     try 
     { 
      tempBluetoothServerSocket=bluetoothAdapter.listenUsingRfcommWithServiceRecord("My Souvenirs", uuid); 
     } 
     catch(IOException ioException) 
     { 
     } 
     bluetoothServerSocket=tempBluetoothServerSocket; 
    } 
    @Override 
    public void run() { 
     // TODO Auto-generated method stub 
     BluetoothSocket bluetoothSocket=null; 
     while(true) 
     { 
      try 
      { 
       try { 
        Thread.sleep(2000); 
       } catch (InterruptedException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
       System.out.println(bluetoothServerSocket); 
       if(bluetoothServerSocket!=null) 
       { 
        bluetoothSocket=bluetoothServerSocket.accept(); 
       } 
       System.out.println("accept"); 
      } 
      catch(IOException ioException){ 
       break; 
      } 
      if(bluetoothSocket!=null) 
      { 
       manageConnectedSocket(bluetoothSocket); 
       try { 
        bluetoothServerSocket.close(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
       break; 
      } 
     } 
    } 
    public void cancel() 
    { 
     try{ 
      bluetoothServerSocket.close(); 
     } 
     catch(IOException ioException){ 

     } 
    } 

} 

代碼來管理連接:

 private class ManageConnectedDevicesThread extends Thread 
{ 
    private final BluetoothSocket connectedBluetoothSocket; 
    public ManageConnectedDevicesThread(BluetoothSocket socket) { 
     // TODO Auto-generated constructor stub 
     connectedBluetoothSocket=socket; 
     InputStream tempInputStream=null; 
     OutputStream tempOutputStream=null; 
     try 
     { 
      tempInputStream=socket.getInputStream(); 
      tempOutputStream=socket.getOutputStream(); 
     } 
     catch(IOException ioException) 
     { 

     } 
     inputStream=tempInputStream; 
     outputStream=tempOutputStream; 
    } 
    @Override 
    public void run() { 
     // TODO Auto-generated method stub 
        byte[] buffer=new byte[1024*8]; 
     int bytes; 
     while(true) 
     { 
      try 
      { 
       bytes=inputStream.read(buffer); 
       handler.obtainMessage(MESSAGE_READ,bytes,-1,buffer).sendToTarget(); 
       System.out.println("handler"); 
      } 
      catch(IOException ioException) 
      { 
       System.out.println("for handler:" +ioException); 
       break; 
      } 
     } 
    } 
    public void write(byte[] bytes) 
    { 
     try 
     { 
      outputStream.write(bytes); 
     } 
     catch(IOException ioException){ 
      System.out.println("exception in wrie tatement of managing connections"); 
     } 

    } 


    public void close() 
    { 
     try { 
      connectedBluetoothSocket.close(); 
     } catch (IOException e) { 
      // TODO: handle exception 
     } 
    } 
} 

代碼重置連接:

爲處理程序

代碼如下所示:

private final Handler handler=new Handler(){ 
public void handleMessage(Message msg) { 
    switch (msg.what) { 
    case MESSAGE_READ: 
     System.out.println("MESSAGE_READ"); 
     byte[] readBuf = (byte[]) msg.obj; 
     // construct a string from the valid bytes in the buffer 
     String readMessage = new String(readBuf, 0, msg.arg1); 
     byte[] b=readMessage.getBytes(); 
     Bitmap bitmap1=BitmapFactory.decodeByteArray(readBuf, 0, readBuf.length); 
     imageView.setImageBitmap(bitmap1); 
     break; 
    } 

};

的logcat中顯示以下內容:提前

01-25 14:49:31.800: D/dalvikvm(9451): Debugger has detached; object registry had 1 entries 
    01-25 14:49:38.380: V/BluetoothSocket.cpp(9451): initSocketNative 
    01-25 14:49:38.380: V/BluetoothSocket.cpp(9451): ...fd 40 created (RFCOMM, lm = 26) 
    01-25 14:49:38.380: V/BluetoothSocket.cpp(9451): initSocketFromFdNative 
    01-25 14:49:40.420: D/BluetoothUtils(9451): isSocketAllowedBySecurityPolicy start : device null 
    01-25 14:49:41.680: I/System.out(9451): bluetoothSocketInThread failed 
    01-25 14:49:41.680: V/BluetoothSocket.cpp(9451): abortNative 
    01-25 14:49:41.680: V/BluetoothSocket.cpp(9451): ...asocket_abort(40) complete 
    01-25 14:49:41.680: V/BluetoothSocket.cpp(9451): destroyNative 
    01-25 14:49:41.680: V/BluetoothSocket.cpp(9451): ...asocket_destroy(40) complete 

感謝。

+0

你能告訴我如何解決這個問題嗎?我太絕望了...... –

+1

抱歉,遲到回覆。由於服務連接需要幾毫秒才能執行,只需等待大約一秒左右,然後運行您的代碼。然後系統將檢測連接的服務。 –

+0

謝謝你的男人。我會嘗試。 –

回答

6

也許你可以嘗試添加thread.sleep一秒?見this discussion

「我已經能夠解決這個問題的唯一方法是通過關閉連接之前添加 的Thread.Sleep一秒鐘」

也看到丹的兩個連續的意見對this thread

「我能夠得到這個只運行調用分離到 findBT()之後; openBT();

否則,mmSocket.connect();拋出一個異常,「服務發現 失敗」

但如果我把findBT()放在onCreate(),只需使用按鈕爲 openBT();它工作正常。或者,如果我製作第二個按鈕,每個按鈕一個,它就可以正常工作。

對此提出建議?「

從第二評論摘錄:

Set pairedDevices = mBluetoothAdapter.getBondedDevices(); 
mmDevice = mBluetoothAdapter.getRemoteDevice(「00:06:66:46:5A:91″); 
if (pairedDevices.contains(mmDevice)) 
{ 
statusText.setText(「Bluetooth Device Found, address: 」 + mmDevice.getAddress()); 
Log.d(「ArduinoBT」, 「BT is paired」); 
} 

,我進入我的藍牙設備的地址原始代碼 找到設備並返回正確的地址,但 mmSocket。 connect();生成異常「java.io.IOException: 服務發現失敗」

建議?

+0

謝謝,它解決了我的問題。現在我在同一個應用程序中面臨另一個問題。我的下一個問題的鏈接如下: http://stackoverflow.com/questions/21568601/successive-transfer-of-bitmap-via-bluetooth-generate-multiple-inputstream-read 希望ypu將幫助我也解決了這個問題。 再次感謝。 –