Android 对于多个可连接设备,未接收到已连接/已断开连接的事件
在从本机获取连接/断开事件后,我需要连接/断开多个蓝牙LE设备Android 对于多个可连接设备,未接收到已连接/已断开连接的事件,android,bluetooth,bluetooth-lowenergy,android-bluetooth,Android,Bluetooth,Bluetooth Lowenergy,Android Bluetooth,在从本机获取连接/断开事件后,我需要连接/断开多个蓝牙LE设备 即BluetoothAdapter.STATE\u CONNECTED/BluetoothAdapter.STATE\u DISCONNECTED 我可以接收所有的事件,比如 BluetoothAdapter.STATE\u CONNECTING,BluetoothAdapter.STATE\u CONNECTED,BluetoothAdapter.STATE\u DISCONNECTED,BluetoothAdapter.STAT
即BluetoothAdapter.STATE\u CONNECTED/BluetoothAdapter.STATE\u DISCONNECTED
我可以接收所有的事件,比如
BluetoothAdapter.STATE\u CONNECTING,BluetoothAdapter.STATE\u CONNECTED
,BluetoothAdapter.STATE\u DISCONNECTED,BluetoothAdapter.STATE\u 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;
}
}
}
公共类ConnectionReceiver扩展了BroadcastReceiver{
@凌驾
公共void onReceive(上下文arg0,意图){
String action=intent.getAction();
if(action.equalsIgnoreCase(BluetoothDevice.action\u BOND\u STATE\u CHANGED)){
int extra=intent.getIntExtra(BluetoothDevice.extra\u BOND\u STATE,345);
Log.d(“,”>>>>额外的债券状态=“+EXTRA”);
int extra1=
intent.getIntExtra(BluetoothAdapter.EXTRA连接状态,345);
Log.d(“”,
“>>>>>>额外的连接状态=“+extra1”);
开关(额外){
机箱BluetoothDevice.BOND_BONDED:
Log.d(“Bikash----”,“bond bonded BluetoothDevice.bond_bonded”);
打破
机箱BluetoothDevice.BOND\u键合:
Log.d(“Bikash----”,“bond bonding BluetoothDevice.bond_bonding”);
打破
case BluetoothDevice.BOND_无:
Log.d(“Bikash--”和“bond NONE--BluetoothDevice.bond_NONE”);
打破
违约:
打破
}
}
如果(action.equalsIgnoreCase(
BluetoothAdapter.操作(连接状态已更改){
int extra=intent.getIntExtra(BluetoothAdapter.extra\u连接\u状态,
蓝牙适配器。状态(已断开);
蓝牙设备=
intent.getParcelableExtra(BluetoothDevice.EXTRA_设备);
Log.d(“Bikash----”,“----蓝牙A2DP设备
:“+device.getName()+”地址:“+device.getAddress());
开关(额外){
案例BluetoothAdapter.STATE_正在连接:
Log.d(“Bikash----”,“----蓝牙A2DP状态连接”);
打破
案例BluetoothAdapter.STATE_已连接:
Log.d(“Bikash----”,“----蓝牙A2DP状态已连接”);
打破
案例BluetoothAdapter.STATE\u断开连接:
Log.d(“Bikash----”,“----蓝牙A2DP状态
断开连接”);
打破
案例BluetoothAdapter.STATE_已断开:
Log.d(“Bikash----”,“----蓝牙A2DP状态断开”);
打破
违约:
Log.d(“Bikash----”,“----默认情况1”);
打破
}
}
}