2015年6月24日 星期三

Android BLE App Development


Bluetooth and BLE

BLE standards for Bluetooth Low Energy, which is standardized as Bluetooth 4.0. It doesn't intended to replace or enhance classic Bluetooth (2.0/3.0). It is designed with a new protocol stack for the specific services/applications which required much lower power consumption for getting much longer lifetime. 



BLE defines following roles in GAP from the perspective of BLE .
- Peripheral device
The peripheral device broadcast
- Central device

I think you can find a lot of more information in the other public websites. Therefore the detail about BLE is not introduced here. The major focus here is to let you know BLE programming for Android.

BLE Working Flow

Generally the BLE programming on a smartphone or table works as a central device. It scans the peripheral devices and then connect to it. The connection then will be used to access the peripheral device's attributes/characteristics or wait for the notification from peripheral device. You can refer to the profile document to know more detail about how to access the attributes/characteristics.

Below I divided a complete BLE session into following pieces from the role of a central device.

1.      Scan BLE device
2.      Connect to a device
3.      Read/write BLE attributes (characteristics)
4.      Receive GATT notification (optional, based on the need of profile)

BLE Permission Declaration

Before starting the BLE programming, you have to declare enough permissions for  BLE operations on an Android device. Below are the permissions you should add in your App.

Declare the Bluetooth permission(s) in your application manifest file.

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


If you want to declare that your app is available to BLE-capable devices only, include the following in your app's manifest:

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


Android BLE API

Android starts to support BLE since 4.3 (API level 18). It introduced the Bluetooth LE related functions as below:
BLE scanning function was added in the existing class “BluetoothAdapter” which works originally for the traditional Bluetooth (2.0/3.0). More classes were also added to support BLE features  

Since Lollipop (API level 21), a new package “android.bluetooth.le” was introduced to address the BLE device scanning and advertising. The class “BluetoothLeScanner” is used to replace the Bluetooth LE scanning function

Device scanning:

(BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);

API level 18 ~ 20
BluetoothAdapter = BluetoothManager.getAdapter()
BluetoothAdapter.startLeScan()

API level 21 or later
BluetoothAdapter = BluetoothManager.getAdapter()
BluetoothLeScanner = BluetoothAdapter.getBluetoothLeScanner()
BluetoothLeScanner.startScan()

Device Connecting

To setup a connection, you need to create a service to receive the response of a service request.
Use bindService to bind this service with your activity. After it, the connectivity is managed by the event (intent) for connection, profile attributes, characteristic

That’s why we need to do 2 things before really starting the connecting.
-          Bind Service
-          Register Receivers to register all intents in the activities from the BLE service you bind.

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.

BluetoothDevice = BluetoothAdapter.getRemoteDevice() // by giving device address
BluetoothGatt = BluetoothDevice.connectGatt()

Speedup the reconnection
BluetoothGatt.connect()


Read/write Attributes

-          After connecting to the remote GATT server, you can request to get the service
BluetoothGattService = BluetoothGatt.getServices()
-          When you get the service, you can use it to get its characteristic to read or write according to the definition in the profile.
BluetoothGattCharacteristic = BluetoothGattService.getCharacteristic()

Here I use the diagram below to illustrate the relationship of the classes.




Receive GATT notification (optional)






1 則留言:

  1. Do not use a CD or pen drive received from another person unless you are certain that the disk does not contain a virus. scscan

    回覆刪除