前言:

之前介绍过很多蓝牙beacon、搜索、连接、通讯的文章。不过最近我发现:之前写的蓝牙广播包搜索的工程,搜索频率太慢,而且不能一直保持搜索状态。因此,这里探讨下高频蓝牙广播包扫描 —— 蓝牙BLE扫描。

注: 本文将从对比之前慢的和现在快的两个工程进行展开


1、初始化-onCreate

新的:

  1. // Get the local Bluetooth adapter
  2. // Initializes Bluetooth adapter.
  3. final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
  4. mBluetoothAdapter = bluetoothManager.getAdapter();
  5. // Ensures Bluetooth is available on the device and it is enabled. If not,
  6. // displays a dialog requesting user permission to enable Bluetooth.
  7. if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
  8. Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
  9. startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
  10. }

老的:

  1. // Register for broadcasts when a device is discovered
  2. IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
  3. this.registerReceiver(mReceiver, filter);
  4. // Register for broadcasts when discovery has finished
  5. filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
  6. this.registerReceiver(mReceiver, filter);
  7. // Get the local Bluetooth adapter
  8. mBtAdapter = BluetoothAdapter.getDefaultAdapter();

可见:老的是通过注册广播过滤条件BluetoothDevice.ACTION_FOUNDBluetoothAdapter.ACTION_DISCOVERY_FINISHED,来实现监听蓝牙设备扫描的发现和停止扫描事件。而mReceiver则是回调函数,接下来会介绍;新的暂时看不出啥头绪,仅仅获得bluetoothManagermBluetoothAdapter,接下来会用到。


2、开始扫描-doDiscovery

新的:

  1. // Start device discover with the BluetoothAdapter
  2. private void doDiscovery() {
  3. // If we're already discovering, stop it
  4. if (mBluetoothAdapter.isDiscovering()) {
  5. mBluetoothAdapter.stopLeScan(mLeScanCallback);
  6. }
  7. // Request discover from BluetoothAdapter
  8. //use filter not work!!!!!!!!!!
  9. //UUID[] uuid_arrays = new UUID[1];
  10. //uuid_arrays[0] = ParcelUuid.fromString(UUID_SERVICE).getUuid();
  11. //mBluetoothAdapter.startLeScan(uuid_arrays,mLeScanCallback);
  12. //Log.d("RSSI",uuid_arrays[0].toString() + " " + UUID.randomUUID().toString());
  13. mBluetoothAdapter.startLeScan(mLeScanCallback);
  14. }

老的:

  1. // Start device discover with the BluetoothAdapter
  2. private void doDiscovery() {
  3. // If we're already discovering, stop it
  4. if (mBtAdapter.isDiscovering()) {
  5. mBtAdapter.cancelDiscovery();
  6. }
  7. // Request discover from BluetoothAdapter
  8. mBtAdapter.startDiscovery();
  9. }

可见:区别在于一个是BLE操作、一个是普通蓝牙操作。


3、监听

新的:

  1. // Device scan callback.
  2. private BluetoothAdapter.LeScanCallback mLeScanCallback =
  3. new BluetoothAdapter.LeScanCallback() {
  4. @Override
  5. public void onLeScan(final BluetoothDevice device, int rssi,
  6. byte[] scanRecord) {
  7. runOnUiThread(new Runnable() {
  8. @Override
  9. public void run() {
  10. if(device_filter(device)){
  11. //mDevicesNameVector.add(device.getName());
  12. //mDevicesAddrVector.add(device.getAddress());
  13. //mRSSIVector.add((short)rssi);
  14. Log.d("RSSI",device.getAddress() + " " + device.getName() + " " + String.valueOf(rssi));
  15. ...
  16. }
  17. }
  18. });
  19. }
  20. };

