2012-08-22 53 views
5

我有使用OBEX Object Push Profile(OPP)通過藍牙發送數據的設備。通過藍牙通過OBEX Object PushProfile接收文件

利用亞行logcat我看到我的Android設備接收的連接(而中止這方面?)

08-22 11:14:37.939: I/BtOppRfcommListener(22586): Accepted connectoin from 00:07:CF:5F:52:A0 
08-22 11:14:37.939: I/BtOpp Service(22586): Start Obex Server 
08-22 11:14:38.109: D/Obex ServerSession(22586): java.io.IOException: Software caused connection abort 
08-22 11:14:38.109: D/PowerManagerService(180): @PowerManagement: 'BtOppObexServer' releaseWakeLock when screen locked 
08-22 11:14:39.219: D/BluetoothEventLoop(180): Device property changed: 00:07:CF:5F:52:A0 property: Connected value: false 

當我安裝藍牙文件傳輸(從市場的免費應用程序),那麼我就能收到文件。 但我不想安裝其他應用程序。

+0

使用以下內容可以發佈有問題的代碼,以便Android上的SO專家可以看看問題? –

+0

這有幫助嗎? http://stackoverflow.com/questions/3625959/android-rfcomm-with-obex-push-not-working – domsom

+0

@domsom我已經嘗試過類似的代碼沒有成功。國際海事組織的問題似乎是,本地藍牙仍然接受傳入的OPP數據並且自定義代碼不會被觸發。因此,解決方案不僅要監聽傳入的連接,還要與本地藍牙「註冊」,以便調用自定義OPP處理程序。 –

回答

2

我相信我(至少有一部分)的解決方案應該允許通過OPP和自定義代碼添加攔截文件。第一步是去設置>應用>運行>藍牙共享和BluetoothOppService

然後我用反射到上BluetoothAdapter(下面的代碼),其允許監聽特定端口上訪問的方法。之後,我們可以攔截傳入的OPP通信並與輸入和輸出流進行交互。 This SO螺紋將與OPP通信部分幫助,但作爲一個初始步驟I讀出的數據流,並用OPP「OK」消息reponded即os.writeByte(ObexSession.OBEX_SUCCESS | ObexSession.OBEX_FINAL_BIT);

// simplified exception handling 
public class BluetoothAdapterProxy 
{ 
    public static final int CHANNEL_OPP = 12; 

    final BluetoothAdapter target; 
    static final Class<?> targetClass = BluetoothAdapter.class; 
    Method listenOn; 

    public BluetoothAdapterProxy(BluetoothAdapter target) 
    { 
     this.target = target; 
     Class<?>[] args = new Class[] { int.class }; 
     try 
     { 
      this.listenOn = targetClass.getDeclaredMethod(
       "listenUsingRfcommOn", args); 
     } 
     catch (NoSuchMethodException e) 
     { 
      e.printStackTrace(); 
     } 
    } 

    public BluetoothServerSocket listenUsingRfcommOn(int channel) 
    { 
     try 
     { 
      return (BluetoothServerSocket) (listenOn.invoke(target, 
       new Object[] { channel })); 
     } 
     catch (Exception e) 
     { 
      // complain loud, complain long 
      throw new RuntimeException(ex); 
     } 
    } 
} 

用法:初始化使用

serverSocket = new BluetoothAdapterProxy(BluetoothAdapter.getDefaultAdapter()) 
    .listenUsingRfcommOn(BluetoothAdapterProxy.CHANNEL_OPP); 

之後,從單獨的Thread(以防止阻止)和遠程設備可以通過socket = serverSocket.accept();

+0

我不明白你爲什麼需要一個包裝類,但無論如何:你可以使用公共的'BluetoothDevice.listenUsingRfcommWithServiceRecord()'方法使用OPP UUID'00001105-0000-1000-8000-00805f9b34fb'。問題的關鍵可能在於另一個(系統)服務已在OBEX通道上偵聽。因此,無論您是否可以使用您所說的「殺死」解決方案,或者使用系統服務並像這樣監控它:http://stackoverflow.com/questions/3625959/android-rfcomm-with-obex-push-not-工作 – domsom

+0

包裝類用於訪問'hidden android api'中的方法,該代碼在運行時可見,但從發佈的API和android.jar中排除。我有大量的測試代碼來探測已發佈的API - 雖然沒有一個測試已啓用自定義代碼來響應外部OPP數據,但聽力並不是問題。 –

+0

我還應該提及,發佈的示例代碼不是完整的解決方案,因爲我發現它在不同設備上的行爲不同。它能夠通過我的HTC One X上的OPP接收數據,但在三星Nexus S和Galaxy Tab 2上表現不同,並不完全成功。 –