Android BLE学习(一): Android搜索BLE设备
来源:互联网 发布:mac电脑开机密码设置 编辑:程序博客网 时间:2024/06/06 20:38
背景
总结一下最近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();
搜索流程
开启蓝牙设备
这里我们使用一个Intent来开启系统牙,此处我们还需要为应用添加蓝牙相关权限
<uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
开启蓝牙,返回时接收Result,用于显示蓝牙是否开启成功。
Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);startActivityForResult(enableIntent, 1);
检查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; }}
搜索
mBluetoothAdapter.stopLeScan(mBLEScanCallback);//启动搜索BLE设备mBluetoothAdapter.startLeScan(mBLEScanCallback);//停止搜索BLE设备
当收索到内容时,将调用我们设置的回调函数。可以得到信号强度和BLE设备信息以及广播内容。
new BluetoothAdapter.LeScanCallback(){ @Override public void onLeScan(final BluetoothDevice device, final int rssi, byte[] scanRecord) { Log.i("MainActivity", device.getAddress()); addBLEDeviceData(device, rssi); } };
VONCHENCHEN_BLE就是我们的51822 BLE设备。
小结
至此,我们就完成了BLE设备的搜索,总结一下就是使用BluetoothAdapter类提供的方法完成对BLE设备的扫描,获取到BLE设备的相关信息,如设备名字和Mac地址等,我们可以使用这些信息进行蓝牙连接。
- Android BLE学习(一): Android搜索BLE设备
- Android BLE学习(一): Android搜索BLE设备
- android 6.0 不能搜索Ble设备
- android 6.0 不能搜索Ble设备
- Android BLE设备扫描
- android BLE搜索beacon
- Android Ble开发(一)
- Android BLE学习笔记
- Android BLE学习笔记
- Android BLE学习笔记
- Android BLE学习笔记
- Android BLE学习笔记
- Android BLE学习笔记
- android 蓝牙ble设备开发
- Android BLE虚拟设备端
- Android之BLE开发(一)
- Android蓝牙BLE(一)--使用介绍
- android BLE蓝牙详细讲解(一)
- overridePendingTransition
- [LeetCode]163. Missing Ranges
- Redis介绍、安装、命令行
- redis翻译(1)
- spring的资源访问工具类
- Android BLE学习(一): Android搜索BLE设备
- 【 bzoj 3065 】 带插入区间K小值 - 树套树乱搞
- 【redis编译】Redis 2.8.18 编译出错
- ④C语言,二维数组模拟井字棋游戏开发
- 为什么MFC里这个常出现“未定义标识符”?
- SpeedTree - 建立UE4版本的模型
- dubby带你学java---WeakReference
- (一三六)基类——第十三章
- Java动态代理机制详解(JDK 和CGLIB,Javassist,ASM)