在 [深入了解Android蓝牙Bluetooth——《基础篇》](http://blog.csdn.net/androidstarjack/article/details/60468468)一篇中我们对蓝牙的各个版本的有了一个认识,蓝牙版本的历程及其优劣式介绍。那么接下来咱们就深入一点继续开车进入BLE的进及篇章。

蓝牙BLE4.x

BLE分为三部分:

  • Service

  • Characteristic

  • Descriptor

    这三部分都用UUID作为唯一标识符。UUID为这种格式:0000ffe1-0000-1000-8000-00805f9b34fb。比如有3个Service,那么就有三个不同的UUID与Service对应。这些UUID都写在硬件里,我们通过BLE提供的API可以读取到。

  • 一个BLE终端可以包含多个Service, 一个Service可以包含多个Characteristic,一个Characteristic包含一个value和多个Descriptor,一个Descriptor包含一个Value。Characteristic是比较重要的,是手机与BLE终端交换数据的关键,读取设置数据等操作都是操作Characteristic的相关属性。

API相关介绍

  • 1.先介绍一下关于蓝牙4.0中的一些名词吧:
    (1)GATT(Gneric Attibute Profile)

    通过ble连接,读写属性类小数据Profile通用的规范。现在所有的ble应用Profile 都是基于GATT

    • (2)ATT(Attribute Protocal) GATT是基于ATT Potocal的ATT针对BLE设备专门做的具体就是传输过程中使用尽量少的数据,每个属性都有个唯一的UUID,属性chartcteristics and Service的形式传输。
    • (3)Service是Characteristic的集合。
    • (4).Characteristic 特征类型。

    比如。有个蓝牙ble的血压计。他可能包括多个Servvice,每个Service有包括多个Characteristic

    注意:蓝牙ble只能支持Android 4.3以上的系统 SDK>=18

  • 2.以下是开发的步骤:

  • 2.1首先获取BluetoothManager

  • 2.2获取BluetoothAdapter

  • 2.3创建BluetoothAdapter.LeScanCallback

  • 2.4.开始搜索设备。

  • 2.5.BluetoothDevice 描述了一个蓝牙设备 提供了getAddress()设备Mac地址,getName()设备的名称。

  • 2.6开始连接设备

  • 2.7连接到设备之后获取设备的服务(Service)和服务对应的Characteristic。

  • 2.8获取到特征之后,找到服务中可以向下位机写指令的特征,向该特征写入指令。

  • 2.9写入成功之后,开始读取设备返回来的数据。

  • 2.10、断开连接

  • 2.11、数据的转换方法

大概整体就是如上的步骤。但是也是要具体根据厂家的协议来实现通信的过程。

那么具体要怎么使用呢?我们据需开车往下走。

一.添加权限

  和经典蓝牙一样,应用使用蓝牙,需要声明BLUETOOTH权限,如果需要扫描设备或者操作蓝牙设置,则还需要BLUETOOTH_ADMIN权限:

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

除了蓝牙权限外,如果需要BLE feature则还需要声明uses-feature:

<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>

按时required为true时,则应用只能在支持BLE的Android设备上安装运行;required为false时,Android设备均可正常安装运行,需要在代码运行时判断设备是否支持BLE feature:

// 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, R.string.ble_not_supported, Toast.LENGTH_SHORT).show();
finish();
}

第一步:开启蓝牙:

  • 1.首先获取有BluetoothAdapter两种方式:
private BluetoothManager bluetoothManager;

bluetoothManager =   (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter();

或者是:

mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

两行方式都是可以的。

注:这里通过getSystemService获取BluetoothManager,再通过BluetoothManager获取BluetoothAdapter。BluetoothManager在Android4.3以上支持(API level 18)。

  • 2.判断手机设备是否有蓝牙模块
// 检查设备上是否支持蓝牙
if (mBluetoothAdapter == null) {
showToast("没有发现蓝牙模块");
return;
}
  • 3.开启蓝牙设备

    开启蓝牙设备有两种方式:

    • 第一种直接简单暴力不给用户进行提示:
    if (!mBluetoothAdapter.isEnabled()) {
    mBluetoothAdapter.enable();
    }
    • 第二种直优雅的践行开启并且有弹框进行提示,隐式启动Intent:
    if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
    Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
    }
  • 4.扫描蓝牙设备

    我这里是把扫描到的BLE地址存放到List集合中去。这里我们可以写一个方法进行封装一下。

