您的位置:首页 > 产品设计 > UI/UE

Android Bluetooth Low Energy(Android低功耗蓝牙)

2017-03-10 22:51 423 查看


Android 4.3(API Level 18)开始引入Bluetooth
Low Energy(BLE,低功耗蓝牙)的核心功能并提供了相应的API,应用程序通过这些api可以扫描设备、查询services,读写设备的characteristics(属性特征)。对比传统的蓝牙,BLE的设计能够显著减低功耗。这让Android应用程序与BLE设备之间的低功耗通讯成为可能,例如距离传感器、心率监视器、健身设备等等。

1、关键术语和概念

1.1以下是关键BLE关键术语和概念的摘要:

                                             


如上图所示,使用低功耗蓝牙可以包括多个Profile,一个Profile中有多个Service,一个Service中有多个Characteristic,一个Characteristic中包括一个value和多个Descriptor。该图来自https://my.oschina.net/tingzi/blog/215008

   1、Generic Attribute Profile (GATT):GATT profile是用于通过BLE链路发送和接收称为“属性”的短数据段的一般规范。所有当前低能量应用配置文件均基于GATT。蓝牙技术联盟SIG已经为BLE设备定义了许多profiles。profile是一种特定应用程序定义设备如何工作的规范。请注意,一台设备可以实现多个profile。例如,一台设备可以包含心率监测器和电池水平检测器。
  

   2、Attribute Protocol(ATT,属性协议):GATT建立在属性协议(ATT)之上。这也称为GATT
/ ATT。 ATT针对BLE设备的运行进行了优化。为此,它尽可能使用更少的字节。每个属性由通用唯一标识符(UUID)唯一确定,UUID是一个标准化128位格式的字符串ID,用于唯一标识信息。属性通过ATT协议格式化为characteristics和services后进行传输。

   3、Characteristic:一个characteristic中包含一个值,以及0个或多个用于描述characteristic值的descriptor。characteristic可以被认为是一个类型,类似于类。

   4、Descriptor:Descriptor(描述符)中定义的属性用于描述一个characteristic值。例如,一个descriptor可以为一个characteristic的值指定一个在可接受范围内的可读性描述,或者为一个characteristic的值指定一个计量单位。  

  
   5、Service:一个service是一个characteristic的集合。例如,你可以持有一个名为“心率监视器”的service,它包含的characteristic例如“心率测量”。你可以在bluetooth.org上找到一系列基于GATT的profile和service。

1.2 角色和职能

以下是一台Android设备与BLE设备交互时的一些适用角色和职能:
 
中央设备和外围设备。这适用于BLE自身的连接。担任中央设备角色的设备负责扫描和搜索广告,担任外围设备的角色负责发送广告。
GATT服务端和GATT客户端。这取决于两台设备在建立连接后如何互相通信。

为了理解这之间的区别,想象你有一台Android手机和一台BLE设备作为活动追踪器。手机将担任中央设备角色;活动追踪器将担任外围设备角色(你需要具备两种角色才能建立一个BLE连接,两者都担任外围设备角色不能互相通信,同样两者都担任中央设备角色也不能互相通信)。

一旦手机和活动追踪器建立连接,它们就可以互相传输GATT媒体数据。根据它们传输的数据,其中一方需要担任服务端的角色。例如,如果活动追踪器想要发送传感器数据给手机,活动追踪器就需要担任服务端的角色。如果活动追踪器想要接收手机的数据,手机就需要担任服务端的角色。

在本片文档的例子中,Android应用程序(运行在Android设备上)是GATT客户端。应用程序从GATT服务端获取数据,这个服务端由支持Heart
Rate Profile的BLE心率监视器设备担任。但是你可以交替让你的Android应用程序扮演GATT服务端的角色。具体参考BluetoothGattService

 

2、BLE Permissions(BLE权限)

为了在你的应用程序中使用Bluetooth的功能,你必须声明android.permission.BLUETOOTH权限。你需要这个权限来执行一些蓝牙通信的操作,例如请求链接,接受连接,还有传输数据。

如果你想让你的应用程序进行设备扫描或者管理蓝牙设置,你必须同时声明android.permission.BLUETOOTH_ADMIN权限。注意,如果你使用BLUETOOTH_ADMIN权限,你必须同时声明BLUETOOTH权限。

