Best Practices and what's new in Android 5files.meetup.com/17404842/Android+BLE_DarioLaverde...BLE...

Post on 02-Jan-2021

2 views 0 download

Transcript of Best Practices and what's new in Android 5files.meetup.com/17404842/Android+BLE_DarioLaverde...BLE...

Android and BLE: Best Practices and

what's new in Android 5.0

google.com/+DarioLaverde HTC Developer Evangelist

Thurs, Jan 29, 2015

First, a developer challenge:

WiFi: HTC_Guest pwd: https://www.htcdev.com/devcenter/opensense-sdk/bluetooth-smart/htc-fetch/ or: http://bit.ly/htcfetch

Android & “The Internet of Things”

connecting to devices from Android: wifi, bluetooth, nfc, … and now BLE and IR BLE API: Bluetooth Low Energy (as of Android 4.3) IR API: Consumer InfraRed (as ofAndroid 4.4!) and the things are getting smaller: stickers, keyfobs, remotes… and wearable: Android Wear, Google Glass….

IoT (or IoE) - Wearables

IoT / Wearables: the next big thing

Hype Cycle for Emerging Technologies, 2014

What is BLE?

●  Started in 2001 - originally introduced under the name Wibree by Nokia in 2006 - merged into the main Bluetooth standard in 2010, when Bluetooth Core Specification Version 4.0 was adopted

●  Low power and improved latency ●  Same radio+antenna so older devices technically could be

retrofitted with the le stack (however requires chipset firmware update)

●  BLE is a part of the Bluetooth 4.1 specification

BT Classic vs BT Low Energy

BT Classic vs BT Low Energy

Bluetooth 4.1 http://developer.bluetooth.org/TechnologyOverview/Pages/v4.aspx

The core spec consists of several parts: ●  Classic Bluetooth technology ●  Bluetooth low energy technology ●  Bluetooth high speed technology

Bluetooth SMART

2011 BT Smart branding:

●  Bluetooth SMART (LE only) ●  Bluetooth SMART Ready (dual mode support)

Bluetooth LE Use Cases Initially we’re seeing Health & Wellness / Sport & Fitness and Proximity use cases – but there’s many more possibilities…

BLE Profiles BLE Profiles: optional specifications for particular set of applications – since they are optional you can create custom ones. http://developer.bluetooth.org/TechnologyOverview/Pages/Profiles.aspx#GATT

BLE Profiles BLE device manufacturers are encouraged by the Bluetooth SIG to build new profiles and even extend current ones with additional “proprietary” characteristics and services. Creates difficulty debugging certain devices for 3rd party app developers without contacting device OEM or reverse engineering.

BLE and Android Prior to Android 4.3, several OEMs already have devices on the market that support BLE and each provide their own BLE API. HTC, Samsung, Motorola and others

In most if not all cases, you will build on top of the GATT profile – i.e. your app must include the implementation of your required BLE profile(s) - similar to IOS.

BLE Profiles are on top of GATT | V

BLE Protocols BLE GATT Clients and Servers: Servers expose attributes Clients use attributes Client Actions: Discover, Read, Write, Confirm an Indication Server Actions: respond to Client actions, send Notifications

BLE Protocols

Phone (Master – Central Role) Gatt Client

BLE Smart device (Slave – Peripheral Role) Gatt Server

Service Characteristic: value descriptor(s) Observer Role

BLE Protocols Phone (Master – Central Role) Gatt Client

*Phone (Slave – Peripheral Role) Gatt Server

Service

*New support in Android 5.0 (Lollipop)

Characteristic: value descriptor(s) Observer Role

Broadcaster Role

a small sampling of crowd-sourced BLE startups

cookoo http://www.kickstarter.com/projects/cuckoo/cookootm-the-watch-for-the-connected-generation

StickNFind http://www.indiegogo.com/projects/sticknfind-bluetooth-powered-ultra-small-location-stickers atomwear https://www.kickstarter.com/projects/343910040/atomwear

now some BLE devices to demo

HTC Fetch

HTC Fetch on the Play Store

HTC Fetch Open BLE Specs From htcdev.com: BLE Profiles: Find Me Profile, Proximity Profile, Battery Service The only addition HTC has added to the standard BLE profiles and service listed above is the following additional service: Service uuid: 0daa5375-02d3-4b47-b6b7-53408ff159e5 Characteristic (Notify) uuid: 1daa5375-02d3-4b47-b6b7-53408ff159e5 This is the service used to enable the "find my phone" feature of HTC Fetch, the characteristic notification sent when the tag's button is pressed.

RE camera

