蓝牙的扫描

2024-06-20 03:58
文章标签 扫描 蓝牙

本文主要是介绍蓝牙的扫描,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

上一篇 蓝牙打开和关闭

在这里插入图片描述

蓝牙扫描 返回boolean值
public boolean startDiscovery()

示例:

   /*** 开始扫描** @return*/public boolean startScan(){if (BluetoothManager.newInstance().isBlueEnable()){//如果正在扫描取消if (BluetoothManager.newInstance().getBluetoothAdapter().isDiscovering()){BluetoothManager.newInstance().getBluetoothAdapter().cancelDiscovery();}/*开始扫描蓝牙  返回true表示扫描成功  通过广播接收返回结果*/return BluetoothManager.newInstance().getBluetoothAdapter().startDiscovery();}else {Toast.makeText(getContext(),"请打开蓝牙",Toast.LENGTH_LONG).show();}return false;}

正在扫描和取消扫描
public boolean isDiscovering()
public boolean cancelDiscovery()

示例:

  /*** 取消扫描*/public void cancelScan(){if (BluetoothManager.newInstance().isBlueEnable()){if (BluetoothManager.newInstance().getBluetoothAdapter().isDiscovering()){BluetoothManager.newInstance().getBluetoothAdapter().cancelDiscovery();}}}

通过广播接收扫描的结果

  • 注册广播
 public void register(){IntentFilter filter1 = new IntentFilter(android.bluetooth.BluetoothAdapter.ACTION_DISCOVERY_STARTED);IntentFilter filter2 = new IntentFilter(android.bluetooth.BluetoothAdapter.ACTION_DISCOVERY_FINISHED);IntentFilter filter3 = new IntentFilter(BluetoothDevice.ACTION_FOUND);getContext().registerReceiver(scanBlueReceiver,filter1);getContext().registerReceiver(scanBlueReceiver,filter2);getContext().registerReceiver(scanBlueReceiver,filter3);}
  • 接收广播
String TAG= "=======bluetooth======";private ScanBlueReceiver scanBlueReceiver = new ScanBlueReceiver();public class  ScanBlueReceiver extends BroadcastReceiver{@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)@Overridepublic void onReceive(Context context, Intent intent) {String action = intent.getAction();BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
//            Log.d(TAG, "action:" + action);switch (action){case BluetoothAdapter.ACTION_DISCOVERY_STARTED:Log.d(TAG, "开始扫描...");progressBar .setVisibility(View.VISIBLE);break;case BluetoothAdapter.ACTION_DISCOVERY_FINISHED:Log.d(TAG, "结束扫描...");progressBar .setVisibility(View.GONE);break;case BluetoothDevice.ACTION_FOUND://发现设备if (!TextUtils.isEmpty(device.getName())){Log.d(TAG, "device: 蓝牙名字=" + device.getName()+",mac地址 = "+device.getAddress()+",蓝牙类型 ="+device.getType()+",绑定状态 ="+device.getBondState());list.add(device);adapter.notifyDataSetChanged();}if (scanBluetoothResult!=null){scanBluetoothResult.result(device);}break;}}}
  • 完整代码
public class DiscoveryBluetooth extends Dialog {private ProgressBar progressBar;private RecyclerView mRecyclerView ;private MyAdapter adapter;List<BluetoothDevice> list = new ArrayList<>();public DiscoveryBluetooth(Context context) {super(context,R.style.dialog);setContentView(R.layout.discovery_bluetooth_layout);progressBar=findViewById(R.id.progressBar);mRecyclerView = findViewById(R.id.mRecyclerView);LinearLayoutManager linearLayoutManager=new LinearLayoutManager(getContext());linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);mRecyclerView.addItemDecoration(new DividerItemDecoration(getContext(),DividerItemDecoration.VERTICAL));mRecyclerView.setLayoutManager(linearLayoutManager);mRecyclerView.setAdapter(adapter=new MyAdapter());findViewById(R.id.start).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {list.clear();startScan();}});findViewById(R.id.stop).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {cancelScan();}});register();Window window = getWindow();WindowManager.LayoutParams lp = window.getAttributes();lp.width = WindowManager.LayoutParams.MATCH_PARENT;lp.height = WindowManager.LayoutParams.MATCH_PARENT;window.setAttributes(lp);}/*** 开始扫描** @return*/public boolean startScan(){if (BluetoothManager.newInstance().isBlueEnable()){//如果正在扫描取消if (BluetoothManager.newInstance().getBluetoothAdapter().isDiscovering()){BluetoothManager.newInstance().getBluetoothAdapter().cancelDiscovery();}/*开始扫描蓝牙  返回true表示扫描成功  通过广播接收返回结果*/return BluetoothManager.newInstance().getBluetoothAdapter().startDiscovery();}else {Toast.makeText(getContext(),"请打开蓝牙",Toast.LENGTH_LONG).show();}return false;}/*** 取消扫描*/public void cancelScan(){if (BluetoothManager.newInstance().isBlueEnable()){if (BluetoothManager.newInstance().getBluetoothAdapter().isDiscovering()){BluetoothManager.newInstance().getBluetoothAdapter().cancelDiscovery();}}}public void register(){IntentFilter filter1 = new IntentFilter(android.bluetooth.BluetoothAdapter.ACTION_DISCOVERY_STARTED);IntentFilter filter2 = new IntentFilter(android.bluetooth.BluetoothAdapter.ACTION_DISCOVERY_FINISHED);IntentFilter filter3 = new IntentFilter(BluetoothDevice.ACTION_FOUND);getContext().registerReceiver(scanBlueReceiver,filter1);getContext().registerReceiver(scanBlueReceiver,filter2);getContext().registerReceiver(scanBlueReceiver,filter3);}String TAG= "=======bluetooth======";private ScanBlueReceiver scanBlueReceiver = new ScanBlueReceiver();public class  ScanBlueReceiver extends BroadcastReceiver{@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)@Overridepublic void onReceive(Context context, Intent intent) {String action = intent.getAction();BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
//            Log.d(TAG, "action:" + action);switch (action){case BluetoothAdapter.ACTION_DISCOVERY_STARTED:Log.d(TAG, "开始扫描...");progressBar .setVisibility(View.VISIBLE);break;case BluetoothAdapter.ACTION_DISCOVERY_FINISHED:Log.d(TAG, "结束扫描...");progressBar .setVisibility(View.GONE);break;case BluetoothDevice.ACTION_FOUND://发现设备if (!TextUtils.isEmpty(device.getName())){Log.d(TAG, "device: 蓝牙名字=" + device.getName()+",mac地址 = "+device.getAddress()+",蓝牙类型 ="+device.getType()+",绑定状态 ="+device.getBondState());list.add(device);adapter.notifyDataSetChanged();}if (scanBluetoothResult!=null){scanBluetoothResult.result(device);}break;}}}private ScanBluetoothResult scanBluetoothResult;public interface ScanBluetoothResult{void result(BluetoothDevice device);}/*** @param scanBluetoothResult* @return*/public DiscoveryBluetooth setScanBluetoothResult(ScanBluetoothResult scanBluetoothResult) {this.scanBluetoothResult = scanBluetoothResult;return this ;}public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder>{@NonNull@Overridepublic MyAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {return new MyViewHolder(LayoutInflater.from(getContext()).inflate(android.R.layout.test_list_item,viewGroup,false));}@Overridepublic int getItemCount() {return list.size();}@Overridepublic void onBindViewHolder(@NonNull MyAdapter.MyViewHolder myViewHolder,  int i) {if (list.size()>0){final BluetoothDevice d = list.get(i);myViewHolder.textView.setText(d.getName()+"-"+d.getAddress());myViewHolder.itemView.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {CreateBondBluetooth createBondBluetooth=new CreateBondBluetooth(getContext());createBondBluetooth.CreateBond(d);createBondBluetooth.show();}});}}class MyViewHolder extends RecyclerView.ViewHolder{public TextView textView;public MyViewHolder(@NonNull View itemView) {super(itemView);textView =itemView.findViewById(android.R.id.text1);textView.setTextColor(Color.BLACK);}}}
}

这篇关于蓝牙的扫描的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1077035

相关文章

电脑蓝牙连不上怎么办? 5 招教你轻松修复Mac蓝牙连接问题的技巧

《电脑蓝牙连不上怎么办?5招教你轻松修复Mac蓝牙连接问题的技巧》蓝牙连接问题是一些Mac用户经常遇到的常见问题之一,在本文章中,我们将提供一些有用的提示和技巧,帮助您解决可能出现的蓝牙连接问... 蓝牙作为一种流行的无线技术,已经成为我们连接各种设备的重要工具。在 MAC 上,你可以根据自己的需求,轻松地

Vue 调用摄像头扫描条码功能实现代码

《Vue调用摄像头扫描条码功能实现代码》本文介绍了如何使用Vue.js和jsQR库来实现调用摄像头并扫描条码的功能,通过安装依赖、获取摄像头视频流、解析条码等步骤,实现了从开始扫描到停止扫描的完整流... 目录实现步骤:代码实现1. 安装依赖2. vue 页面代码功能说明注意事项以下是一个基于 Vue.js

SQL注入漏洞扫描之sqlmap详解

《SQL注入漏洞扫描之sqlmap详解》SQLMap是一款自动执行SQL注入的审计工具,支持多种SQL注入技术,包括布尔型盲注、时间型盲注、报错型注入、联合查询注入和堆叠查询注入... 目录what支持类型how---less-1为例1.检测网站是否存在sql注入漏洞的注入点2.列举可用数据库3.列举数据库

IDEA常用插件之代码扫描SonarLint详解

《IDEA常用插件之代码扫描SonarLint详解》SonarLint是一款用于代码扫描的插件,可以帮助查找隐藏的bug,下载并安装插件后,右键点击项目并选择“Analyze”、“Analyzewit... 目录SonajavascriptrLint 查找隐藏的bug下载安装插件扫描代码查看结果总结Sona

python-nmap实现python利用nmap进行扫描分析

《python-nmap实现python利用nmap进行扫描分析》Nmap是一个非常用的网络/端口扫描工具,如果想将nmap集成进你的工具里,可以使用python-nmap这个python库,它提供了... 目录前言python-nmap的基本使用PortScanner扫描PortScannerAsync异

开放式耳机好用?平价开放式耳机推荐?四款开放式的蓝牙耳机推荐

开放式耳机好用吗?有平价些的开放式耳机推荐吗?那这两个问题的回答当然是肯定的。 首先开放式耳机好不好用取决于对耳机的需求,因为开放式耳机其实是比较适用于需要注意周围环境、需要‌长时间佩戴舒适以及需要频繁与人交流的场景中,在这些场景下使用开放式耳机的话就会比较有优势。就例如跑步骑行健身等运动的时候,能够兼得佩戴舒适度的同时,增加一定的安全性;还有在办公学习的时候,会很适合长时间佩戴,能够方便和

独立按键单击检测(延时消抖+定时器扫描)

目录 独立按键简介 按键抖动 模块接线 延时消抖 Key.h Key.c 定时器扫描按键代码 Key.h Key.c main.c 思考  MultiButton按键驱动 独立按键简介 ​ 轻触按键相当于一种电子开关,按下时开关接通,松开时开关断开,实现原理是通过轻触按键内部的金属弹片受力弹动来实现接通与断开。  ​ 按键抖动 由于按键内部使用的是机

Flutter 中的低功耗蓝牙概述

随着智能设备数量的增加,控制这些设备的需求也在增加。对于多种使用情况,期望设备在需要进行控制的同时连接到互联网会受到很大限制,因此是不可行的。在这些情况下,使用低功耗蓝牙(也称为 Bluetooth LE 或 BLE)似乎是最佳选择,因为它功耗低,在我们的手机中无处不在,而且无需连接到更广泛的网络。因此,蓝牙应用程序的需求也在不断增长。 通过阅读本文,您将了解如何开始在 Flutter 中开

三维激光扫描点云配准外业棋盘的布设与棋盘坐标测量

文章目录 一、棋盘标定板准备二、棋盘标定板布设三、棋盘标定板坐标测量 一、棋盘标定板准备 三维激光扫描棋盘是用来校准和校正激光扫描仪的重要工具,主要用于提高扫描精度。棋盘标定板通常具有以下特点: 高对比度图案:通常是黑白相间的棋盘格,便于识别。已知尺寸:每个格子的尺寸是已知的,可以用于计算比例和调整。平面标定:帮助校准相机和激光扫描仪之间的位置关系。 使用方法 扫描棋盘:

开放式蓝牙耳机哪个品牌好用?盘点五款超优秀的开放式耳机!

开放式蓝牙耳机现在挺受欢迎的,它们最大的好处就是不塞耳朵,戴着舒服,特别适合长时间佩戴。而且,这种耳机能让你在听音乐的同时,还能听到周围的环境声,这样在外面运动或者骑车的时候就更安全。音质方面,现在的开放式耳机也做得越来越好,有些高端款式还有特别的技术来减少漏音,保护你的隐私。但是现在市场上的开放式耳机品牌太多了,很多人不知道怎么选?为了帮助您在众多选项中做出选择,我根据个人经验挑选了一些表现良好