2010-08-24 177 views
0

我在Android 2.1中看到它突出說它支持新的平臺:「藍牙2.1,新的BT配置文件:對象推送配置文件(OPP)和電話簿訪問配置文件(PBAP)」。現在我擁有支持OPP的藍牙adpater。我可以搜索並與之配對。但我怎樣才能得到它發送給我的txt文件。這個函數沒有API。我使用如下結構的BluetoothChat示例代碼。但是代碼是 「bytes = mmInStream.read(buffer);」中的塊。 沒有任何反應。爲什麼?沒有收到?如何用對象推送協議連接藍牙適配器?

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

    public ConnectedThread(BluetoothSocket socket) { 
     Log.d(TAG, "create ConnectedThread"); 
     mmSocket = socket; 
     InputStream tmpIn = null; 
     OutputStream tmpOut = null; 

     // Get the BluetoothSocket input and output streams 
     try { 
      tmpIn = socket.getInputStream(); 
      tmpOut = socket.getOutputStream(); 
     } catch (IOException e) { 
      Log.e(TAG, "temp sockets not created", e); 
     } 

     mmInStream = tmpIn; 
     mmOutStream = tmpOut; 
    } 

    public void run() { 
     Log.i(TAG, "BEGIN mConnectedThread"); 
     byte[] buffer = new byte[1024]; 
     int bytes; 

     // Keep listening to the InputStream while connected 
     while (true) { 
      try { 
       // Read from the InputStream 
       bytes = mmInStream.read(buffer); 

       // Send the obtained bytes to the UI Activity 
       mHandler.obtainMessage(BluetoothChat.MESSAGE_READ, bytes, -1, buffer) 
         .sendToTarget(); 
      } catch (IOException e) { 
       Log.e(TAG, "disconnected", e); 
       connectionLost(); 
       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(BluetoothChat.MESSAGE_WRITE, -1, -1, buffer) 
        .sendToTarget(); 
     } catch (IOException e) { 
      Log.e(TAG, "Exception during write", e); 
     } 
    } 

    public void cancel() { 
     try { 
      mmSocket.close(); 
     } catch (IOException e) { 
      Log.e(TAG, "close() of connect socket failed", e); 
     } 
    } 
} 

回答

1

據我所知,OPP和PBAP功能是由Android API爲開發者提供的。

他們所做的是將這些配置文件實現爲應用程序,並將其與平臺一起發佈。您可以在設備上看到運行OPP和PBAP服務,因此他們將接受並處理外部連接,而不是您的應用。

這些應用我提到的源代碼都可以在這裏:
https://android.googlesource.com/platform/packages/apps/Bluetooth