蓝牙开关的监听

蓝牙权限 <uses-permission android:name="android.permission.BLUETOOTH" />

1、监听手机本身蓝牙状态的广播

手机蓝牙开启关闭时发送

action: BluetoothAdapter.ACTION_STATE_CHANGED

复制代码
if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
    int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,
            BluetoothAdapter.ERROR);
    switch (state) {
        case BluetoothAdapter.STATE_OFF:
            Log.d("aaa", "STATE_OFF 手机蓝牙关闭");
            break;
        case BluetoothAdapter.STATE_TURNING_OFF:

            Log.d("aaa", "STATE_TURNING_OFF 手机蓝牙正在关闭");
            break;
        case BluetoothAdapter.STATE_ON:
            Log.d("aaa", "STATE_ON 手机蓝牙开启");
            break;
        case BluetoothAdapter.STATE_TURNING_ON:
            Log.d("aaa", "STATE_TURNING_ON 手机蓝牙正在开启");
            break;
    }
}
复制代码

2、监听蓝牙设备配对状态的广播

蓝牙设备配对和解除配对时发送

action: BluetoothDevice.ACTION_BOND_STATE_CHANGED

复制代码
if (action.equals(BluetoothDevice.ACTION_BOND_STATE_CHANGED)) {
    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
    String name = device.getName();
    Log.d("aaa", "device name: " + name);
    int state = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, -1);
    switch (state) {
        case BluetoothDevice.BOND_NONE:
            Log.d("aaa", "BOND_NONE 删除配对");
            break;
        case BluetoothDevice.BOND_BONDING:
            Log.d("aaa", "BOND_BONDING 正在配对");
            break;
        case BluetoothDevice.BOND_BONDED:
            Log.d("aaa", "BOND_BONDED 配对成功");
            break;
    }
} 
复制代码

3、监听蓝牙设备连接和连接断开的广播

蓝牙设备连接上和断开连接时发送, 这两个监听的是底层的连接状态

action: BluetoothDevice.ACTION_ACL_CONNECTED   BluetoothDevice.ACTION_ACL_DISCONNECTED

复制代码
if (action.equals(BluetoothDevice.ACTION_ACL_CONNECTED)) {
    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
    Log.d("aaa", device.getName() + " ACTION_ACL_CONNECTED");
} else if (action.equals(BluetoothDevice.ACTION_ACL_DISCONNECTED)) {
    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
    Log.d("aaa", device.getName() + " ACTION_ACL_DISCONNECTED");
}
复制代码

BluetoothClass 可以获取蓝牙设备的类型

如果想获取当前已连接上的所有蓝牙设备,可以在这两个广播中手动维护一个连接设备的列表。

像下面这样:

复制代码
/**
 * 记录当前正在连接的所有蓝牙输入设备
 */
public List<BluetoothDevice> connectedBluetoothDevices = new ArrayList<BluetoothDevice>();

if (intent.getAction().equals(BluetoothDevice.ACTION_ACL_CONNECTED)) {
    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
    if (isInputDevice(device)) {
        List<BluetoothDevice> connectedBluetoothDevices = ((MyApplication) getApplication()).connectedBluetoothDevices;
        if (!connectedBluetoothDevices.contains(device)) {
            connectedBluetoothDevices.add(device);
        }
    }
} else if (intent.getAction().equals(BluetoothDevice.ACTION_ACL_DISCONNECTED)) {
    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
    if (isInputDevice(device)) {
        List<BluetoothDevice> connectedBluetoothDevices = ((MyApplication) getApplication()).connectedBluetoothDevices;
        connectedBluetoothDevices.remove(device);
    }
}

/**
 * 判断蓝牙设备是否是输入设备,这里认为 PERIPHERAL是输入设备
 */
private boolean isInputDevice(BluetoothDevice device) {
    int deviceMajorClass = device.getBluetoothClass().getMajorDeviceClass();
    if (deviceMajorClass == BluetoothClass.Device.Major.PERIPHERAL) {
        return true;
    }

    return false;
}




发一下我写的
 
 
 
 

private void registerBoradcastReceiver() {
    //注册监听
    getApplication().registerReceiver(stateChangeReceiver, makeFilter());
}

private IntentFilter makeFilter() {
    IntentFilter filter = new IntentFilter();
    filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
    return filter;
}

private BroadcastReceiver stateChangeReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("蓝牙", "" + intent.getAction());
        switch (intent.getAction()) {
            case BluetoothAdapter.ACTION_STATE_CHANGED:
                int blueState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 0);
                Log.d("蓝牙", "蓝牙" + blueState);
                switch (blueState) {
                    case BluetoothAdapter.STATE_TURNING_ON:
                        Log.d("STATE_TURNING_ON", "蓝牙");
                        break;
                    case BluetoothAdapter.STATE_ON:
                        Log.d("STATE_ON", "蓝牙");
                        break;
                    case BluetoothAdapter.STATE_TURNING_OFF:
                        Log.d("STATE_TURNING_OFF", "蓝牙");
                        //蓝牙开关手动关闭 会收到这条广播
                        break;
                    case BluetoothAdapter.STATE_OFF:
                        Log.d("STATE_OFF", "蓝牙");
                        break;
                }
                break;
        }
    }
};








  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值