android中将蓝牙设置为始终可发现模式

BlueAlwaysDiscoverableReceiver.java

java 复制代码
package com.ks.xybase.receiver;

import android.bluetooth.BluetoothAdapter;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;


import com.ks.xybase.utils.L;

import java.lang.reflect.Method;
import java.util.Objects;

//蓝牙设置为始终可发现模式
public class BlueAlwaysDiscoverableReceiver extends BroadcastReceiver {
    private static final String TAG = "BlueAlwaysDiscoverableR";
    private Context mContext;
    private BluetoothAdapter mBluetoothAdapter;
    private IntentFilter mIntentFilter;

    public BlueAlwaysDiscoverableReceiver(Context context) {
        mContext = context;
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        mIntentFilter = new IntentFilter();
        mIntentFilter.addAction(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);
    }

    public void start() {
        mContext.registerReceiver(this, mIntentFilter);
        setBlueCanVisible();
    }

    public void stop() {
        mContext.unregisterReceiver(this);
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (!Objects.equals(action, BluetoothAdapter.ACTION_SCAN_MODE_CHANGED)) {
            return;
        }
        setBlueCanVisible();
    }

    private void setBlueCanVisible() {
        try {
            Method setScanModeMethod = BluetoothAdapter.class.getMethod("setScanMode", int.class);
            setScanModeMethod.setAccessible(true);
            setScanModeMethod.invoke(mBluetoothAdapter, BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE);
        } catch (Exception e) {
            L.d(TAG, "error when setBlueCanVisible,e==" + e.getMessage());
        }
    }
}

然后在activity中

java 复制代码
private BlueAlwaysDiscoverableReceiver mBlueAlwaysDiscoverableReceiver;

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //设置蓝牙为始终被发现模式
        mBlueAlwaysDiscoverableReceiver = new BlueAlwaysDiscoverableReceiver(this);
        mBlueAlwaysDiscoverableReceiver.start();
    }

 @Override
    protected void onDestroy() {
        mBlueAlwaysDiscoverableReceiver.stop();
        super.onDestroy();
    }
相关推荐
qq_25183645721 分钟前
springboot vue3 开发实现 拼豆管理系统
java·开发语言·ai编程
郑州光合科技余经理1 小时前
同城外卖小程序开发:下单成功后,后台导出能不能对上用户端状态
开发语言·前端·git·后端·uni-app·php·ai编程
知识分享小能手1 小时前
C++ 学习教程,从入门到精通,C++ 入门知识 — 完整知识点(1)
开发语言·c++·学习
+VX:Fegn08952 小时前
计算机毕业设计|基于springboot + vue旅游管理系统(源码+数据库+文档)
java·开发语言·vue.js·spring boot·课程设计
阿里嘎多学长2 小时前
2026-09-20 GitHub 热点项目精选
开发语言·程序员·github·代码托管
hai_android3 小时前
Kotlin / Android 常用函数使用示例手册
android·java·kotlin
hai_android4 小时前
Android MeasureSpec 详解
android·java·kotlin
weixin_307779134 小时前
C++代码实现MATLAB中的ode23tb函数功能
开发语言·c++·算法·matlab
2601_966949654 小时前
为什么量化回测中 for 循环比 DataFrame 慢三个数量级?从向量化原理讲起
开发语言·python·pandas·股票数据·quantdash
又见情义5 小时前
RK3568 Android 13 本地 U 盘 OTA 升级实战:基于 RKUpdateService 的完整流程
android