http://my.csdn.net/lidec

背景

总结一下最近ble的学习情况。自从入手ble 51822开发板后就开始不停加班,中途出于好奇,业余时间写了一些单片机上json解析相关的东西,妄图使用蓝牙传输json数据,不知道是否实用,既然开始写了,得写出点样子,晃晃荡荡,2016年的1月份就过去了。

这里本章我们主要总结一下ble搜索相关的内容,先建立直观印象,然后剖析ble模块与Android相关代码,看看源码与现象是如何对应的。最后,当我们了解流程后,就可以比较容易地理解蓝牙协议中的一些内容,最终实现照我们自己的需求建立协议,开发属于我们自己的模块的目的。

51822中自带了蓝牙协议栈,协议栈也规定了程序的框架,感觉这样的好处就是简化了开发流程,我们可以按照能跑通的demo进行修改即可,方便学习。

Android BLE 搜索

BluetoothAdapter

以下是android官方对这个类的介绍

Represents the local device Bluetooth adapter. The BluetoothAdapter lets you perform fundamental Bluetooth tasks, such as initiate device discovery, query a list of bonded (paired) devices, instantiate a BluetoothDevice using a known MAC address, and create a BluetoothServerSocket to listen for connection requests from other devices, and start a scan for Bluetooth LE devices.

To get a BluetoothAdapter representing the local Bluetooth adapter, when running on JELLY_BEAN_MR1 and below, call the static getDefaultAdapter() method; when running on JELLY_BEAN_MR2 and higher, retrieve it through getSystemService(String) with BLUETOOTH_SERVICE. Fundamentally, this is your starting point for all Bluetooth actions. Once you have the local adapter, you can get a set of BluetoothDevice objects representing all paired devices with getBondedDevices(); start device discovery with startDiscovery(); or create a BluetoothServerSocket to listen for incoming connection requests with listenUsingRfcommWithServiceRecord(String, UUID); or start a scan for Bluetooth LE devices with startLeScan(LeScanCallback).

大意在讲这个类代表了本地蓝牙适配器,可以对蓝牙进行一些基本操作,比如: 
1.发现设备 
2.获取配对设备列表 
3.获取设备mac地址 
4.创建监听 
5.搜索BLE设备

我们所需要的就是其搜索BLE设备的功能。这也就是为什么我们的Android程序在运行时只能看到ble设备而看不到其他一些蓝牙设备的原因。

下面看一下我们需要用到搜索ble设备的API

boolean startLeScan(BluetoothAdapter.LeScanCallback callback)

启动搜索BLE设备

boolean startLeScan(UUID[] serviceUuids, BluetoothAdapter.LeScanCallback callback)

搜索指定serviceUUID的BLE设备

void stopLeScan(BluetoothAdapter.LeScanCallback callback)

停止收索BLE设备

其中的回调函数就是收索到设备后进行的回调。

BlueToothAdapter通过BluetoothManager的getAdapter()方法获取实例,具体代码如下:

final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);mBluetoothAdapter = bluetoothManager.getAdapter();
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

搜索流程

开始开启蓝牙设备检查BLE设备是否可用BLE设备可用?开始搜索BLE设备并获取相关信息结束yesno

开启蓝牙设备

这里我们使用一个Intent来开启系统牙,此处我们还需要为应用添加蓝牙相关权限

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

开启蓝牙,返回时接收Result,用于显示蓝牙是否开启成功。

Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntent, 1);
  • 1
  • 2
  • 1
  • 2

检查BLE是否可用

private void checkBLEDevice(){// Use this check to determine whether BLE is supported on the device.  Then you can// selectively disable BLE-related features.if(!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {Toast.makeText(this, " ble not supported ", Toast.LENGTH_SHORT).show();finish();
}// Initializes a Bluetooth adapter.  For API level 18 and above, get a reference to
// BluetoothAdapter throughBluetoothManager.
final BluetoothManager bluetoothManager = (BluetoothManager)getSystemService(Context.BLUETOOTH_SERVICE);mBluetoothAdapter = bluetoothManager.getAdapter();// Checks if Bluetooth is supported on the device.
if (mBluetoothAdapter == null) {Toast.makeText(this, " ble not supported ", Toast.LENGTH_SHORT).show();finish();return;}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

搜索

mBluetoothAdapter.stopLeScan(mBLEScanCallback);//启动搜索BLE设备mBluetoothAdapter.startLeScan(mBLEScanCallback);//停止搜索BLE设备
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

当收索到内容时,将调用我们设置的回调函数。可以得到信号强度和BLE设备信息以及广播内容。

new BluetoothAdapter.LeScanCallback(){@Overridepublic void onLeScan(final BluetoothDevice device, final int rssi, byte[] scanRecord) {Log.i("MainActivity", device.getAddress());addBLEDeviceData(device, rssi);}};
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

VONCHENCHEN_BLE就是我们的51822 BLE设备。

