2012-12-03 46 views
1

因此Im試圖讓這小小的一段代碼運行listenUsingRfcommWithServiceRecord(字符串名稱,UUID UUID)不工作

public AcceptThread() { 
    // Use a temporary object that is later assigned to mmServerSocket, 
    // because mmServerSocket is final 
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
    BluetoothServerSocket tmp = null; 
    try { 
     // MY_UUID is the app's UUID string, also used by the client code 
     tmp = mBluetoothAdapter.listenUsingRfcommWithServiceRecord("Server", MY_UUID); 
    } catch (IOException e) { } 
    mmServerSocket = tmp; 
} 

但是,當我從那裏TMP應分配它仍然是空行繼續。藍牙已被激活,除了它以外,其他任何東西都不會在任何事情發生時設置tmp。任何想法爲什麼不呢?順便說一句。這種方法不適用於2.2安卓機器,因爲它在2.2機器上的那條線上崩潰,而不是在我的4.1.2機器上崩潰。

如果需要更多的信息能夠回答,請直接詢問並儘可能地給予我。

回答

1

發現我沒有給予應用程序使用藍牙的權限,因此解決了。

1

我對Android開發完全陌生,遇到同樣的問題。如果其他人遇到這種情況,可以提供更多的幫助:

因爲這發生在服務器端,所以在啓動接受連接線程之前,請確保啓用了可啓用性。當收到正確的resultCode時,可以通過在onActivityResult()方法中啓動線程。