/***********
* 扫描设备
********/
private void scanLeDevice(final boolean enable) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
if (enable) {
devices.clear();//清空集合
// Stops scanning after a pre-defined scan period.
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
mBluetoothAdapter.stopLeScan(mLeScanCallback);
}
}
}, INTERVAL_TIME);
mBluetoothAdapter.startLeScan(mLeScanCallback);
} else {
try {
mBluetoothAdapter.stopLeScan(mLeScanCallback);
} catch (Exception e) {
}
}
}
}

在这个扫描方法中,我们在AndroidStudio或者是Eclipse中会看到startLeScan方法会有横线,表明这个方式显示过期的方法,那么

如果你只需要搜索指定UUID的外设,你可以调用 startLeScan(UUID[], BluetoothAdapter.LeScanCallback)方法。 其中UUID数组指定你的应用程序所支持的GATT Services的UUID。

那么LeScanCallback的初始化代码如下:

private void initCallBack(){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
mLeScanCallback = new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (device != null) {
if (!TextUtils.isEmpty(device.getName())) {
// devices.add(device);
String name = device.getName();
if (name.contains(BluetoothDeviceAttr.OYGEN_DEVICE_NAME)) {
if (!devices.contains(device)) {
devices.add(device);
}
}
}
}
}
});
}
};
} else {
getToast("设备蓝牙版本过低");
return;
}
}

那么如果在设备多的情况下我们讲搜出很多的设备。我们可以选择我们所需要的地址进行链接。但是这类要注意的是:搜索时,你只能搜索传统蓝牙设备或者BLE设备,两者完全独立,不可同时被搜索.

  • 5.进行链接设备
final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
if (device == null) {
Log.w(TAG, "Device not found. Unable to connect.");
return false;
}
// We want to directly connect to the device, so we are setting the autoConnect
// parameter to false.
mBluetoothGatt = device.connectGatt(this, false, mGattCallback);

这里调用的是device的connectGatt方法

/**
* Connect to GATT Server hosted by this device. Caller acts as GATT client.
* The callback is used to deliver results to Caller, such as connection status as well
* as any further GATT client operations.
* The method returns a BluetoothGatt instance. You can use BluetoothGatt to conduct
* GATT client operations.
* @param callback GATT callback handler that will receive asynchronous callbacks.
* @param autoConnect Whether to directly connect to the remote device (false)
* or to automatically connect as soon as the remote
* device becomes available (true).
* @throws IllegalArgumentException if callback is null
*/
public BluetoothGatt connectGatt(Context context, boolean autoConnect,
BluetoothGattCallback callback) {
return (connectGatt(context, autoConnect,callback, TRANSPORT_AUTO));
}

 api中阐述的是第一个参数是上下文对象Context,第二个参数是是否自动连接,第三个是蓝牙的GattCallback回调。

private BluetoothGattCallback GattCallback = new BluetoothGattCallback() {
// 这里有9个要实现的方法,看情况要实现那些,用到那些就实现那些
//当连接状态发生改变的时候
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState){ };
//回调响应特征写操作的结果。
@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status){ };
//回调响应特征读操作的结果。
@Override
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
}
//当服务被发现的时候回调的结果
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
}
当连接能被被读的操作
@Override
public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) { super.onDescriptorRead(gatt, descriptor, status);
}
......
};

 连接的过程我们一个通过service来进行连接,也可以在activity中进行操作。 好,到此为止,一个BLE蓝牙连接设备的整个流程我们已经清楚完毕。