在你的应用程序manifest文件中声明蓝牙权限,例如:

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


如果你想声明你的应用程序只能在支持BLE的设备上运行,可以将下面声明包含进你的应用程序manifest文件中:

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


然而,如果你想让你的应用程序也能够在不支持BLE的设备上运行,你就应该将上面标签中的属性设置为required="false"。然后在运行的过程中使用PackageManager.hasSystemFeature()方法来判断设备是否支持BLE:
// 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();
}




3、Requesting User Permissions(请求用户权限)

要从NETWORK_PROVIDER或GPS_PROVIDER接收位置更新,您必须通过在Android清单文件中分别声明{@code
ACCESS_COARSE_LOCATION}或{@code ACCESS_FINE_LOCATION}权限来请求用户的权限。没有这些权限,您的应用程式的位置更新要求会失败,并产生权限错误。

如果您同时使用NETWORK_PROVIDER和GPS_PROVIDER,则只需要请求{@code
ACCESS_FINE_LOCATION}权限,因为它包含了两个提供者的权限。
{@code ACCESS_COARSE_LOCATION}的权限仅允许访问NETWORK_PROVIDER。

警告:如果您的应用程式指定Android 5.0(API级别21)或更高版本,您必须声明应用程式在清单档案中使用android.hardware.location.network或android.hardware.location.gps硬体功能,这取决于您的应用程式从NETWORK_PROVIDER或GPS_PROVIDER接收位置更新。如果您的应用从这些位置提供商来源中获取位置信息,则需要声明该应用在您的应用清单中使用了这些硬件功能。在运行Android
5.0(API 21)之前的版本的设备上,请求{@code ACCESS_FINE_LOCATION}或{@code
ACCESS_COARSE_LOCATION}权限包含对位置硬件功能的隐含请求。但是,请求这些权限不会自动请求Android 5.0(API级别21)及更高版本上的位置硬件功能。

以下代码示例演示如何在从设备的GPS读取数据的应用的清单文件中声明权限和硬件功能:

<manifest ... >
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
...
<!-- Needed only if your app targets Android 5.0 (API level 21) or higher. -->
<uses-feature android:name="android.hardware.location.gps" />
...
</manifest>


4、Setting
Up BLE(设置BLE)

在你的应用程序通过BLE进行通信之前,你需要确认设备是否支持BLE。如果支持,还要再确认是否已经启用。注意这个检查步骤只有在<uses-festure.../>设置为false的情况下才需要执行。

如果不支持BLE,你应该优雅的禁止一些使用BLE的功能。如果支持BLE,但是目前禁用了,那么你需要在不离开你的应用程序状态下,请求用户启用蓝牙用能。这个过程需要使用BluetoothAdapter,分两个步骤完成:

4.1获取BluetoothAdapter

基本上所有使用蓝牙的activity都需要BluetoothAdapter。BluetoothAdapter代表了设备本身的蓝牙适配器(蓝牙发送接收器)。在整个系统中有一个BluetoothAdapter对象,你的应用程序可以使用这个对象进行交互。下面的代码片段展示了如果获取这个适配器。注意下面的这种方法使用getSystemService()方法来获取一个BluetoothManager实例,之后再通过BluetoothManager获取BluetoothAdapter。Android
4.3(API Level 18)才开始支持BluetoothManager:

private BluetoothAdapter mBluetoothAdapter;
...
// Initializes Bluetooth adapter.
final BluetoothManager bluetoothManager =
(BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter();

4.2启用蓝牙

下一步,你需要确保蓝牙已经启动。调用isEnable()方法来检查蓝牙当前是否已经启用。如果方法返回false,说明蓝牙被禁用了。下面的代码片段中检查蓝牙是否已经启用。如果没有启用,代码片段会显示一个错误提示用户去设置中启用蓝牙:
// Ensures Bluetooth is available on the device and it is enabled. If not,
// displays a dialog requesting user permission to enable Bluetooth.
if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}


注意:传递给startActivityForResult的REQUEST_ENABLE_BT常量(android.content.Intent,int)是系统在您的onActivityResult(int,int,android.content)中传回给您的本地定义的整数(必须大于0)。Intent)实现作为requestCode参数