老的:

  1. // The BroadcastReceiver that listens for discovered devices and
  2. // changes the title when discovery is finished
  3. //【查找蓝牙设备】
  4. private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
  5. @Override
  6. public void onReceive(Context context, Intent intent) {
  7. Log.d("onReceive","OK");
  8. String action = intent.getAction();
  9. // When discovery finds a device
  10. if (BluetoothDevice.ACTION_FOUND.equals(action)) {
  11. // Get the BluetoothDevice object from the Intent
  12. BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
  13. mDevicesNameVector.add(device.getName());
  14. mDevicesAddrVector.add(device.getAddress());
  15. short rssi = intent.getExtras().getShort(BluetoothDevice.EXTRA_RSSI);
  16. mRSSIVector.add(rssi);
  17. Log.d("RSSI",device.getName()+" "+String.valueOf(rssi));
  18. // When discovery is finished, change the Activity title
  19. } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
  20. setProgressBarIndeterminateVisibility(false);
  21. if (mDevicesNameVector.size() != 0) {
  22. Message msg = new Message();//消息
  23. Bundle bundle = new Bundle();
  24. bundle.clear();Log.d("onReceive","1");
  25. msg.what = 0x01;//消息类别
  26. bundle.putShort("msg",(short) 0);Log.d("onReceive","2");
  27. msg.setData(bundle);Log.d("onReceive","3");
  28. myHandler.sendMessage(msg);Log.d("onReceive","4");
  29. }
  30. }
  31. }
  32. };

可见:新的相对比较简单、可以持续不断的扫描获取(同一个设备会被不断的扫描到);老的则分为两步:第一步是每次扫描到一次新设备都会有一个FOUND事件、最后停止扫描了还有个FINISH事件,这里我在FINISH事件结束时发出一个msg来通知进行其他操作。


4、权限文件配置

新的:

  1. <uses-permission a:name="android.permission.ACCESS_FINE_LOCATION"/>
  2. <uses-permission a:name="android.permission.ACCESS_COARSE_LOCATION"/>
  3. <uses-permission a:name="android.permission.BLUETOOTH"/>
  4. <uses-permission a:name="android.permission.BLUETOOTH_ADMIN"/>
  5. <uses-feature a:name="android.hardware.bluetooth_le" a:required="true"/>

老的:

  1. <uses-permission a:name="android.permission.BLUETOOTH" />
  2. <uses-permission a:name="android.permission.BLUETOOTH_ADMIN" />
  3. <uses-permission a:name="android.permission.ACCESS_COARSE_LOCATION" />

可见: 相差不大,新的比老的多了bluetooth_le说明。


5、最后说明

当你尝试使用BLE SCAN之后,你会感觉有一种飞一般的感觉,几乎同一个设备每一秒都会被扫描到多次。拿这些高频扫描的大量数据,就可以做类似beacon、距离估算、定位等小应用了!效果会比老的scan方法要好很多~


LINKS

[1]. 本项目GITHUB链接地址

[2]. 在Linux下搭建安卓APP的开发烧写环境(makefile版)—— 在Linux上用命令行+VIM开发安卓APP

[3]. android developer TextView

[4]. android developer Vector

[5]. android developer String

[6]. android developer Formatter

[7]. android developer Matcher

[8]. android developer Pattern

[9]. 等宽字体-Android 设置字体的三种方法(TypeFace)

[10]. Android 设置TextView滑动滚动条和滑动效果


  1. @beautifulzzzz
  2. 智能硬件、物联网,热爱技术,关注产品
  3. 博客:http://blog.beautifulzzzz.com
  4. 园友交流群:414948975