RE camera ● BLE + WIFI Direct

●  BLE

control, connection management ●  WIFI

media, streaming

●  APIs forthcoming

Thalmic Lab’s Myo a BLE gesture controlled armband

Kwikset Kevo

Kwikset Kevo

BLE developer device kits BLE chipset OEMs provide device kits – supporting different profiles The TI SensorTag kit (BLE chipset CC2541) contains 6 sensors and won the best developer kit award of the year by EE Times / EDN Ace Awards. Considered best value for a BLE dev kit at only $25

TI SensorTag http://ti.com/ble

TI SensorTag

Bluetooth SIG Developer Portal

http://developer.bluetooth.org

Specifications, sample code, developer resources, videos of webinars and much more…

Beacon support pre-5.0 and 5.0

pre-5.0 (demo with Google Glass) https://github.com/RadiusNetworks/android-ibeacon-service pre-5.0 and 5.0 https://github.com/AltBeacon https://github.com/AltBeacon/android-beacon-library/blob/android-l-apis/src/

main/java/org/altbeacon/beacon/BeaconTransmitter.java

BLE overview source: https://developer.android.com/guide/topics/connectivity/bluetooth-le.html Setup BLE:

BLE Scanning

Connect to a Gatt Server

note: when autoconnecting First connect directly initially (false) and on disconnect then connect with autoconnect = true

service discovery, callbacks

reading characteristics In this case for every service

Receiving Gatt Notifications

Running as a Gatt Server

from BluetoothManager: see api docs for BluetoothGattServer (next slide)

And don’t forget to close!

Sample Code Demo BluetoothLeGatt official Android BLE sample project: BluetoothLeGattExtend adds more services, use cases (work in progress) AltBeacon/android-beacon-library open source Android 5 supported library

BLE demos BLE demos based on sample code from: - Android SDK - developer.bluetooth.org - Ti.com/ble - bit.ly/htcfetch

Android 5.0 A diff shows about 140+ APIs in the new BLE package alone Most important feature: Peripheral Mode Support! - BLE clients and servers with phones - Phones as devices that run Central Mode Broadcaster (advertising) and Observer (scanning) Roles supported and optimized!

check the API diffs!

● check diffs from API Level “L Preview” to API Level 21 (shown)

● also check diffs from API Level 20 to

API Level 21

Optimizing for Power in Android 5.0

for both Scanning and Advertising

android.bluetooth.le

Limitations? 40 characteristics 10 devices 15? notifications see:https://android.googlesource.com/platform/external/bluetooth/bluedroid/+/android-5.0.0_r5/bta/gatt/bta_gattc_int.h

some tools to debug Needs dedicated sniffer (more than one to cover advertising on several channels) see open source solution: http://ubertooth.sourceforge.net/hardware/one/

Enabling Bluetooth HCI packet Capture

-> /sdcard/btsnoop_hci.log

Some BLE Development Best Practices - don’t treat the device like a classic BT device! (won’t switch modes as quickly - remember it’s low powered) - acknowledge reads, writes (handle all callbacks) (yes the spec implies asynchronous but not always in practice) - implement retries, handle timeouts - store and manage device addresses to handle reconnects and avoid scanning each time

Some BLE Development Best Practices -­‐  Test against all 4.3+ devices…. include oem workarounds -­‐  When connecting multiple devices - each has associated

gatt object!

-­‐  make a decision about pre vs post 4.4.3 (major bug fixes include mac address pool exhaustion)

-­‐  NEW possible issues with BLE in Android 5.0

What’s new in Bluetooth 4.1? L2CAP Dedicated Channels is a foundational feature of Bluetooth 4.1 (for future IPv6 support) Dual Mode Topology and Link Layer Topology Software Features (act as a Bluetooth Smart Ready Hub and peripheral at the same time) Low Duty Cycle Directed Advertising, L2CAP Connection Oriented Channels (improves efficiency)

Just finalized: Bluetooth 4.2 Power and Privacy features: - Low-power IP connectivity over Bluetooth with a new profile which supports IPv6 and 6LoWPAN - Allows MAC address of Bluetooth devices to be masked unless connecting to a trusted device. At the same time, the new devices will have additional refinements to reduce power consumption as part of the Low Energy Privacy 1.2 spec

BLE Resources and references https://d.android.com/guide/topics/connectivity/bluetooth-le.html http://processors.wiki.ti.com/index.php/Bluetooth_SensorTag http://www.htcdev.com/devcenter/opensense-sdk/bluetooth-smart http://developer.bluetooth.org https://github.com/AltBeacon

Q & A

contact: http://google.com/+DarioLaverde