2010-08-24 109 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