天天看点

Android通过广播接收器BroadcastReceiver监听蓝牙打开关闭状态变化

实际开发过程中需要通过对蓝牙状态的监听以满足需求,这里对蓝牙状态的改变进行了一次封装。

这里运用了动态广播注册,优点是注册取消可以由代码控制,在用到的地方注册,用不到的地方及时取消,避免对手机资源的浪费。

广播接收器BlueToothStateReceiver类

import android.bluetooth.BluetoothAdapter;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

/**
 * 广播监听蓝牙状态
 */
public class BlueToothStateReceiver extends BroadcastReceiver {
    public static int DEFAULT_VALUE_BULUETOOTH = 1000;
    public OnBlueToothStateListener onBlueToothStateListener;

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
            int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, DEFAULT_VALUE_BULUETOOTH);
            switch (state) {
                case BluetoothAdapter.STATE_OFF://蓝牙已关闭
                    onBlueToothStateListener.onStateOff();
                    break;
                case BluetoothAdapter.STATE_ON://蓝牙已开启
                    onBlueToothStateListener.onStateOn();
                    break;
                case BluetoothAdapter.STATE_TURNING_ON://蓝牙正在打开
                    onBlueToothStateListener.onStateTurningOn();
                    break;
                case BluetoothAdapter.STATE_TURNING_OFF://蓝牙正在关闭
                    onBlueToothStateListener.onStateTurningOff();
                    break;
                default:
                    Log.e("BlueToothError", "蓝牙状态未知");
            }
        }
    }

    public interface OnBlueToothStateListener {
        void onStateOff();

        void onStateOn();

        void onStateTurningOn();

        void onStateTurningOff();
    }

    public void setOnBlueToothStateListener(OnBlueToothStateListener onBlueToothStateListener) {
        this.onBlueToothStateListener = onBlueToothStateListener;
    }

}
           

接下来对调用工具类BlueToothUtils进行封装,避免重复实例化,这里使用单例模式。

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.IntentFilter;

/**
 * @author wang songbo
 */
public class BlueToothUtils {

    private static BlueToothUtils INSTANCE;

    public static synchronized BlueToothUtils getInstance() {
        if (INSTANCE == null) {
            INSTANCE = new BlueToothUtils();
        }
        return INSTANCE;
    }

    BlueToothStateReceiver blueToothStateReceiver;

    //注册广播接收器,用于监听蓝牙状态变化
    public void registerBlueToothStateReceiver(Activity activity) {
        //注册广播,蓝牙状态监听
        blueToothStateReceiver = new BlueToothStateReceiver();
        IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
        activity.registerReceiver(blueToothStateReceiver, filter);
        blueToothStateReceiver.setOnBlueToothStateListener(new BlueToothStateReceiver.OnBlueToothStateListener() {
            @Override
            public void onStateOff() {
                //do something
            }

            @Override
            public void onStateOn() {
                //do something
            }

            @Override
            public void onStateTurningOn() {
                //do something
            }

            @Override
            public void onStateTurningOff() {
                //do something
            }
        });
    }

    public void unregisterBlueToothStateReceiver(Activity activity) {
        activity.unregisterReceiver(blueToothStateReceiver);
    }
}
           

在Activity中注册广播接收器

BlueToothUtils.getInstance().registerBlueToothStateReceiver(this);
           

在Activity中取消广播接收器注册

​BlueToothUtils.getInstance().unregisterBlueToothStateReceiver(this);
           

这个广播接收器只是获取了蓝牙状态的变化,并不能判断蓝牙连接情况。将在下一篇写出监听蓝牙连接状态的广播接收器封装类。

注:AndroidMainfests.xml文件中需加入蓝牙权限:

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

蓝牙连接状态变化监听传送门:

https://blog.csdn.net/u010161303/article/details/88842804