+ All Categories
Home > Engineering > Connect your Android to the real world with Bluetooth Low Energy

Connect your Android to the real world with Bluetooth Low Energy

Date post: 02-Jul-2015
Category:
Upload: gabor-paller
View: 5,791 times
Download: 4 times
Share this document with a friend
Description:
This is my presentation at the IoT Londroid Meetup, 2014 nov. 19. It presents 2 projects employing BLE and Android devices.
35
Connect your Android to the real world with Bluetooth Low Energy Gabor Paller [email protected] Sfonge Ltd. & LRG Ltd. http://www.sfonge.com http://www.lrg.hu/
Transcript
Page 1: Connect your Android to the real world with Bluetooth Low Energy

Connect your Android to the real world with Bluetooth Low Energy

Gabor [email protected] Ltd. & LRG Ltd.http://www.sfonge.com

http://www.lrg.hu/

Page 2: Connect your Android to the real world with Bluetooth Low Energy

What has changed?

● Let's take this sentence apart:Connecting handheld devices with short-range radio for remote control/sensing …

Page 3: Connect your Android to the real world with Bluetooth Low Energy

The singularity is near ...

● Radio standard: Single, widely deployed short-range communication protocol specially tailored to devices ...

Page 4: Connect your Android to the real world with Bluetooth Low Energy

The singularity is near ...

● Access devices:Ubiquitous, popular portable devices providing great user experience ...

Censored

Page 5: Connect your Android to the real world with Bluetooth Low Energy

The singularity is near ...

● Easy integration:Hardware modules that can be easily integrated into existing electronics to make those things accessible from our devices ...

Page 6: Connect your Android to the real world with Bluetooth Low Energy

Disruption, hockey stick, synergy …

Just kidding :-)

Page 7: Connect your Android to the real world with Bluetooth Low Energy

Food chain 1.

Cloud Access deviceThing

Wide-arearadio network(e.g. GSM)

Internet (fixed or mobile)

Page 8: Connect your Android to the real world with Bluetooth Low Energy

Food chain 2.

CloudAccess deviceThing

Short-rangeradio network

(e.g. Bluetooth)Internet

(fixed or mobile)

Page 9: Connect your Android to the real world with Bluetooth Low Energy

Food chain 2a.

Cloud

Access device

Thing

Short-rangeradio network

(e.g. Bluetooth)Internet

(fixed or mobile)

Gateway

Internet (fixed or mobile)

Page 10: Connect your Android to the real world with Bluetooth Low Energy

From this point I will talk about BLE only

Page 11: Connect your Android to the real world with Bluetooth Low Energy

Bluetooth Low Energy

● BLE is a variant of Bluetooth that is optimized for:● Low power consumption of the edge sensors● Short, low-delay messages

● Differences● Radio layer is incompatible with Bluetooth Classic● Application layer is built on attribute manipulation

– Attribute: key-value pair

Page 12: Connect your Android to the real world with Bluetooth Low Energy

Attributes

● BLE applications communicate by means of attributes

● An attribute has:● Type (e.g. org.bluetooth.characteristic.heart_rate_measurement,

UUID:0x2A37)● Handle – used to identify the attribute● Value – according to the type, can be an array

● Attribute plus meta-info is called characteristic.

● Characteristics are used to compose services.

Page 13: Connect your Android to the real world with Bluetooth Low Energy

Client and server

Page 14: Connect your Android to the real world with Bluetooth Low Energy

ATT operations – client-initiated

● Find Information Request – get attribute handles/types in handle range● Find By Type Value Request – get attribute handles that match a type value● Read By Type Request – get attribute values where the type is known● Read Request – get attribute value by handle● Read Blob Request – read specific part of a long attribute value● Read Multiple Request – read two or more attributes● Read by Group – read attributes belonging to a group● Write Request – write an attribute● Signed Write Request – write an attribute, protected by digital signature● Prepare Write Request – write an attribute, waiting for a commit● Execute Write Request – commit prepared write requests

Page 15: Connect your Android to the real world with Bluetooth Low Energy

ATT operations – server-initiated

● Handle value notification – server sends the current value of an attribute

● Handle value indication – server sends the current value of an attribute, expects a confirmation response

Page 16: Connect your Android to the real world with Bluetooth Low Energy

Generic Attribute Profile (GATT)

● Composition of services from attributes of given types.● Descriptor: attribute that adds meta-info about a

characteristic's value● Characteristic: single value, any number of

descriptors.● Service: set of characteristics.

● GATT profiles: set of standard services.

Page 17: Connect your Android to the real world with Bluetooth Low Energy

RFDuino

● Arduino+BLE modem● BLE programming is

hidden behind an Arduino-based application model

● Uses its own proprietary service, cannot be used to implement a standard GATT service.

Page 18: Connect your Android to the real world with Bluetooth Low Energy

Ready for development

Page 19: Connect your Android to the real world with Bluetooth Low Energy

Easy to integrate

Page 20: Connect your Android to the real world with Bluetooth Low Energy

First project

https://www.youtube.com/watch?v=iYWIzbJK81U

Page 21: Connect your Android to the real world with Bluetooth Low Energy

Connectionless

● BLE has space in its advertisement packets for application-specific data.

