2010-12-22 167 views
0

我想有一个广播接收机监听BT设备连接。 有人可以告诉我我要听哪个广播吗? 我试过android.bluetooth.device.action.ACL_CONNECTED,但它似乎没有工作。Android - 蓝牙设备连接广播

谢谢。

+0

看看这个链接http://developer.android.com/reference/android/bluetooth/BluetoothServerSocket.html – Mudassir 2010-12-23 03:32:28

回答

0

您可以使用您的BroadcastReceiver.onReceive()功能里面以下拉设备:

BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
0

如果您正在寻找该装置是“连接”,然后你想要android.bluetooth.adapter.action.CONNECTION_STATE_CHANGED

但是,当设备处于STATE_CONNECTED状态时,不会触发。所以我所做的就是国家在连接,几秒钟内发送一个广播。

if (bluetoothAdapter 
      .getProfileConnectionState(BluetoothProfile.HEADSET) == BluetoothProfile.STATE_CONNECTING 
      || bluetoothAdapter 
        .getProfileConnectionState(BluetoothProfile.A2DP) == BluetoothProfile.STATE_CONNECTING) { 

     final Handler handler = new Handler(); 
     handler.postDelayed(new Runnable() { 
      @Override 
      public void run() { 
       if (bluetoothAdapter 
         .getProfileConnectionState(BluetoothProfile.HEADSET) != BluetoothProfile.STATE_CONNECTING 
         || bluetoothAdapter 
           .getProfileConnectionState(BluetoothProfile.A2DP) != BluetoothProfile.STATE_CONNECTING) { 
        context.sendBroadcast(new Intent(
          BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED)); 
       } 
      } 
     }, 2000); 

一种骇人的,这就是为什么我在Android缺陷跟踪器中发布此问题。 http://code.google.com/p/android/issues/detail?id=25957