5、Finding BLE Devices(搜索BLE设备)

搜索BLE设备,你可以使用startLeScan()方法。这个方法需要一个BluetoothAdapter.LeScanCallback对象作为参数。你必须实现这个callback接口,因为扫描的结果会通过这个接口返回。由于搜索设备是比较耗电的操作,你应该遵循以下指南使用:

*  一旦你找到目标设备,应该马上停止搜索。
*  不要死循环搜索,并设置搜索的最长时间。一台以前可以访问的设备可能已经移出了可检测范围,继续扫描只会消耗电量。

下面的代码片段展示了如何开始和停止搜索:
/**
* Activity for scanning and displaying available BLE devices.
*/
public class DeviceScanActivity extends ListActivity {

private BluetoothAdapter mBluetoothAdapter;
private boolean mScanning;
private Handler mHandler;

// Stops scanning after 10 seconds.
private static final long SCAN_PERIOD = 10000;
...
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);
}
}, SCAN_PERIOD);

mScanning = true;
mBluetoothAdapter.startLeScan(mLeScanCallback);
} else {
mScanning = false;
mBluetoothAdapter.stopLeScan(mLeScanCallback);
}
...
}
...
}

如果你只想搜索指定类型的外围设备,你可以替换成startLeScan(UUID[], BluetoothAdapter.LeScanCallback)方法,并提供一个你的应用程序所支持的GATT服务的UUID对象数组。

下面是一个BluetoothAdapter.LeScanCallback的实现,它是一个用于接收BLE搜索结果的接口:
private LeDeviceListAdapter mLeDeviceListAdapter;
...
// Device scan callback.
private BluetoothAdapter.LeScanCallback mLeScanCallback =
new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, int rssi,
byte[] scanRecord) {
runOnUiThread(new Runnable() {
@Override
public void run() {
mLeDeviceListAdapter.addDevice(device);
mLeDeviceListAdapter.notifyDataSetChanged();
}
});
}
};

注意:正如Bluetooth文档中所描述的,在同一个时间你只能搜索BLE设备或者搜索传统蓝牙设备。你不能同时搜索BLE设备和传统蓝牙设备。

6、Connecting
to a GATT Server(连接一个GATT服务)

与BLE设备交互的第一步就是要连接上它——更准确的说,是连接设备上的GATT服务。连接BLE设备上的GATT服务,你可以使用connectGatt()方法。这个方法需要三个参数:一个Context对象,autoConnect(一个表示是否当BLE设备可访问时马上自动连接的boolean值),还有一个BluetoothGattCallback:

mBluetoothGatt = device.connectGatt(this, false, mGattCallback);

上面的代码会连接BLE设备管理的GATT服务,并返回一个BluetoothGatt实例,通过这个实例就可以执行GATT客户端的相关操作。这个调用者(Android应用程序)就是GATT客户端。里面的BluetoothGattCallback对象用于交付操作结果给客户端,例如连接状态,还有将来一些GATT客户端操作的结果。

