2013-10-15 48 views
0

我正在開發其中如果兩個設備通過藍牙藍牙設備連接問題

我使用下面的代碼註冊的廣播Reciever連我檢查的Android應用程序。

IntentFilter filter1 = new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED); 
    IntentFilter filter2 = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED); 

    this.registerReceiver(mReceiver, filter1); 
    this.registerReceiver(mReceiver, filter2); 

BroadcastReceiver看起來像這樣。

private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 
    public void onReceive(Context context, Intent intent) { 
     String action = intent.getAction(); 
     // When discovery finds a device 

     if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) 
     { 
      Log.e("bluetooth connected","bluetooth connected"); 
     } 
     else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) 
     { 
      Log.e("bluetooth not connected","bluetooth not connected"); 
     }  
    } 
}; 

如何這不工作。不知道我哪裏出錯了。請幫忙!謝謝!

回答

2

你有清單中的BLUETOOTH權限嗎?

<uses-permission android:name="android.permission.BLUETOOTH" /> 

也不要註冊一個接收器兩次,並使用兩個過濾器,你可以做

IntentFilter filter = new IntentFilter(); 
filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED); 
filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);  

this.registerReceiver(mReceiver, filter); 
+0

我已經添加了權限並使用了上面的代碼。不工作。它不是調用BroadcastReceiver! – sanjana

0

沒有ü嘗試藍牙管理權限?

<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> 
+0

是的。我已經添加了此權限。 – sanjana

0

Android文檔聲明「ACL連接由Android藍牙堆棧自動管理」。可能它們不會在應用程序級別進行管理; BluetoothDevice.ACTION_ACL_CONNECTEDBluetoothDevice.ACTION_ACL_DISCONNECTED調度取決於設備和固件版本(例如,我經歷過Nexus S正確調度它們,而舊的GT-I5800不調度)。