BLE低功耗蓝牙设备开发

概述

Android API 18开始支持低功耗蓝牙设备(BLE,Bluetooth Low Energy)。BLE与以往的蓝牙设备不同,它长期处于sleep状态,只在有连接事件发生时才被唤醒,所以待机时间很长,适合用于穿戴设备等。

本文参考了这篇tutorial:http://toastdroid.com/2014/09/22/android-bluetooth-low-energy-tutorial/


相关概念

介绍与BLE开发相关的几个重要概念

GATT Profile

每个BLE设备都实现了一个多个profile,profile是定义手机与BLE通信方式的较高级别的概念。BLE的profile是基于Generic Attribute Profile (GATT)的,GATT描述了BLE中数据片(即Attribute)的传输方式。

Client

Client负责发出GATT命令,并接收结果。Android设备即为Client。

Server

Server负责接收GATT命令,并返回结果,BLE设备即为Server。

Characteristic

Characteristic表示Client和Server之间传输的数据。

Service

由若干个Characteristic组成,共同完成某一功能。

Descriptor

用于提供Characteristic的额外信息。

Attributes

Characteristic、Service、Descriptor都被称为Attributes,由128bit的UUID唯一表示。但开发者一般只需要关心其中的16个bit,即第5到第8个16进制位。

xxxxXXXX-xxxx-xxxx-xxxx-xxxxxxxxxxxx


在Android平台开发BLE

接下来是Android平台下的开发要点,再次提醒,需要API 18及以上。

AndroidManifest.xml

添加如下权限:

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

获得并启动BluetoothAdapter

BluetoothManager btManager = (BluetoothManager)getSystemService(Context.BLUETOOTH_SERVICE);

BluetoothAdapter btAdapter = btManager.getAdapter();
if (btAdapter != null && !btAdapter.isEnabled()) {
    Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);   
    startActivityForResult(enableIntent,REQUEST_ENABLE_BT);
}

扫描Device

得到BluetoothAdapter后,可以用一下方法开始/停止扫描BLE设备:

btAdapter.startLeScan(leScanCallback);
btAdapter.stopLeScan(leScanCallback);

其中leScanCallback是扫描到BLE设备的回调接口:

private BluetoothAdapter.LeScanCallback leScanCallback = new BluetoothAdapter.LeScanCallback() {
    @Override
    public void onLeScan(final BluetoothDevice device, final int rssi, final byte[] scanRecord) {
        // 对扫描到的Device进行操作,如建立GATT连接,获取Service、Characteristic等
    }
}

扫描到需要的设备后,应该尽快停止扫描,扫描是耗电量很大的操作。

定义BluetoothGattCallback

获得Device之后,下一步就是建立连接:

BluetoothGatt bluetoothGatt = bluetoothDevice.connectGatt(context, false, btleGattCallback);

其中btleGattCallback是监测连接状态的回调接口:

private final BluetoothGattCallback btleGattCallback = new BluetoothGattCallback() {

    @Override
    public void onCharacteristicChanged(BluetoothGatt gatt, final BluetoothGattCharacteristic characteristic) {
        // 发生characteristic读写操作时被调用
    }

    @Override
    public void onConnectionStateChange(final BluetoothGatt gatt, final int status, final int newState) { 
        // 连接建立或连接断开时被调用,建立连接后,可以开始获取Service等
        gatt.discoverServices();
    }

    @Override
    public void onServicesDiscovered(final BluetoothGatt gatt, final int status) { 
        // 当GATT调用了BluetoothGatt.discoverServices()方法时被回调
    }
}

获得Services和Characteristics

建立GATT连接后,获取GATT包含的BluetoothGattService,返回一个List<BluetoothGattService>

bluetoothGatt.discoverServices();

可以使用如下方法继续获得BluetoothGattService中的Characteristic

BluetoothGattService.getCharacteristics();

根据需要,可以对Characteristic进行读写操作:

Characteristic.setValue(byte[]);
BluetoothGatt.writeCharacteristic(Characteristic);

关闭

使用结束后关闭GATT:

bluetoothGatt.disconnect();
bluetoothGatt.close();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值