在这个例子中,BLE应用程序提供了一个activity(DeviceControlActivity)来连接、显示数据,和显示BLE设备所支持的GATT的service以及characteristic。基于用户的输入,这个activity会和一个名为BluetoothLeService的Service通信,这个service通过Android
BLE的API与BLE设备进行交互。
// A service that interacts with the BLE device via the Android BLE API.
public class BluetoothLeService extends Service {
private final static String TAG = BluetoothLeService.class.getSimpleName();

private BluetoothManager mBluetoothManager;
private BluetoothAdapter mBluetoothAdapter;
private String mBluetoothDeviceAddress;
private BluetoothGatt mBluetoothGatt;
private int mConnectionState = STATE_DISCONNECTED;

private static final int STATE_DISCONNECTED = 0;
private static final int STATE_CONNECTING = 1;
private static final int STATE_CONNECTED = 2;

public final static String ACTION_GATT_CONNECTED =
"com.example.bluetooth.le.ACTION_GATT_CONNECTED";
public final static String ACTION_GATT_DISCONNECTED =
"com.example.bluetooth.le.ACTION_GATT_DISCONNECTED";
public final static String ACTION_GATT_SERVICES_DISCOVERED =
"com.example.bluetooth.le.ACTION_GATT_SERVICES_DISCOVERED";
public final static String ACTION_DATA_AVAILABLE =
"com.example.bluetooth.le.ACTION_DATA_AVAILABLE";
public final static String EXTRA_DATA =
"com.example.bluetooth.le.EXTRA_DATA";

public final static UUID UUID_HEART_RATE_MEASUREMENT =
UUID.fromString(SampleGattAttributes.HEART_RATE_MEASUREMENT);
// Various callback methods defined by the BLE API.
private final BluetoothGattCallback mGattCallback =
new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status,
int newState) {
String intentAction;
if (newState == BluetoothProfile.STATE_CONNECTED) {
intentAction = ACTION_GATT_CONNECTED;
mConnectionState = STATE_CONNECTED;
broadcastUpdate(intentAction);
Log.i(TAG, "Connected to GATT server.");
Log.i(TAG, "Attempting to start service discovery:" +
mBluetoothGatt.discoverServices());

} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
intentAction = ACTION_GATT_DISCONNECTED;
mConnectionState = STATE_DISCONNECTED;
Log.i(TAG, "Disconnected from GATT server.");
broadcastUpdate(intentAction);
}
}
@Override
// New services discovered
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);
} else {
Log.w(TAG, "onServicesDiscovered received: " + status);
}
}
@Override
// Result of a characteristic read operation
public void onCharacteristicRead(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic,
int status) {
if (status == BluetoothGatt.GATT_SUCCESS) {
broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
}
}
...
};
...
}

当一个特定的的回调方法被调用的时候,它就会适当调用broadcastUpdate()帮助方法并传递一个操作标识。注意本节中的数据是根据蓝牙心率测量的profile规范解析的:
private void broadcastUpdate(final String action) {
final Intent intent = new Intent(action);
sendBroadcast(intent);
}
private void broadcastUpdate(final String action,
final BluetoothGattCharacteristic characteristic) {
final Intent intent = new Intent(action);
// This is special handling for the Heart Rate Measurement profile. Data
// parsing is carried out as per profile specifications.
if (UUID_HEART_RATE_MEASUREMENT.equals(characteristic.getUuid())) {
int flag = characteristic.getProperties();
int format = -1;
if ((flag & 0x01) != 0) {
format = BluetoothGattCharacteristic.FORMAT_UINT16;
Log.d(TAG, "Heart rate format UINT16.");
} else {
format = BluetoothGattCharacteristic.FORMAT_UINT8;
Log.d(TAG, "Heart rate format UINT8.");
}
final int heartRate = characteristic.getIntValue(format, 1);
Log.d(TAG, String.format("Received heart rate: %d", heartRate));
intent.putExtra(EXTRA_DATA, String.valueOf(heartRate));
} else {
// For all other profiles, writes the data formatted in HEX.
final byte[] data = characteristic.getValue();
if (data != null && data.length > 0) {
final StringBuilder stringBuilder = new StringBuilder(data.length);
for(byte byteChar : data)
stringBuilder.append(String.format("%02X ", byteChar));
intent.putExtra(EXTRA_DATA, new String(data) + "\n" +
stringBuilder.toString());
}
}
sendBroadcast(intent);
}

回到DeviceControlActivity,下面的事件通过一个BroadcaseReceiver处理:
// Handles various events fired by the Service.
// ACTION_GATT_CONNECTED: connected to a GATT server.
// ACTION_GATT_DISCONNECTED: disconnected from a GATT server.
// ACTION_GATT_SERVICES_DISCOVERED: discovered GATT services.
// ACTION_DATA_AVAILABLE: received data from the device. This can be a
// result of read or notification operations.
private final BroadcastReceiver mGattUpdateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (BluetoothLeService.ACTION_GATT_CONNECTED.equals(action)) {
mConnected = true;
updateConnectionState(R.string.connected);
invalidateOptionsMenu();
} else if (BluetoothLeService.ACTION_GATT_DISCONNECTED.equals(action)) {
mConnected = false;
updateConnectionState(R.string.disconnected);
invalidateOptionsMenu();
clearUI();
} else if (BluetoothLeService.
ACTION_GATT_SERVICES_DISCOVERED.equals(action)) {
// Show all the supported services and characteristics on the
// user interface.
displayGattServices(mBluetoothLeService.getSupportedGattServices());
} else if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)) {
displayData(intent.getStringExtra(BluetoothLeService.EXTRA_DATA));
}
}
};

