Android蓝牙笔记

一、相关介绍
1、概述

蓝牙无线技术是一种全球通用的短距离无线技术,通过蓝牙技术能够实现多种电子设备间的相互连接,特别是在小型无线电、耗电量低、成本低、安全性、稳定性、易用性以及特别的联网能力等固有的优势上,蓝牙无线技术发展迅速。

2、分类

蓝牙分为三种:Bluetooth Smart Ready、Bluetooth Smart(Smart是低功耗蓝牙的标识)、以及标准 Bluetooth。根据 Bluetooth SIG的说法,这样是为了要分辨装置间的相容性以及标识各版本的传输频率。基本上来说,Bluetooth Smart Ready适用于任何双模蓝牙4.0的电子产品,而Bluetooth Smart是应用在心率监视器或计步器等使用扭扣式电池并传输单一的装置。Bluetooth Smart Ready的相容性最高,可与Bluetooth Smart及标准蓝牙相通。标准蓝牙则无法与Bluetooth Smart相通。
\

3、BLE介绍

BLE是Bluetooth Low Energy的缩写,又叫蓝牙4.0,区别于蓝牙3.0和之前的技术。BLE前身是NOKIA开发的Wibree技术,主要用于实现移动智能终端与周边配件之间的持续连接,是功耗极低的短距离无线通信技术,并且有效传输距离被提升到了100米以上,同时只需要一颗纽扣电池就可以工作数年之久。BLE是在蓝牙技术的基础上发展起来的,既同于蓝牙,又区别于传统蓝牙。BLE设备分单模和双模两种,双模简称BR,商标为Bluetooth Smart Ready,单模简称BLE或者LE,商标为Bluetooth Smart。Android是在4.3后才支持BLE,这说明不是所有蓝牙手机都支持BLE,而且支持BLE的蓝牙手机一般是双模的。双模兼容传统蓝牙,可以和传统蓝牙通信,也可以和BLE通信,常用在手机上,android4.3和IOS4.0之后版本都支持BR,也就是双模设备。单模只能和BR和单模的设备通信,不能和传统蓝牙通信,由于功耗低,待机长,所以常用在手环的智能设备上。

二、基本概念
1、Generic Access Profile(GAP)

用来控制设备连接和广播,GAP使你的设备被其他设备可见,并决定了你的设备是否可以或者怎样与合同设备进行交互。

2、Generic Attribute Profile(GATT)

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

3、Attribute Protocol (ATT)

GATT是基于ATTProtocol的,ATT针对BLE设备做了专门的优化,具体就是在传输过程中使用尽量少的数据,每个属性都有一个唯一的UUID,属性将以characteristics and services的形式传输。

4、Characteristic

Characteristic可以理解为一个数据类型,它包括一个value和0至多个对次value的描述(Descriptor)。

5、Descriptor

对Characteristic的描述,例如范围、计量单位等。

6、Service

Characteristic的集合。例如一个service叫做“Heart Rate Monitor”,它可能包含多个Characteristics,其中可能包含一个叫做“heart ratemeasurement”的Characteristic。

7、UUID

唯一标示符,每个Service,Characteristic,Descriptor,都是由一个UUID定义。

三、Android BLE API
1、BluetoothGatt

继承BluetoothProfile,通过BluetoothGatt可以连接设备(connect),发现服务(discoverServices),并把相应地属性返回到BluetoothGattCallback,可以看成蓝牙设备从连接到断开的生命周期。

2、BluetoothGattCharacteristic

相当于一个数据类型,可以看成一个特征或能力,它包括一个value和0~n个value的描述(BluetoothGattDescriptor)。

3、BluetoothGattDescriptor

描述符,对Characteristic的描述,包括范围、计量单位等。

4、BluetoothGattService

服务,Characteristic的集合。

5、BluetoothProfile

一个通用的规范,按照这个规范来收发数据。

6、BluetoothManager

通过BluetoothManager来获取BluetoothAdapter。
BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);

7、BluetoothAdapter

代表了移动设备的本地的蓝牙适配器, 通过该蓝牙适配器可以对蓝牙进行基本操作,一个Android系统只有一个BluetoothAdapter,通过BluetoothManager获取。
BluetoothAdapter bluetoothAdapter = bluetoothManager.getAdapter();

8、BluetoothDevice

扫描后发现可连接的设备,获取已经连接的设备。
BluetoothDevice bluetoothDevice = bluetoothAdapter.getRemoteDevice(address);

9、BluetoothGattCallback

已经连接上设备,对设备的某些操作后返回的结果。

?
1
2
3
4
5
<code class = "hljs cs" >BluetoothGattCallback bluetoothGattCallback = new BluetoothGattCallback(){
     //实现回调方法,根据业务做相应处理
};
BluetoothGatt bluetoothGatt = bluetoothDevice.connectGatt( this , false , bluetoothGattCallback);
</code>
三、操作流程
1、蓝牙开启

在使用蓝牙BLE之前,需要确认Android设备是否支持BLE feature(required为false时),另外要需要确认蓝牙是否打开。如果发现不支持BLE,则不能使用BLE相关的功能;如果支持BLE,但是蓝牙没打开,则需要打开蓝牙。代码示例如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<code class = "hljs java" > //是否支持蓝牙模块
@TargetApi ( 18 )
public static boolean isSupportBle(Context context) {
     if (context != null && context.getPackageManager().hasSystemFeature( "android.hardware.bluetooth_le" )) {
         BluetoothManager manager = (BluetoothManager)context.getSystemService( "bluetooth" );
         return manager.getAdapter() != null ;
     } else {
         return false ;
     }
}
//是否开启蓝牙
@TargetApi ( 18 )
public static boolean isBleEnable(Context context) {
     if (!isSupportBle(context)) {
         return false ;
     } else {
         BluetoothManager manager = (BluetoothManager)context.getSystemService( "bluetooth" );
         return manager.getAdapter().isEnabled();
     }
}
//开启蓝牙
public static void enableBle(Activity act,
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值