小结

至此,我们就完成了BLE设备的搜索,总结一下就是使用BluetoothAdapter类提供的方法完成对BLE设备的扫描,获取到BLE设备的相关信息,如设备名字和Mac地址等,我们可以使用这些信息进行蓝牙连接。

Android BLE学习(一): Android搜索BLE设备相关推荐

  1. Android FrameWork 学习之Android 系统源码调试

    这是很久以前访问掘金的时候 无意间看到的一个关于Android的文章,作者更细心,分阶段的将学习步骤记录在自己博客中,我觉得很有用,想作为分享同时也是留下自己知识的一些欠缺收藏起来,今后做项目的时候会 ...

  2. Android开发学习——2.Android开发环境准备

    文章目录 一.引言 二.开发前准备 1. JDK 2. Android SDK 3. Android Studio 三.结尾 四.参考 一.引言 前一篇文中提到了Android Studio(简称AS ...

  3. Android动画学习笔记-Android Animation

    3.0以前,android支持两种动画模式,tween animation,frame animation,在android3.0中又引入了一个新的动画系统:property animation,这三 ...

  4. [转][android深入学习]android窗口管理机制

    在学习 WindowManager 接口的时候,了解到这个接口很重要,因为它可以直接与Window Manager(窗口管理器)进行交互,那这个 Window Manager 究竟是怎么一回事呢? 通 ...

  5. 【BLE学习笔记】之 BLE基础知识

    前言 很多时候,对于BLE的开发者而言,有90%的时间会用在coding上,从而很容易忽视了BLE硬件相关的知识.小编在下键盘准备敲打内容的时候,在想 "到底要不要花费时间在这占比很小的硬件 ...

  6. Android usb学习笔记:Android AOA协议Android端 流程总结

    背景 上篇文章中我们了解了嵌入式设备端将Android手机设置为accessory模式的流程以及嵌入式设备端接收和发送数据的流程,本文将对应介绍Android端accessory模式被激活的过程,以及 ...

  7. android开发学习大体思路

    android开发学习: android学习的前提是java基础.如果你没有好的java基础,那就赶紧补充,我在这里不做介绍. android是基于linux的,如果你要做底层的东西,可以买一些关于l ...

  8. 《Android Studio应用开发实战详解》——第1章,第1.5节Android开发学习路线图

    本节书摘来自异步社区<Android Studio应用开发实战详解>一书中的第1章,第1.5节Android开发学习路线图,作者 王翠萍,更多章节内容可以访问云栖社区"异步社区& ...

  9. 移动开发技术(Android)——实验5 Android高级控件的应用

    移动开发技术--实验5 Android高级控件的应用 一.实验目的 二.实验内容 1.Spinner.ListView控件与Adapter适配器(一) 2.Spinner.ListView控件与Ada ...

最新文章

  1. 遗传算法与C++实现
  2. 手机拍视频最怕抖,只能靠AI拯救了
  3. python list 查找子列_python – SQLAlchemy查询,其中列包含一个子字符串
  4. 删零c语言,C语言(请不要删)
  5. OpenCV+yolov3实现目标检测(C++,Python)
  6. linux指定内核位置,ARM linux内核启动时几个关键地址
  7. SpringMVC接收Post的实体/JSon数据
  8. iOS基本UI元素示例教程
  9. mysql专业连接工具_mysql(MySQL客户端连接工具)
  10. 某CRM旗舰版功能齐全客户管理系统源码
  11. python 常数赋值给tensor、常数和tensor比较大小、常数和tensor比较大小后作为tensor索引
  12. 那些有趣/用的 Python 库
  13. 【附源码】计算机毕业设计SSM网上鲜花店系统
  14. 液晶显示屏的C语言编码,AT89C51单片机驱动液晶显示汉字C语言
  15. Matlab的GUI程序转换为单独可执行的exe文件
  16. 第四课:如何安装树莓派系统
  17. 用python一键去除图片、PDF水印
  18. 火狐浏览器中设置打开新地址时,不会覆盖原页面的方法
  19. look forward to 用法
  20. 解决Android11版本无法获取安装应用APP版本号的问题

热门文章

  1. python数字信号处理应用中文pdf_人邮新书 Python数字信号处理应用 Python在DSP中应用教程 Python基础 Pytho...
  2. 基线管理之Centos安全配置
  3. 使用Banner实现轮播展示
  4. confirm修改按钮文字_踏入MG动画设计的门,才知道文字动画这么重要……
  5. OSG的垃圾回收机制
  6. Win10(UEFI启动)安装Ubuntu18.04双系统
  7. 海南大学计算机科学与技术专业考研,2021年海南大学计算机科学与技术(081200)硕士研究生招生信息_考研招生计划和招生人数 - 学途吧...
  8. React全栈之Instagram开源视频教程
  9. iOS 常用公共方法
  10. 开发RESTful WebService