7、Reading BLE Attribute(读取BLE属性)

一旦你的Android应用程序连接上了一个GATT服务并且发现了设备上的service,就可以在支持读写的地方读写属性。例如,下面的代码片段通过迭代服务的service和characteristic,并将它们显示在界面上:
public class DeviceControlActivity extends Activity {
...
// Demonstrates how to iterate through the supported GATT
// Services/Characteristics.
// In this sample, we populate the data structure that is bound to the
// ExpandableListView on the UI.
private void displayGattServices(List<BluetoothGattService> gattServices) {
if (gattServices == null) return;
String uuid = null;
String unknownServiceString = getResources().
getString(R.string.unknown_service);
String unknownCharaString = getResources().
getString(R.string.unknown_characteristic);
ArrayList<HashMap<String, String>> gattServiceData =
new ArrayList<HashMap<String, String>>();
ArrayList<ArrayList<HashMap<String, String>>> gattCharacteristicData
= new ArrayList<ArrayList<HashMap<String, String>>>();
mGattCharacteristics =
new ArrayList<ArrayList<BluetoothGattCharacteristic>>();

// Loops through available GATT Services.
for (BluetoothGattService gattService : gattServices) {
HashMap<String, String> currentServiceData =
new HashMap<String, String>();
uuid = gattService.getUuid().toString();
currentServiceData.put(
LIST_NAME, SampleGattAttributes.
lookup(uuid, unknownServiceString));
currentServiceData.put(LIST_UUID, uuid);
gattServiceData.add(currentServiceData);

ArrayList<HashMap<String, String>> gattCharacteristicGroupData =
new ArrayList<HashMap<String, String>>();
List<BluetoothGattCharacteristic> gattCharacteristics =
gattService.getCharacteristics();
ArrayList<BluetoothGattCharacteristic> charas =
new ArrayList<BluetoothGattCharacteristic>();
// Loops through available Characteristics.
for (BluetoothGattCharacteristic gattCharacteristic :
gattCharacteristics) {
charas.add(gattCharacteristic);
HashMap<String, String> currentCharaData =
new HashMap<String, String>();
uuid = gattCharacteristic.getUuid().toString();
currentCharaData.put(
LIST_NAME, SampleGattAttributes.lookup(uuid,
unknownCharaString));
currentCharaData.put(LIST_UUID, uuid);
gattCharacteristicGroupData.add(currentCharaData);
}
mGattCharacteristics.add(charas);
gattCharacteristicData.add(gattCharacteristicGroupData);
}
...
}
...
}

8、Receiving GATT Notification(接收GATT通知)

BLE应用程序要求在设备的某个指定characteristic改变的时候接收到通知是很常见。下面的代码片段展示了如何通过使用setCharacteristicNotification()为一个characteristic设置通知:
private BluetoothGatt mBluetoothGatt;
BluetoothGattCharacteristic characteristic;
boolean enabled;
...
mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
...
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(
UUID.fromString(SampleGattAttributes.CLIENT_CHARACTERISTIC_CONFIG));
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
mBluetoothGatt.writeDescriptor(descriptor);

一旦为一个characteristic启用了通知,当远程设备上的characteristic改变的时候就会触发onCharacteristicChanged()方法:
@Override
// Characteristic notification
public void onCharacteristicChanged(BluetoothGatt gatt,
        BluetoothGattCharacteristic characteristic) {
    broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
}

9、Closing the Client
App(关闭客户端应用程序)

一旦你的应用程序使用完BLE设备,你应该调用close()方法,这样系统才能适当释放占用的资源:
public void close() {
    if (mBluetoothGatt == null) {
        return;
    }
    mBluetoothGatt.close();
    mBluetoothGatt = null;
}


原地址:https://developer.android.com/guide/topics/connectivity/bluetooth-le.html


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息