Android BLE低功耗蓝牙开发极简系列(一)之扫描与连接
来源:互联网 发布:windows是那个公司 编辑:程序博客网 时间:2024/05/22 07:53
简书地址:Android BLE低功耗蓝牙开发极简系列(一)之扫描与连接
背景
公司硬件模块升级,由原本的单模式蓝牙,换成现在的双模蓝牙。单模蓝牙就是通过BlueTooth 2.0传统蓝牙进行音频或者数据传输,双模蓝牙兼容传统蓝牙,可以和传统蓝牙通信,也可以和BLE通信,谷歌在Android 4.3后开始支持BLE低功耗蓝牙。
由来
公司使用的蓝牙模块,供应商那边提供了相应的sdk,在代码测试和开发工程中,发现连接速度的稳定性存在问题,和其他公司的技术人员联系,协商问题,应该是工作中费时费力的一件事,最后还不一定能够得到解决。询问过sdk那边相关人员,说可以自己用系统的方法进行调试。
BLE相关概念
UUID
Universally Unique Identifier ,唯一标示符,一般开发中会用到SERVICE、WRITE、NOTIFICATION等的UUID,需固件端配合修改。
Generic Attribute Profile (GATT)
GATT是用于发送和接收的通用规范,不同于传统蓝牙使用socket, BLE之间的文件数据传输基于GATT。GATT连接是一对一的,也就说一个BLE设备智能连接一个设备。一旦连接成功,只有当连接断开,否则其它设备无法连接当前设备的的。
Attribute Protocol (ATT)
GATT是建立在ATT的基础上,ATT针对BLE设备进行了优化,在传输中使用尽量少的数据,每个属性都有一个唯一的UUID,属性间的传输会以characteristics 和services的形式来进行传输。
Characteristic
Characteristic特征值,包含一个或者多个value,可以通过characteristic进行文件的读写操作。
Descriptor
用于描述特征值的属性
Service
特征值得集合
开发
权限
进行蓝牙相关操作,需要使用到蓝牙权限,在AndroidManifest.xml清单文件中添加相应权限
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true" /><uses-permission android:name="android.permission.BLUETOOTH" /><uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
在android6.0以后,蓝牙BLE还需要需要获得位置权限
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
初始化蓝牙Adapter
使用蓝牙进行相关操作之前,需要先获取蓝牙适配器
final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); if (bluetoothManager != null) { mBluetoothAdapter = bluetoothManager.getAdapter(); }
判断蓝牙是否可用或者是否开启,如果蓝牙关闭,那么开启蓝牙
if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) { Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, 1); }
扫描
扫描过程中,需要对扫描结果进行回调,在onLeScan()方法中对扫描的结果进行相关处理
private BluetoothDevice mDevice;final BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() { @Override public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) { Log.d("haha", "onLeScan: " + device.getName() + " : " + rssi); String name = device.getName(); if (name != null) { deviceName.setText(name); if (name.equals("test_ble")) { mDevice = device; mBluetoothAdapter.stopLeScan(mLeScanCallback); } } }};
由于蓝牙扫描耗时耗电,所以在进行扫描的时候,注意自定义一个合适的扫描时间,在实际的开发和项目应用过程中,自己选择合适的时间。定义好蓝牙扫描回调,开始扫描蓝牙,扫描到想要的蓝牙,就可以停止扫描
private void scanLeDevice(final boolean enable) { if (enable) { // Stops scanning after a pre-defined scan period. // 预先定义停止蓝牙扫描的时间(因为蓝牙扫描需要消耗较多的电量) mHandler.postDelayed(new Runnable() { @Override public void run() { mScanning = false; mBluetoothAdapter.stopLeScan(mLeScanCallback); } }, 30000); mScanning = true; // 定义一个回调接口供扫描结束处理 mBluetoothAdapter.startLeScan(mLeScanCallback); } else { mScanning = false; mBluetoothAdapter.stopLeScan(mLeScanCallback); }}
通过获取的mBluetoothAdapter调用startLeScan()传入mLeScanCallback参数,即可进行蓝牙扫描。
BluetoothGattCallback实现
GATT是用于发送和接收的通用规范, BLE之间的文件数据传输基于GATT,因此在进行连接之前,需要进行Gatt接口回调。
private String TAG = "haha";private boolean isServiceConnected;private BluetoothGattCallback mGattCallback = new BluetoothGattCallback() { @Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { super.onConnectionStateChange(gatt, status, newState); Log.d("haha", "onConnectionStateChange: " + newState); if (status != BluetoothGatt.GATT_SUCCESS) { String err = "Cannot connect device with error status: " + status; // 当尝试连接失败的时候调用 disconnect 方法是不会引起这个方法回调的,所以这里 // 直接回调就可以了。 gatt.close(); if (mBluetoothGatt != null) { mBluetoothGatt.disconnect(); mBluetoothGatt.close(); mBluetoothGatt = null; } if (mDevice != null) { mBluetoothGatt = mDevice.connectGatt(MainActivity.this, false, mGattCallback); } Log.e(TAG, err); return; } if (newState == BluetoothProfile.STATE_CONNECTED) { //当蓝牙设备已经接 Toast.makeText(MainActivity.this, "连接成功", Toast.LENGTH_SHORT).show(); Log.i("haha", "Attempting to start service discovery:" + Log.d("haha", "onConnectionStateChange: " + "连接成功"); } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {//当设备无法连接 if (mBluetoothGatt != null) { mBluetoothGatt.disconnect(); mBluetoothGatt.close(); mBluetoothGatt = null; } gatt.close(); if (mDevice != null) { mBluetoothGatt = mDevice.connectGatt(MainActivity.this, false, mGattCallback); } }};
通过status对当前连接进行判断,当status != BluetoothGatt.GATT_SUCCESS时,可以进行Gatt的重置操作,尝试重连。当newState == BluetoothProfile.STATE_CONNECTED时,此时连接成功。
连接
当实现了Gatt连接回到之后,即可对Ble蓝牙进行连接操作
public void startConnect(View view) { if (mDevice != null) { if (mBluetoothGatt != null) { mBluetoothGatt.disconnect(); mBluetoothGatt.close(); mBluetoothGatt = null; } mBluetoothGatt = mDevice.connectGatt(MainActivity.this, false, mGattCallback); }}
注意事项
BLE实现android 4.3以后google才提供了支持,所以Ble项目只可运行在API 18以上的手机;Android 6.0以上,Ble服务还需要定位权限,否则使用不了。不使用时,还应注意对蓝牙进行关闭或断开操作,由于Ble连接属于独占操作,有设备连接上了,其它设备是无法进行任何操作的。
Github
BleDemo
下一篇Android BLE低功耗蓝牙开发极简系列(二)之读写操作
- Android BLE低功耗蓝牙开发极简系列(一)之扫描与连接
- Android BLE低功耗蓝牙开发极简系列(二)之读写操作
- Android 低功耗蓝牙(BLE)开发(4)-- 蓝牙扫描和连接
- 【android 蓝牙开发——BLE(低功耗)蓝牙】
- 低功耗蓝牙BLE之连接建立
- Android ble低功耗蓝牙开发
- Android BLE低功耗蓝牙开发
- Android-BLE低功耗蓝牙开发
- BLE 低功耗蓝牙开发 (一概述)
- 低功耗蓝牙(BLE)之开发步骤
- Android 低功耗蓝牙(BLE)开发(1)-- 基本概念
- Android 低功耗蓝牙(BLE)开发(2)-- BluetoothAdapter详解
- Android 低功耗蓝牙(BLE)开发(3)-- BluetoothDevice详解
- Android-低功耗蓝牙BLE(Bluetooth Low Energy)开发
- Android BLE低功耗蓝牙开发(上)关于GATT服务器的理论与搭建
- Android BLE低功耗蓝牙开发(下) BLE客户端(中央设备)与GATT服务的通讯
- 低功耗蓝牙(BLE)
- 低功耗蓝牙(BLE)
- 数据库自动化运维平台--DDL平台服务
- lvm定义&创建&删除&拉伸&缩减
- 全文检索引擎Solr系列—–全文检索基本原理
- rtmp complex handshake,变更的握手,支持h264/aac
- 常见排序算法
- Android BLE低功耗蓝牙开发极简系列(一)之扫描与连接
- c++ sort函数()用法
- Android BLE低功耗蓝牙开发极简系列(二)之读写操作
- springmvc拦截器与web组件filter使用的粗浅认识
- linux修改系统的yum源为阿里云并添加epel源
- Mybatis-Plus代码生成器使用
- 磁盘及网络IO工作方式解析
- Centos6.5下装ZooKeeper
- R语言之多重共线性的判别以及解决方法