Android4.x的蓝牙不太成熟性

 但是在实际操作过程中难免会出现一些比较坑人的问题。比如我用一个地址进行蓝牙设备连接,偶尔会出现蓝牙连接不上或者是说连接上设备后不返回数据等问题。那么此时我们可能会重启一下蓝牙或手机就立马有成功了。此时我们千万不能蒙蔽,也不要怀疑自己的人生。这是因为Android4.x的蓝牙还是不太成熟。目前可以来说是个测试阶段。

  • 手机可能会搜索不到蓝牙设备
  • 有时候会在广播中接收不到数据
  • 出现异常需要重新启动手机或者是重启才能恢复正常

这个时候我们怎么办呢?

此时不要抱怨什么,难到我们作为Android程序员就注定如此的苦逼吗?

答案是否定的。

如何去优化呢?那么我们就应该从UI界面,用户体验上进行操作来实现

  • 做一个定时器,如果在在确定蓝牙设备一打开并且存在的情况系,可以在手机搜索5s内没有搜索到蓝牙设备时重启蓝牙,并且在广播中接收到蓝牙开启后再次搜索

  • 可以在UI上进行对用户进行相对应的提示

    • 当蓝牙为启动时,提示用户去开启器蓝牙
    • 当蓝牙开启后,在处在开启状态后,提示用户蓝牙正在开启...
    • 蓝牙已开启,设备并没有连接上,提示用户去进行连接
    • 设备正在连接上手机,提示用户,正在连接,请等待...
    • 蓝牙设备连接上手机,正在读取,提示数据正在读取中...

    我们不能子在Android系统上来操作什么,我们在体验上做到了我们能做的就可以的。

手机蓝牙连接BLE设备要求

  • 手机Android 4.3以上的系统 SDK>=18
  • 蓝牙版本>=4.0

学习参考道demo下载地址: https://github.com/androidstarjack/Bluetooth_4.3-master

学到这里,关于AndroidBLE蓝牙连接我们已经基本上实现了蓝牙的搜索,连接,读取等。

大家项目中如果经常涉及到硬件比如手环,温度计,汗液仪,心电图,血压计等这些ble的蓝牙设备,就一定会用到蓝相关方面的知识。这里笔者先给大家提前踩一下坑,进行了总结,为后面的小伙伴在研究蓝牙方面尽量的少踩一些坑。如多对蓝牙的历程还未有清楚的认识,请参考深入了解Android蓝牙Bluetooth4.0——《基础篇》。

如果你觉得此文对您有所帮助,欢迎入群 QQ交流群 :232203809    微信公众号:终端研发部

(欢迎关注学习和交流)

深入了解Android蓝牙Bluetooth——《进阶篇》的更多相关文章

  1. 深入了解Android蓝牙Bluetooth ——《总结篇》

    在我的上两篇博文中解说了有关android蓝牙的认识以及API的相关的介绍,蓝牙BLE的搜索,连接以及读取. 没有了解的童鞋们请參考: 深入了解Android蓝牙Bluetooth--<基础篇& ...

  2. android -- 蓝牙 bluetooth (三)搜索蓝牙

    接上篇打开蓝牙继续,来一起看下蓝牙搜索的流程,触发蓝牙搜索的条件形式上有两种,一是在蓝牙设置界面开启蓝牙会直接开始搜索,另一个是先打开蓝牙开关在进入蓝牙设置界面也会触发搜索,也可能还有其它触发方式,但 ...

  3. ZT android -- 蓝牙 bluetooth (三)搜索蓝牙

    android -- 蓝牙 bluetooth (三)搜索蓝牙 分类: Android的原生应用分析 2013-05-31 22:03 2192人阅读 评论(8) 收藏 举报 bluetooth蓝牙s ...

  4. ZT android -- 蓝牙 bluetooth (四)OPP文件传输

    android -- 蓝牙 bluetooth (四)OPP文件传输 分类: Android的原生应用分析 2013-06-22 21:51 2599人阅读 评论(19) 收藏 举报 4.2源码AND ...

  5. ZT android -- 蓝牙 bluetooth (二) 打开蓝牙

    android -- 蓝牙 bluetooth (二) 打开蓝牙 分类: Android的原生应用分析 2013-05-23 23:57 4773人阅读 评论(20) 收藏 举报 androidblu ...

  6. ZT android -- 蓝牙 bluetooth (五)接电话与听音乐

    android -- 蓝牙 bluetooth (五)接电话与听音乐 分类: Android的原生应用分析 2013-07-13 20:53 2165人阅读 评论(9) 收藏 举报 蓝牙android ...

  7. android -- 蓝牙 bluetooth (四)OPP文件传输

    在前面android -- 蓝牙 bluetooth (一) 入门文章结尾中提到了会按四个方面来写这系列的文章,前面已写了蓝牙打开和蓝牙搜索,这次一起来看下蓝牙文件分享的流程,也就是蓝牙应用opp目录 ...

  8. ZT android -- 蓝牙 bluetooth (一) 入门

    android -- 蓝牙 bluetooth (一) 入门 分类: Android的原生应用分析 2013-05-19 21:44 4543人阅读 评论(37) 收藏 举报 bluetooth4.2 ...

  9. 深入了解Android蓝牙Bluetooth——《基础篇》

    什么是蓝牙?   也可以说是蓝牙技术.所谓蓝牙(Bluetooth)技术,实际上是一种短距离无线电技术,是由爱立信公司公司发明的.利用"蓝牙"技术,能够有效地简化掌上电脑.笔记本电 ...