● The whole advertisement packet is 31 bytes long● 1 byte length field● Local Name: 2 bytes overhead+name characters● Service Data: 4 bytes overhead+service data payload

● With 1 character local name, max. length of payload: 23 bytes

● RFDuino advertisement packet max. length of payload: 15 bytes

Page 22: Connect your Android to the real world with Bluetooth Low Energy

RFDuino side

● void loop() { … RFduinoBLE.deviceName = gasSensorName; RFduinoBLE.advertisementData = tbuffer;

// start the BLE stack RFduinoBLE.begin(); // advertise for duration milliseconds Rfduino_ULPDelay(duration);

// stop the BLE stack RfduinoBLE.end();}

Page 23: Connect your Android to the real world with Bluetooth Low Energy

Android side

● Start BLE scan with callback: mBluetoothAdapter.startLeScan(mLeScanCallback);

● In the callback obtain the advertisement data:public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) 

● Use device to obtain peripheral's info like device name or address.

● Use rssi (received signal strength indicator) to estimate the distance of the peripheral from the access device.

● Use scanRecord to retrieve the service data including the measurement from the sensor.

Page 24: Connect your Android to the real world with Bluetooth Low Energy

Second project

https://www.youtube.com/watch?v=IWID35cOLZg

Page 25: Connect your Android to the real world with Bluetooth Low Energy

Connection-oriented

● RFDuino cannot be used with its default firmware to implement any standard GATT profile.

● It uses its own proprietary profile.● This solution also has a huge advantage:

Arduino code does not have to deal with BLE artifacts like characteristics, etc.

Page 26: Connect your Android to the real world with Bluetooth Low Energy

RFDuino characteristics

● RFDuino server exposes “read” and “write” characteristics.

● If you want to send data to the RFDuino, write the “write” characteristic.

● Data from the other end is received from the “read” characteristic.● It is possible to subscribe for incoming data

● There is a “disconnect” characteristic. When “disconnect” is written by the access device, the peripheral disconnects.

Page 27: Connect your Android to the real world with Bluetooth Low Energy

RFDuino side

● void RFduinoBLE_onConnect() - invoked when the other end tries to connect to the peripheral.

● void RFduinoBLE_onDisconnect() - invoked when the other end disconnects.

● void RFduinoBLE_onReceive(char *data, int len) – invoked when data is sent from the other end.

● RFduinoBLE.send(v) – send data to the other end.

Page 28: Connect your Android to the real world with Bluetooth Low Energy

Android side

● Steps:● Discover● Connect● Discover services and retrieve the RFDuino service● Retrieve RFDuino characteristics from the RFDuino

service● Use the characteristics to send and receive data● Write the “disconnect” characteristic● Disconnect

Page 29: Connect your Android to the real world with Bluetooth Low Energy

Discover

● bluetoothAdapter.startLeScan(leScanCallback);● public void onLeScan(final BluetoothDevice

device, int rssi, byte[] scanRecord) { ...

Page 30: Connect your Android to the real world with Bluetooth Low Energy

Connect

● device.connectGatt(MainActivity.this, false, gattCallback);

● public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {

if( newState == BluetoothProfile.STATE_DISCONNECTED ) { ...

} elseif( newState == BluetoothProfile.STATE_CONNECTED) {

…}

Page 31: Connect your Android to the real world with Bluetooth Low Energy

Discover services

● boolean success = gatt.discoverServices();

● public void onServicesDiscovered(BluetoothGatt gatt, int status) { if (status == BluetoothGatt.GATT_SUCCESS) { gattService = gatt.getService(RFDUINO_SERVICE_UUID); receiveCharacteristic = gattService.getCharacteristic( RECEIVE_CHARACTERISTIC_UUID ); sendCharacteristic = gattService.getCharacteristic(SEND_CHARACTERISTIC_UUID); disconnectCharacteristic = gattService.getCharacteristic(DISCONNECT_CHARACTERISTIC_UUID); gatt.setCharacteristicNotification(receiveCharacteristic, true); BluetoothGattDescriptor receiveConfigDescriptor = receiveCharacteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIGURATION_UUID); receiveConfigDescriptor.setValue( BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);

gatt.writeDescriptor(receiveConfigDescriptor);

Page 32: Connect your Android to the real world with Bluetooth Low Energy

Communicate

● sendCharacteristic.setValue( MASK_MOTOR_RIGHT, BluetoothGattCharacteristic.FORMAT_UINT8,0);

● public void onCharacteristicChanged (BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {

Page 33: Connect your Android to the real world with Bluetooth Low Energy

Disconnect

● disconnectCharacteristic.setValue("");

● disconnectCharacteristic.setWriteType( BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);

● gatt.writeCharacteristic(disconnectCharacteristic);

● gatt.disconnect();

Page 34: Connect your Android to the real world with Bluetooth Low Energy

Resources

● Connectionless:http://mylifewithandroid.blogspot.hu/2014/09/gas-sensor-prototype-explained.html

● Connection-oriented:http://www.sfonge.com/forum/topic/motor-boat-control-bluetooth-low-energy

● Hamsters: contact personally

Page 35: Connect your Android to the real world with Bluetooth Low Energy

Questions?


Recommended