Android通过广播接收器BroadcastReceiver监听蓝牙连接变化

上一篇介绍了通过广播对蓝牙的打开关闭等变化的监听,这一篇将对蓝牙连接状态的变化进行封装。

首先是广播接收器BlueToothConnectReceiver 类

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

public class BlueToothConnectReceiver extends BroadcastReceiver {

    private OnBleConnectListener onBleConnectListener;

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
        switch (action) {
            case BluetoothDevice.ACTION_ACL_CONNECTED:
                if (onBleConnectListener != null) {
                    onBleConnectListener.onConnect(device);
                }
                Log.d("BlueToothConnect", "BroadcastReceiver蓝牙已连接:" + device.getName());
                break;
            case BluetoothDevice.ACTION_ACL_DISCONNECTED:
                //当直接关闭蓝牙时此处不会被触发,只有当蓝牙未关闭并且断开蓝牙时才会触发
                if (onBleConnectListener != null) {
                    onBleConnectListener.onDisConnect(device);
                }
                Log.d("BlueToothConnect", "BroadcastReceiver蓝牙已断开:" + device.getName());
                break;
        }
    }

    public interface OnBleConnectListener {
        void onConnect(BluetoothDevice device);

        void onDisConnect(BluetoothDevice device);
    }

    public void setOnBleConnectListener(OnBleConnectListener onBleConnectListener) {
        this.onBleConnectListener = onBleConnectListener;
    }
}

接着是封装注册和取消注册的BlueToothUtils类,依旧使用单例模式

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

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

    private static BlueToothUtils INSTANCE;
    private BlueToothStateReceiver blueToothStateReceiver;
    private BlueToothConnectReceiver blueToothConnectReceiver;

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

    //注册广播接收器,用于监听蓝牙状态变化
    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);
    }

    //注册广播接收器,用于监听蓝牙连接情况的变化
    public void registerBlueToothConnectReceiver(Activity activity) {
        blueToothConnectReceiver = new BlueToothConnectReceiver();
        IntentFilter filter_connect = new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED);
        activity.registerReceiver(blueToothConnectReceiver, filter_connect);
        IntentFilter filter_disconnect = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED);
        activity.registerReceiver(blueToothConnectReceiver, filter_disconnect);
        blueToothConnectReceiver.setOnBleConnectListener(new BlueToothConnectReceiver.OnBleConnectListener() {
            @Override
            public void onConnect(BluetoothDevice device) {

            }

            @Override
            public void onDisConnect(BluetoothDevice device) {

            }
        });
    }

    public void unregisterBlueToothConnectReceiver(Activity activity) {
        activity.unregisterReceiver(blueToothConnectReceiver);
    }

}

在Activity中注册与取消

BlueToothUtils.getInstance().registerBlueToothConnectReceiver(this);//注册广播
BlueToothUtils.getInstance().unregisterBlueToothConnectReceiver(this);//取消注册广播

AndroidManifest.xml中添加权限

    
    

蓝牙打开关闭状态变化传送门:

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

你可能感兴趣的:(Android通过广播接收器BroadcastReceiver监听蓝牙连接变化)