首先简单介绍一下蓝牙4.0 BLE开发
BLE全名为Bluetooth Low Energy 顾名思义,蓝牙低能耗。
蓝牙4.0及以上称之为BLE,而4.0以下则称之为传统蓝牙。那么二者有什么区别呢?
BLE与传统蓝牙相比四大特点:
1、低能耗,蓝牙4.0设备与周围设备交流时,其峰值能耗仅为传统蓝牙设备的一半
2、传输距离远,100米以上
3、使用128-bit AES完全加密,为数据封包提供高度加密性及认证度
4、低延时:最短可在3ms内完成连接设置并开始传输数据。(传统蓝牙设备的迟延最高达100ms)
在介绍BLE使用之前,先介绍几个概念。
使用低功耗蓝牙可以包括多个Profile,一个Profile中有多个Service,一个Service中有多个Characteristic,一个Characteristic中包括一个value和多个Descriptor。
1.Generic Attribute Profile(GATT)—GATT配置文件是一个通用规范,用于在BLE链路上发送和接收被称为“属性”的数据块。目前所有的BLE应用都基于GATT。 蓝牙SIG规定了许多低功耗设备的配置文件。配置文件是设备如何在特定的应用程序中工作的规格说明。注意一个设备可以实现多个配置文件。例如,一个设备可能包括心率监测仪和电量检测。
2.Attribute Protocol(ATT)—GATT在ATT协议基础上建立,也被称为GATT/ATT。ATT对在BLE设备上运行进行了优化,为此,它使用了尽可能少的字节。每个属性通过一个唯一的的统一标识符(UUID)来标识,每个String类型UUID使用128 bit标准格式。属性通过ATT被格式化为characteristics和services。
3.Characteristic 一个characteristic包括一个单一变量和0-n个用来描述characteristic变量的descriptor,characteristic可以被认为是一个类型,类似于类。
4.Descriptor Descriptor用来描述characteristic变量的属性。例如,一个descriptor可以规定一个可读的描述,或者一个characteristic变量可接受的范围,或者一个characteristic变量特定的测量单位。
5.Service service是characteristic的集合。例如,你可能有一个叫“Heart Rate Monitor(心率监测仪)”的service,它包括了很多characteristics,如“heart rate measurement(心率测量)”等。你可以在bluetooth.org 找到一个目前支持的基于GATT的配置文件和服务列表。
如何使用呢?这里借用网上的一张图片,清晰明了的展示了BLE的使用流程。
具体使用流程如下:
1.想要使用蓝牙功能,必须添加权限:
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
如果想声明你的app只为具有BLE的设备提供,在manifest文件中包括:
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>
但是如果想让你的app提供给那些不支持BLE的设备,需要在manifest中包括上面代码并设置required=”false”,然后在运行时可以通过使用PackageManager.hasSystemFeature()确定BLE的可用性。
2.检查设备是否支持BLE
if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
Toast.makeText(this,“此设备不支持BLE”, Toast.LENGTH_SHORT).show();
finish();
}
3.获取 BluetoothAdapter适配器
BluetoothManager bluetoothManager =(BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter();
4.开启蓝牙
// 确保蓝牙在设备上可以开启
if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// User chose not to enable Bluetooth.
if (requestCode == REQUEST_ENABLE_BT && resultCode == Activity.RESULT_CANCELED) {
finish();
return;
}
super.onActivityResult(requestCode, resultCode, data);
}
5.搜索BLE设备
为了发现BLE设备,使用startLeScan())方法。这个方法需要一个参数BluetoothAdapter.LeScanCallback。你必须实现它的回调函数,那就是返回的扫描结果。因为扫描非常消耗电量,你应当遵守以下准则:
只要找到所需的设备,停止扫描。
不要在循环里扫描,并且对扫描设置时间限制。以前可用的设备可能已经移出范围,继续扫描消耗电池电量。
private void scanLeDevice(final boolean enable) {
if (enable) {
// 经过预定扫描期后停止扫描,防止大量消耗电量
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
mScanning = false;
//先停止搜索
mBluetoothAdapter.stopLeScan(mLeScanCallback);
}
}, SCAN_PERIOD);
mScanning = true;
//开始搜索
mBluetoothAdapter.startLeScan(mLeScanCallback);
} else {
mScanning = false;
mBluetoothAdapter.stopLeScan(mLeScanCallback);
}
}
6.搜索设备的回调方法LeScanCallback
在4.3之前的api是通过注册广播来处理搜索时发生的一些事件,而支持ble的新的api中,是通过回调的方式来处理的,而mLeScanCallback就是一个接口对象
private BluetoothAdapter.LeScanCallback mLeScanCallback =
new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, int rssi,
byte[] scanRecord) {
//device 搜索到的蓝牙设备
runOnUiThread(new Runnable() {
@Override
public void run() {
//device.getName();获取蓝牙设备名字
//device.getAddress();获取蓝牙设备mac地址 可以将数据使用EventBus进行需要传递
mLeDeviceListAdapter.addDevice(device);
mLeDeviceListAdapter.notifyDataSetChanged();
}
});
}
};
7.连接搜索到的蓝牙设备
通过listview的onItemClick方法获取到选择连接的某一个蓝牙设备,这里可以通过getRemoteDevice(address)获取设备,然后device.connectGatt(this, false, mGattCallback);正式连接蓝牙设备,连接后会返回一个BluetoothGatt 类型的对象,这里定义为mBluetoothGatt。该对象比较重要,后面发现服务读写设备等操作都是通过该对象。
代码里建了一个service,里面封装了连接,读写设备等操作。连接是通过获取到的mac地址去进行连接操作就可以了。
public boolean connect(final String address) {
if (mBluetoothAdapter == null || address == null) {
Log.w(TAG,"BluetoothAdapter not initialized or unspecified address.");
return false;
}
// Previously connected device. Try to reconnect. (先前连接的设备。 尝试重新连接)
if (mBluetoothDeviceAddress != null&& address.equals(mBluetoothDeviceAddress)&& mBluetoothGatt != null) {
Log.d(TAG,"Trying to use an existing mBluetoothGatt for connection.");
if (mBluetoothGatt.connect()) {
mConnectionState = STATE_CONNECTING;
return true;
} else {
return false;
}
}
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
mBluetoothGatt = device.connectGatt(this, false, mGattCallback);
Log.d(TAG, "Trying to create a new connection.");
mBluetoothDeviceAddress = address;
mConnectionState = STATE_CONNECTING;
return true;
}
8:连接后会回调BluetoothGattCallback接口,包括读取设备,往设备里写数据及设备发出通知等都会回调该接口。其中比较重要的是BluetoothGatt。
private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
@Override //当连接上设备或者失去连接时会回调该函数
public void onConnectionStateChange(BluetoothGatt gatt, int status,int newState) {
if (newState == BluetoothProfile.STATE_CONNECTED) { //执行到这里其实蓝牙已经连接成功了
mBluetoothGatt.discoverServices(); //连接成功后就去找出该设备中的服务
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) { //连接失败
//执行重新连接蓝牙设备逻辑
}
}
@Override //当设备是否找到服务时,会回调该函数
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) { //找到服务了
//在这里可以对服务进行解析,寻找到你需要的服务
} else {
Log.w(TAG, “onServicesDiscovered received: ” + status);
}
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
TLog.i(TAG, “onServicesDiscovered”);
} else {
TLog.i(TAG, "onServicesDiscovered status------>" + status);
}
}
@Override //当读取设备时会回调该函数
public void onCharacteristicRead(BluetoothGatt gatt,BluetoothGattCharacteristic characteristic, int status) {
System.out.println(“onCharacteristicRead”);
if (status == BluetoothGatt.GATT_SUCCESS) {
//读取到的数据存在characteristic当中,可以通过characteristic.getValue();函数取出。然后再进行解析操作。
//int charaProp = characteristic.getProperties();if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0)表示可发出通知。 判断该Characteristic属性
}
}
@Override //当向设备Descriptor中写数据时,会回调该函数
public void onDescriptorWrite(BluetoothGatt gatt,BluetoothGattDescriptor descriptor, int status) {
System.out.println("onDescriptorWriteonDescriptorWrite = " + status + ", descriptor =" + descriptor.getUuid().toString());
}
@Override //设备发出通知时会调用到该接口
public void onCharacteristicChanged(BluetoothGatt gatt,BluetoothGattCharacteristic characteristic) {
if (characteristic.getValue() != null) {
System.out.println(characteristic.getStringValue(0));
}
System.out.println("--------onCharacteristicChanged-----");
}
@Override
public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
System.out.println("rssi = " + rssi);
}
@Override //当向Characteristic写数据时会回调该函数
public void onCharacteristicWrite(BluetoothGatt gatt,BluetoothGattCharacteristic characteristic, int status) {
System.out.println("--------write success----- status:" + status);
};
}
9:设备连接成功并回调BluetoothGattCallback接口里面的onConnectionStateChange函数,然后调用mBluetoothGatt.discoverServices();去发现服务。发现服务后会回调BluetoothGattCallback接口里面的 onServicesDiscovered函数,在里面我们可以获取服务列表。
public List<BluetoothGattService> getSupportedGattServices() {
if (mBluetoothGatt == null)
return null;
return mBluetoothGatt.getServices(); //此处返回获取到的服务列表
}
10:获取到服务列表后自然就是要对服务进行解析。解析出有哪些服务,服务里有哪些Characteristic,哪些Characteristic可读可写可发通知等等。
private void displayGattServices(List<BluetoothGattService> gattServices) {
if (gattServices == null)
return;
for (BluetoothGattService gattService : gattServices) { // 遍历出gattServices里面的所有服务
List<BluetoothGattCharacteristic> gattCharacteristics = gattServices.getCharacteristics();
for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) { // 遍历每条服务里的所有Characteristic
if (gattCharacteristic.getUuid().toString().equalsIgnoreCase(需要通信的UUID)) {
// 有哪些UUID,每个UUID有什么属性及作用,一般硬件工程师都会给相应的文档。我们程序也可以读取其属性判断其属性。
// 此处可以可根据UUID的类型对设备进行读操作,写操作,设置notification等操作
// BluetoothGattCharacteristic gattNoticCharacteristic 假设是可设置通知的Characteristic
// BluetoothGattCharacteristic gattWriteCharacteristic 假设是可读的Characteristic
// BluetoothGattCharacteristic gattReadCharacteristic 假设是可写的Characteristic
}
}
}
}
11:可接收通知的UUID,设置其可以接收通知(notification)。下面函数参数为10中的gattNoticCharacteristic
public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic, boolean enabled) {
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
Log.w(TAG, “BluetoothAdapter not initialized”);
return;
}
mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID
.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
if (descriptor != null) {
System.out.println(“write descriptor”);
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mBluetoothGatt.writeDescriptor(descriptor);
}
}
12:可读的UUID。下面函数参数为10中的gattReadCharacteristic。readCharacteristic调用成功会回调步骤8中的onCharacteristicRead函数
public void readCharacteristic(BluetoothGattCharacteristic characteristic) {
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
Log.w(TAG, "BluetoothAdapter not initialized");
return;
}
mBluetoothGatt.readCharacteristic(characteristic);
}
13:可写的UUID。下面函数参数为10中的gattWriteCharacteristic。writeCharacteristic调用成功会回调步骤8中的onCharacteristicWrite函数
public void wirteCharacteristic(BluetoothGattCharacteristic characteristic) {
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
Log.w(TAG, "BluetoothAdapter not initialized");
return;
}
mBluetoothGatt.writeCharacteristic(characteristic);
}