[安卓] 20、基于蓝牙BLE的广播包高频快速搜索的更多相关文章

  1. 玩转BLE(2)_使用bluepy扫描BLE的广播数据

    1. 前言 在linux平台下,bluez是一个很不错的软件,提供了很多基于命令行的测试工具,如hciconfig.hcitool.hcidump.bluetoothctl等.利用这些工具,我们可以方 ...

  2. [蓝牙] 2、蓝牙BLE协议及架构浅析&&基于广播超时待机说广播事件

    第一章 BLE基本概念了解 一.蓝牙4.0和BLE区别   蓝牙4.0是一种应用非常广泛.基于2.4G射频的低功耗无线通讯技术.蓝牙低功耗(Bluetooth Low Energy ),人们又常称之为 ...

  3. 蓝牙4.0 BLE 广播包解析

    在使用EN-Dongle捕获和解析广播包之前,我们先了解一下BLE报文的结构,之后,再对捕获的广播包进行分析.在学习BLE的时候,下面两个文档是极其重要的,这是SIG发布的蓝牙的核心协议和核心协议增补 ...

  4. 一分钟读懂低功耗蓝牙(BLE)连接数据包

    一分钟读懂低功耗蓝牙(BLE)连接数据包 1.概述 BLE 连接过程中有三个重要的数据包:SCAN_REQ, SCAN_RSP 和 CONNECT_REQ. SCAN_REQ: 扫描请求,由主设备(M ...

  5. 【转】蓝牙ble app开发(三) -- 抓包

    原文网址:http://blog.csdn.net/lckj686/article/details/43156617 关于android 蓝牙app开发抓包的重要性在 android 蓝牙ble ap ...

  6. 蓝牙4.0BLE抓包(二) – 广播包解析

    版权声明:本文为博主原创文章,转载请注明作者和出处.    作者:强光手电[艾克姆科技-无线事业部] 在使用EN-Dongle捕获和解析广播包之前,我们先了解一下BLE报文的结构,之后,再对捕获的广播 ...

  7. 蓝牙BLE数据包格式汇总

    以蓝牙4.0为例说明: BLE包格式有:广播包.扫描包.初始化连接包.链路层控制包(LL层数据包).逻辑链路控制和自适应协议数据包(即L2CAP数据包)等: 其中广播包又分为:定向广播包和非定向广播包 ...

  8. 蓝牙BLE: 蓝牙4.0 BLE广播数据解析(转)

    BLE 设备工作的第一步就是向外广播数据.广播数据中带有设备相关的信息.本文主要说一下 BLE 的广播中的数据的规范以及广播包的解析. 1. 广播模式 BLE 中有两种角色 Central 和 Per ...

  9. 微信小程序之蓝牙 BLE 踩坑记录

    前言 前段时间接手了一个微信小程序的开发,主要使用了小程序在今年 3 月开放的蓝牙 API ,此过程踩坑无数,特此记录一下跳坑过程.顺便开了另一个相关的小项目,欢迎 start 和 fork: BLE ...

随机推荐

  1. uni-app版本在线更新问题(下载完成安装时一闪而过,安卓8以上版本)

    我使用的是uni-app插件市场https://ext.dcloud.net.cn/plugin?id=142 出现一闪而过时加入权限 <uses-permission android:name ...

  2. openwrt查看flash、RAM、CPU信息

    1.查看Flash容量大小(存储空间,可以理解为电脑的硬盘) root@OpenWrt:/# dmesg |grep spi |grep Kbytes  #查看Flash容量[    0.660000 ...

  3. 分享:使用 TypeScript 编写的游戏代码

    <上篇博客>我写出了我一直期望的 JavaScript 大型程序的开发模式,以及 TS(TypeScript) 的一些优势.博客完成之后,我又花了一天时间试用 TS,用它来重构之前编写的一 ...

  4. 记一次WordPress 安装的过程

    安装WordPress你我他大家都会,记得10年的时候,哥已经玩转WordPress.dedecms.sns,那为何现在要记一次WordPress安装过程呢? 因为现在不会了! 之前安装都是在Wind ...

  5. Spring中@Autowired和@Resource两种自动装配的方法

    @Autowired 默认按bean类型查找并注入,若此时有多个相同类型的bean时,按bean name查找则为:@Autowired @Qulifer(value=”bean名称”). @Reso ...

  6. notes for python简明学习教程(1)

    print总是以(\n)作为结尾,不换行可以指定一个空 end='' 字符串前面+r, 原始字符串 \ 显示行连接 input()函数以字符串的形式 返回键入的内容 函数参数, 有默认值的形参要放在形 ...

  7. spring-boot-starter-actouator2.1.4与c3p0版本0.9.1.2冲突

    报错前的pom文件: <?xml version="1.0" encoding="UTF-8"?><project xmlns="h ...

  8. 八、OpenStack—Cinder组件安装

    一.安装和配置控制器节点 1.先决条件 1)创建数据库 # mysql -u root -p 2)创建cinder数据库 MariaDB [(none)]> CREATE DATABASE ci ...

  9. vue中 v-show和v-if的区别?

    v-show的操作元素的属性是display v-if的操作元素的移除和新建 还有一个就是权限的时候,v-show普通用户能看到,用v-if的时候普通用户看不到.

  10. BZOJ4668: 冷战 [并查集 按秩合并]

    BZOJ4668: 冷战 题意: 给定 n 个点的图.动态的往图中加边,并且询问某两个点最早什 么时候联通,强制在线. 还可以这样乱搞 并查集按秩合并的好处: 深度不会超过\(O(\log n)\) ...