Android获取蓝牙列表

简介: Android获取蓝牙列表

注意:申请蓝牙权限,有的需要动态申请,否则将没有权限搜索

<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
    <uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

代码如下:

package com.example.traniningtask_26.bluetooth;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import com.example.traniningtask_26.bean.DeviceBean;
import java.util.ArrayList;
/**
 * @ClassName BluetoothUtils
 * @Description TODO
 * @Author shufeng.jiang
 * @Date 2022/4/24 11:02
 */
public class BluetoothUtils {
    final String TAG = getClass().getName();
    Context context;
    private static BluetoothUtils bluetoothInstance;
    private BluetoothAdapter bluetoothAdapter ;
    private BluetoothInterface bluetoothInterface;
    private BluetoothUtils (){}
    private String dev_mac_adress = "";
    ArrayList<DeviceBean> deviceBeans = new ArrayList<>();
    public static BluetoothUtils getInstance() {
        if (bluetoothInstance == null) {
            bluetoothInstance = new BluetoothUtils();
        }
        return bluetoothInstance;
    }
    public void initBluetooth(Context context){
        this.context = context;
        bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        registerBroadcas(context);
    }
    public  void setBluetoothListener(BluetoothInterface bluetoothInterface){
        this.bluetoothInterface = bluetoothInterface;
    }
    private void registerBroadcas(Context context){
        IntentFilter intent = new IntentFilter();
        intent.addAction(BluetoothDevice.ACTION_FOUND);
        intent.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
        intent.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
//        intent.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);//状态改变
//        intent.addAction(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);//行动扫描模式改变了
//        intent.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);//动作状态发生了变化
        context.registerReceiver(bluetoothBroadcast, intent);
        Log.i(TAG,"registerReceiver");
    }
    BroadcastReceiver bluetoothBroadcast = new BroadcastReceiver(){
        @Override
        public void onReceive(Context context, Intent intent) {
            if (BluetoothDevice.ACTION_FOUND.equals(intent.getAction())) {
                /* 搜索结果 */
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                //dev_mac_adress.contains(device.getAddress())避免重复添加
                if( device.getName() != null && !dev_mac_adress.contains(device.getAddress())){
                    deviceBeans.add(new DeviceBean(device.getName(),device.getAddress()));
                    dev_mac_adress += device.getAddress();
                    Log.i(TAG,device.getName()+":"+device.getAddress());
                }
            }else if(BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(intent.getAction())){
                //正在搜索
            }else if(BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(intent.getAction())){
                // 搜索完成
                dev_mac_adress = "";
                bluetoothInterface.getBluetoothList(deviceBeans);
                deviceBeans.clear();
            }
        }
    };
    /** 开启蓝牙 */
    public void enable(){
        if (bluetoothAdapter !=null && !bluetoothAdapter.isEnabled()){
            bluetoothAdapter.enable();
        }
    }
    /** 关闭蓝牙 */
    public void disable(){
        if (bluetoothAdapter !=null && bluetoothAdapter.isEnabled()){
            bluetoothAdapter.disable();
        }
    }
    /** 取消搜索 */
    public void cancelDiscovery(){
        if(isDiscovering()){
            bluetoothAdapter.cancelDiscovery();
        }
    }
    /** 开始搜索 */
    public void startDiscovery(){
        if (bluetoothAdapter !=null && bluetoothAdapter.isEnabled()){
            bluetoothAdapter.startDiscovery();
        }
    }
    /** 判断蓝牙是否打开 */
    public boolean isEnabled(){
        if (bluetoothAdapter !=null){
            return bluetoothAdapter.isEnabled();
        }
        return false;
    }
    /** 判断当前是否正在查找设备,是返回true */
    public boolean isDiscovering(){
        if (bluetoothAdapter !=null){
            return bluetoothAdapter.isDiscovering();
        }
        return false;
    }
    public void onDestroy(){
        context.unregisterReceiver(bluetoothBroadcast);
    }
    public interface BluetoothInterface{
        /* 获取蓝牙列表 */
        void getBluetoothList(ArrayList<DeviceBean> deviceBeans);
    }
}

在 Activity 实现接口 BluetoothUtils.BluetoothInterface,然后在进行初始化:

BluetoothUtils.getInstance().initBluetooth(this);
 BluetoothUtils.getInstance().setBluetoothListener(this);


相关文章
|
存储 消息中间件 人工智能
【04】AI辅助编程完整的安卓二次商业实战-寻找修改替换新UI首页图标-菜单图标-消息列表图标-优雅草伊凡
【04】AI辅助编程完整的安卓二次商业实战-寻找修改替换新UI首页图标-菜单图标-消息列表图标-优雅草伊凡
774 4
|
存储 数据库 Android开发
安卓Jetpack Compose+Kotlin,支持从本地添加音频文件到播放列表,支持删除,使用ExoPlayer播放音乐
为了在UI界面添加用于添加和删除本地音乐文件的按钮,以及相关的播放功能,你需要实现以下几个步骤: 1. **集成用户选择本地音乐**:允许用户从设备中选择音乐文件。 2. **创建UI按钮**:在界面中创建添加和删除按钮。 3. **数据库功能**:使用Room数据库来存储音频文件信息。 4. **更新ViewModel**:处理添加、删除和播放音频文件的逻辑。 5. **UI实现**:在UI层支持添加、删除音乐以及播放功能。
|
Dart 前端开发 Android开发
【09】flutter首页进行了完善-采用android studio 进行真机调试开发-增加了直播间列表和短视频人物列表-增加了用户中心-卓伊凡换人优雅草Alex-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
【09】flutter首页进行了完善-采用android studio 进行真机调试开发-增加了直播间列表和短视频人物列表-增加了用户中心-卓伊凡换人优雅草Alex-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
794 4
【09】flutter首页进行了完善-采用android studio 进行真机调试开发-增加了直播间列表和短视频人物列表-增加了用户中心-卓伊凡换人优雅草Alex-开发完整的社交APP-前端客户端开发+数据联调|以优雅草商业项目为例做开发-flutter开发-全流程-商业应用级实战开发-优雅草Alex
|
存储 监控 API
app开发之安卓Android+苹果ios打包所有权限对应解释列表【长期更新】-以及默认打包自动添加权限列表和简化后的基本打包权限列表以uniapp为例-优雅草央千澈
app开发之安卓Android+苹果ios打包所有权限对应解释列表【长期更新】-以及默认打包自动添加权限列表和简化后的基本打包权限列表以uniapp为例-优雅草央千澈
2178 11
|
Android开发
Android 配置蓝牙遥控器键值
本文详细介绍了Android系统中配置蓝牙遥控器键值的步骤,包括查看设备号、配置键位映射文件(kl文件)、部署kl文件以及调试过程,确保蓝牙遥控器的按键能正确映射到Android系统对应的按键功能。
4341 1
|
前端开发 API Android开发
25. 【Android教程】列表控件 ListView
25. 【Android教程】列表控件 ListView
1171 3
|
存储 API Android开发
29. 【Android教程】折叠列表 ExpandableListView
29. 【Android教程】折叠列表 ExpandableListView
1650 2
|
Android开发
Android获取蓝牙设备列表的方法
Android获取蓝牙设备列表的方法
1347 5
|
Android开发 开发者 UED
Android项目架构设计问题之加载数据到列表如何解决
Android项目架构设计问题之加载数据到列表如何解决
237 0
|
安全 Java API
Android获取Wi-Fi网络列表
【6月更文挑战第21天】
809 0

热门文章

最新文章