Android 广播接收器蓝牙事件捕捉

Android Broadcast Receiver bluetooth events catching

我正在尝试使用广播接收器捕捉蓝牙状态变化。

我的清单:

<uses-permission android:name="android.permission.BLUETOOTH" />
<application>
     <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver android:name=".BluetoothBroadcastReceiver"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.bluetooth.adapter.action.STATE_CHANGED" />
            <action android:name="android.bluetooth.adapter.action.CONNECTION_STATE_CHANGED" />
            <action android:name="android.bluetooth.device.action.ACL_CONNECTED" />
            <action android:name="android.bluetooth.device.action.ACL_DISCONNECTED" />
        </intent-filter>
    </receiver>
</application>

接收者onReceive方法:

public void onReceive(Context context, Intent intent) {

    String action = intent.getAction();
    Log.d("BroadcastActions", "Action "+action+"received");
    int state;
    BluetoothDevice bluetoothDevice;

    switch(action)
    {
        case BluetoothAdapter.ACTION_STATE_CHANGED:
            state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1);
            if (state == BluetoothAdapter.STATE_OFF)
            {
                Toast.makeText(context, "Bluetooth is off", Toast.LENGTH_SHORT).show();
                Log.d("BroadcastActions", "Bluetooth is off");
            }
            else if (state == BluetoothAdapter.STATE_TURNING_OFF)
            {
                Toast.makeText(context, "Bluetooth is turning off", Toast.LENGTH_SHORT).show();
                Log.d("BroadcastActions", "Bluetooth is turning off");
            }
            else if(state == BluetoothAdapter.STATE_ON)
            {
                Log.d("BroadcastActions", "Bluetooth is on");
            }
            break;

        case BluetoothDevice.ACTION_ACL_CONNECTED:
            bluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            Toast.makeText(context, "Connected to "+bluetoothDevice.getName(),
                    Toast.LENGTH_SHORT).show();
            Log.d("BroadcastActions", "Connected to "+bluetoothDevice.getName());
            break;

        case BluetoothDevice.ACTION_ACL_DISCONNECTED:
            bluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            Toast.makeText(context, "Disconnected from "+bluetoothDevice.getName(),
                    Toast.LENGTH_SHORT).show();
            break;
    }
}

我启动应用程序,然后按主页按钮将其最小化。转到设置并打开蓝牙但没有任何反应。虽然我期待吐司和 logcat 消息。这里出了什么问题?

为了捕捉蓝牙状态变化(STATE_OFFSTATE_TURNING_ONSTATE_ONSTATE_TURNING_OFF),执行此操作:

首先,为您的 AndroidManifest 文件添加蓝牙权限:

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

在您的 Activity 或服务中创建 BroadcastReceiver:

    private final BroadcastReceiver mBroadcastReceiver1 = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        final String action = intent.getAction();

        if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
            final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
            switch(state) {
                case BluetoothAdapter.STATE_OFF:
                    ..
                    break;
                case BluetoothAdapter.STATE_TURNING_OFF:
                    ..
                    break;
                case BluetoothAdapter.STATE_ON:
                    ..
                    break;
                case BluetoothAdapter.STATE_TURNING_ON:
                    ..
                    break;
            }

        }
    }
};

创建一个 IntentFilter 并在您的 Activity/Service 的 onCreate() 方法中向 BroadcastReceiver 注册它:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    IntentFilter filter1 = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
    registerReceiver(mBroadcastReceiver1, filter1);
    
    ...
}

在您的 onDestroy() 方法中注销 BroadcastReceiver:

@Override
protected void onDestroy() {
    super.onDestroy();

    unregisterReceiver(mBroadcastReceiver1);
}

为了捕获设备可发现性的变化(SCAN_MODE_NONESCAN_MODE_CONNECTABLESCAN_MODE_CONNECTABLE_DISCOVERABLE),创建另一个 BroadcastReceiver 和 register/unregister 到您的 Activity正如我上面提到的。这些 BroadcastReceivers 之间的唯一区别是第一个使用 BluetoothAdapter.EXTRA_STATE 而另一个使用 BluetoothAdapter.EXTRA_SCAN_MODE。以下是 BroadcastReceiver 捕获可发现性更改的示例代码:

创建一个 IntentFilter 并在 onCreate() 方法中注册它:

IntentFilter filter2 = new IntentFilter();
filter2.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
filter2.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
filter2.addAction(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);
registerReceiver(mBroadcastReceiver2, filter2);

在 Activity/Service 中创建 BroadcastReciver 以捕获可发现性更改:

    private final BroadcastReceiver mBroadcastReceiver2 = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        final String action = intent.getAction();

        if(action.equals(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED)) {

            int mode = intent.getIntExtra(BluetoothAdapter.EXTRA_SCAN_MODE, BluetoothAdapter.ERROR);

            switch(mode){
                case BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE:
                    ..
                    break;
                case BluetoothAdapter.SCAN_MODE_CONNECTABLE:
                    ..
                    break;
                case BluetoothAdapter.SCAN_MODE_NONE:
                    ..
                    break;
            }
        }
    }
};

最后在 onDestroy() 中注销 BroadcastReciver:

unregisterReceiver(mBroadcastReceiver2);

请注意,您无需在 AndroidManifest 文件中添加任何 <intent-filter><receiver>,当然您需要添加蓝牙权限。

如果你想赶上(ACTION_ACL_CONNECTED,ACTION_ACL_DISCONNECTED,ACTION_ACL_DISCONNECT_REQUESTED),现在你需要在你的AndroidManifest文件中添加一个<intent-filter>

<intent-filter>
    <action android:name="android.bluetooth.device.action.ACL_CONNECTED" />
    <action android:name="android.bluetooth.device.action.ACL_DISCONNECTED" />
</intent-filter>

创建过滤器并在 onCreate() 方法中注册它:

IntentFilter filter3 = new IntentFilter();
filter3.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
filter3.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
registerReceiver(mBroadcastReceiver3, filter3);

然后在 Activity/Service:

中创建 BroadcastReceiver
    private final BroadcastReceiver mBroadcastReceiver3 = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();

        switch (action){
            case BluetoothDevice.ACTION_ACL_CONNECTED:
                ..
                break;
            case BluetoothDevice.ACTION_ACL_DISCONNECTED:
                ..
                break;
        }
    }
};

最后,取消注册:

unregisterReceiver(mBroadcastReceiver3);

如果您想阅读更多关于状态常量的信息,这来自文档:

public static final String EXTRA_STATE:

Used as an int extra field in ACTION_STATE_CHANGED intents to request the current power state. Possible values are: STATE_OFF, STATE_TURNING_ON, STATE_ON, STATE_TURNING_OFF

public static final String EXTRA_SCAN_MODE:

Used as an int extra field in ACTION_SCAN_MODE_CHANGED intents to request the current scan mode. Possible values are: SCAN_MODE_NONE, SCAN_MODE_CONNECTABLE, SCAN_MODE_CONNECTABLE_DISCOVERABLE

我一直在尝试复制相同的但未能在这方面取得成功。

这是我的 class 文件

class BluetoothCheckActivity : AppCompatActivity() {
private val bluetoothBroadcastReceiver: BroadcastReceiver = object : BroadcastReceiver() {
    override fun onReceive(context: Context?, intent: Intent?) {
        val action: String? = intent?.action
        if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
            val state: Int? =
                intent?.getIntExtra(
                    BluetoothAdapter.EXTRA_STATE,
                    BluetoothAdapter.ERROR
                )
            when (state) {
                BluetoothAdapter.STATE_ON -> tv_bluetoothCheck.text = "Bluetooth is On"
                BluetoothAdapter.STATE_TURNING_ON -> tv_bluetoothCheck.text =
                    "Bluetooth is Turning ON"
                BluetoothAdapter.STATE_OFF -> tv_bluetoothCheck.text =
                    "Bluetooth is Off"
                BluetoothAdapter.STATE_TURNING_OFF -> tv_bluetoothCheck.text =
                    "Bluetooth is Turning Off"
            }
        }
    }
}

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_bluetooth_check)
    val filter = IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED)
    try {
        registerReceiver(bluetoothBroadcastReceiver, filter)
    } catch (e: Exception) {
        e.printStackTrace()
    }
}
override fun onDestroy() {
    unregisterReceiver(bluetoothBroadcastReceiver)
    super.onDestroy()
}}