多个BLE设备连接未收到已连接/已断开连接的事件

时间:2015-05-22 09:52:23

标签: android bluetooth bluetooth-lowenergy android-bluetooth

在从本机获取连接/断开连接事件后,我需要连接/断开多个蓝牙LE设备。

i.e.BluetoothAdapter.STATE_CONNECTED/BluetoothAdapter.STATE_DISCONNECTED

我能够收到所有的活动 BluetoothAdapter.STATE_CONNECTING,BluetoothAdapter.STATE_CONNECTEDBluetoothAdapter.STATE_DISCONNECTING,BluetoothAdapter.STATE_DISCONNECTED

如果我正在尝试连接第一台设备。但是当第二个设备尝试连接它时,我只给出了绑定状态而不是连接/断开连接状态。

我已注册下面提到的意图以接收状态更改

<action android:name="android.bluetooth.device.action.BOND_STATE_CHANGED" />
<action android:name="android.bluetooth.device.action.ACL_CONNECTED" />
<action android:name="android.bluetooth.device.action.PAIRING_REQUEST" />
<action android:name="android.bluetooth.device.action.CLASS_CHANGED" />
<action   
 android:name="android.bluetooth.adapter.action.CONNECTION_STATE_CHANGED" />
<action android:name="android.bluetooth.adapter.action.STATE_CHANGED" />
<action android:name="android.bluetooth.device.action.ACL_DISCONNECTED" />

请找到我收到的课程

public class ConnectionReciever extends BroadcastReceiver {

   @Override
   public void onReceive(Context arg0, Intent intent) {

    String action = intent.getAction();
    if(action.equalsIgnoreCase(BluetoothDevice.ACTION_BOND_STATE_CHANGED)) {
    int extra = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, 345);  
    Log.d("<<ConnectionReciever>>", ">>>>>EXTRA_Bond_STATE = "+ extra);
    int extra1 = 
       intent.getIntExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE, 345);    
    Log.d("<<ConnectionReciever>>", 
           ">>>>>EXTRA_CONNECTION_STATE = "+ extra1);
        switch (extra) {
        case BluetoothDevice.BOND_BONDED:

            Log.d("Bikash---", "bond bonded BluetoothDevice.BOND_BONDED");
            break;
        case BluetoothDevice.BOND_BONDING:
            Log.d("Bikash---", "bond bonding BluetoothDevice.BOND_BONDING");
            break;
        case BluetoothDevice.BOND_NONE:
            Log.d("Bikash---", "bond NONE---  BluetoothDevice.BOND_None");
            break;

        default:
            break;
        }
    }      
 if(action.equalsIgnoreCase(
   BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED)) {
   int extra = intent.getIntExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE,
    BluetoothAdapter.STATE_DISCONNECTED);

    BluetoothDevice device = 
       intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
        Log.d("Bikash---", "--------- Bluetooth A2DP device 
           :"+device.getName()+" address :"+device.getAddress());

        switch (extra) {
        case BluetoothAdapter.STATE_CONNECTING:
            Log.d("Bikash---", "------- Bluetooth A2DP state Connecting");
            break;
        case BluetoothAdapter.STATE_CONNECTED:
            Log.d("Bikash---", "------- Bluetooth A2DP state Connected");
            break;
        case BluetoothAdapter.STATE_DISCONNECTING:
            Log.d("Bikash---", "------- Bluetooth A2DP state 
                 Disconnecting");
            break;
        case BluetoothAdapter.STATE_DISCONNECTED:
            Log.d("Bikash---", "------- Bluetooth A2DP state Disconnected");
            break;
        default:
            Log.d("Bikash---", "------- default case 1");
            break;
        }

    }       


}

0 个答案:

没有答案