随机推荐

  1. PyCharm 2017 官网 下载 安装 设置 配置 (主题 字体 字号) 使用 入门 教程

    一.安装 Python 3.6 首先,要安装好 Python 3.6.如果你还没有安装,可以参考咪博士之前的教程 Python 3.6.3 官网 下载 安装 测试 入门教程 (windows) 二.官 ...

  2. ubuntu软件使用汇总

    ubuntu使用合集 安装Ubuntu时的硬盘分区 Linux入门(1)--Ubuntu16.04安装搜狗拼音 Linux入门(2)--Ubuntu16.04安装wineQQ Linux入门(3)-- ...

  3. JPA + SpringData 操作数据库原来可以这么简单 ---- 深入了解 JPA - 3

    原创播客,如需转载请注明出处.原文地址:http://www.cnblogs.com/crawl/p/7718741.html ------------------------------------ ...

  4. (转)Nginx与tomcat组合的简单使用

    原文出自:http://www.cnblogs.com/naaoveGIS/ 1.背景 项目中瓦片资源越来越多,如果提高瓦片的访问效率是一个需要解决的问题.这里,我们考虑使用Nginx来代理静态资源进 ...

  5. 2017年最受欢迎的UI框架

    前端领域最近几年发展的特别迅速,可以说是百家争鸣.在底层的前端框架领域中,最早是jquery称霸互联网,近两年MVVM类型的框架慢慢成为主流,Vue.React和Angular三大框架并驾齐驱.可以说 ...

  6. ArrayList 源码(基于Java1.8)

    ArrayList 源码 ArrayList 基于数组实现,也就是类对变量 Object[]系列操作,封装为常用的add,remove,indexOf, contains本质是通过 size 计数器对 ...

  7. [Scikit-learn] 1.2 Dimensionality reduction - Linear and Quadratic Discriminant Analysis

    Ref: http://scikit-learn.org/stable/modules/lda_qda.html Ref: http://bluewhale.cc/2016-04-10/linear- ...

  8. 刘强1109 JavaScript基础二(分支与循环结构)

    [if-else结构] 1.结构的写法: if(判断条件){ 条件为true时,执行if{} } else{ 条件为false时,执行else{} } 2.注意事项: ① else{}语句块,可以根据 ...

  9. ASP.NET Core的身份认证框架IdentityServer4(5)- 包和构建

    包和构建 IdentityServer有许多nuget包 IdentityServer4 nuget | github 包含IdentityServer核心对象模型,服务和中间件. 仅支持内存配置和用 ...

  10. no device found for connection ‘ System eth0′问题

    我用的是centos6 在ping 百度的时候ping不通 提示错误no device found for connection ' System eth0′ 解决方法:虚拟机右上角有一个小电脑的标志 ...