gerdavax AT gmail DOT com Stefano Sanna Bluetooth Low Energy: la rivoluzione silenziosa dell’ambient intelligence
Jul 17, 2015
gerdavax AT gmail DOT com
Stefano SannaBluetooth Low Energy: la rivoluzione silenziosa dell’ambient intelligence
• Presentazioni !
• Ambient Intelligence !
• Bluetooth • Genesi ed evoluzioni • Bluetooth Low Energy • Playground
• FAQ & Conclusioni
gerdavax AT gmail DOT comBluetooth Low Energy
Agenda
• Responsabile Android Lab @ Open Reply !
• Co-autore di “Android Programmazione Avanzata”(con E. Di Saverio, Ed. FAG) !
• Autore del libro “Java Micro Edition” (Hoepli) !
• Supporter @ Codemotion since 2005 (JIP Day!)
gerdavax AT gmail DOT comBluetooth Low Energy
Speaker
gerdavax AT gmail DOT comBluetooth Low Energy
Slide’s title
Cablaggi nella metropolitana di Londra
gerdavax AT gmail DOT comBluetooth Low Energy
2002: quattro mani per usare un PDA con touchscreen resistivo (con pennino), modem 2G seriale, GPS seriale e fotocamera da 1.5MPX)
gerdavax AT gmail DOT comBluetooth Low Energy
In computing, ambient intelligence (AmI) refers to electronic environments that are sensitive and responsive to the presence of people. Ambient intelligence is a vision on the future of consumer electronics, telecommunications and computing that was originally developed in the late 1990s for the time frame 2010–2020.
In an ambient intelligence world, devices work in concert to support people in carrying out their everyday life activities, tasks and rituals in an easy, natural way using information and intelligence that is hidden in the network connecting these devices (see Internet of Things).
As these devices grow smaller, more connected and more integrated into our environment, the technology disappears into our surroundings until only the user interface remains perceivable by users.
gerdavax AT gmail DOT comBluetooth Low Energy
Ambient Intelligence
http://en.wikipedia.org/wiki/Ambient_intelligence
• Sveglia, casa, doccia, colazione, news…
• Abbigliamento, agenda, si parte!
• Cammino, bici, auto, scooter, treno, metro, aereo…
• Home office, HQ office, client office, mobile office…
• Centro commerciale, parcheggio, ospedale, aeroporto, stazione, parco, cinema, palestra, piscina, scuola, mare, montagna, ristorante…
gerdavax AT gmail DOT comBluetooth Low Energy
Everyday Life Activity
•
gerdavax AT gmail DOT comBluetooth Low Energy
Everyday Life Activity
Beacon
Info ambiente
Controllo accessi
Safety & SecurityTracker
Prossimità
Energia
ClimaPressione arteriosa
Glucometro
Metro
Pesapersone
Ciclo del sonno
Termometro corporeo
Ciclometro
Localizzazione
Cardio
frequenzimetro
• 1994: nasce come “cable replacement” per connessioni seriali • 1999: rilasciato Bluetooth 1.0, piuttosto instabile… • 2003: rilasciato Bluetooth 1.2, stabile e realmente funzionale • 2004: arriva Bluetooth 2.0 con EDR: aumenta la velocità • 2006: Nokia annuncia Wibree per comunicazione a basso consumo • 2007: è l’anno di Bluetooth 2.1+EDR. Migliora la sicurezza • 2009: arriva Bluetooth 3.0 • 2010: è annunciato Bluetooth 4.0 con estensioni Low Energy • 2011: Mac mini, MacBook Air e iPhone 4s sono i primi dispositivi
equipaggiati con Bluetooth 4.0 • 2013: 2.5 miliardi di device Bluetooth-enabled nel mondo
gerdavax AT gmail DOT comBluetooth Low Energy
Breve storia di Bluetooth
• Consumo energetico non trascurabile • Massimo 7 slave per ogni master • Fragile in contesti di elevato affollamento • Tempi di discovery troppo lunghi (sino a 20s) • Advertising “su richiesta” • Meccanismi di pairing lenti • Scarsa granularità dei profili • Stack piuttosto ricco (e, dunque, complesso) • Regole visibilità e discovery limitate • Stream-oriented (RFCOMM o Audio)
gerdavax AT gmail DOT comBluetooth Low Energy
Alcuni dei limiti di Bluetooth
• Architettura non simmetrica (central vs peripheral) • Sbilanciare i ruoli consente di avere periferiche semplici, poco costose
e con bassissimo consumo di energia (< 15mA picco) • Compiti complessi lasciati all’host • Non è previsto il cambio di ruolo (vedi HSP)
• Pacchetti piccoli ( < 50 byte) • Richiedono poca energia per essere trasmessi • MCU più semplice, meno memoria, meno possibilità di errore
• Trasmissione a bassa velocità e bassa potenza • Advertising spontaneo e dinamico
• 3 canali di advertising anziché 32FH • 40 canali complessivi
gerdavax AT gmail DOT comBluetooth Low Energy
La contro-evoluzione: 4.0 non è 3.0++!
gerdavax AT gmail DOT comBluetooth Low Energy
Contro-evoluzione: velocitàVe
loci
tà (M
bps)
0
15
30
45
60
v1.1 v2.0 v3.0 v4.0
0,3
54
31
• Magico • apparati piccolissimi • autonomia incredibile • discovery in pochi millisecondi
• Prossimo • funziona a brevissima distanza • l’info sulla potenza (normalizzata 1m) consente localizzazione indoor
• Fisico • È possibile… infilarlo ovunque! • Le prime applicazioni sono davvero legate alle attività quotidiane
gerdavax AT gmail DOT comBluetooth Low Energy
Magico, Prossimo, Fisico
• Advertiser • Invia segnali periodici, non è dotato di ricevitore
• Scanner • Attende segnali provenienti da un broadcaster; non trasmette
• Peripheral • Ricetrasmittente, lavora principalmente in modalità server
• Central • Tipicamente un computer o un device mobile, lavora principalmente
in modalità client verso uno o più peripheral
(su Bluetooth Classic solo Master e Slave, con assegnazione dinamica)
gerdavax AT gmail DOT comBluetooth Low Energy
Ruoli
gerdavax AT gmail DOT comBluetooth Low Energy
Versioni e “branding” dei dispositivi
• Bluetooth Classic • Compatibile esclusivamente con Bluetooth 1.1, 2.0 e 3.0 • Sono tutti i dispositivi pre-Bluetooth 4.0 e quelli attuali non dotati di
funzionalità LE
• Bluetooth Smart • Compatibile esclusivamente Bluetooth 4.0 low energy (single mode) • Funzionante nella sola modalità peripheral
• Bluetooth Smart Ready • Compatibile con l’intera specifica Bluetooth 4.0 (dual mode) • Dialoga con dispositivi Classic e con dispositivi LE
gerdavax AT gmail DOT comBluetooth Low Energy
Versioni e “branding” dei dispositivi
gerdavax AT gmail DOT comBluetooth Low Energy
Stack Bluetooth Classic
BR/EDR PHY
LINK MANAGER
HCI
L2CAP
RFCOMM
SPP/DUN/OPP
SDP
gerdavax AT gmail DOT comBluetooth Low Energy
Stack Bluetooth Smart
LE PHY
LINK LAYER
HCI
SMP
GAP GATT
ATT
L2CAP
gerdavax AT gmail DOT comBluetooth Low Energy
Stack Bluetooth Smart Ready
LE PHY
LINK LAYER
SMP
GAP GATT
ATT
BR/EDR PHY
LINK MANAGER
HCI
L2CAP
RFCOMM
SPP/DUN/OPP
SDP
gerdavax AT gmail DOT comBluetooth Low Energy
I profili in Bluetooth Classic
• Servizio • Insieme di caratteristiche aggregate per erogare una funzionalità • Esempio:
• “Health Thermometer Service”
• Caratteristiche • Un singolo valore accessibile in lettura o scrittura • Esempio:
• “Temperature Measurement” (temperatura letta) • “Temperature Type” (tipo di termometro) • “Intermediate Temperature” (letture in-progress) • “Measurement Interval”
gerdavax AT gmail DOT comBluetooth Low Energy
GATT: Generic Attribute Profile (1)
https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.health_thermometer.xml
• Descrittori • Informazioni accessorie su una caratteristica • Esempio:
• “Is write supported” sulla caratteristica che gestisce l’intervallo di campionamento
• Tutto è un UUID • A qualsiasi entità è assegnato un UUID (Universally Unique
IDentifier) • Accanto a BD-Address e Friendly-Name, un dispositivo può usare un
UUID anche in advertising (feature usata dagli iBeacon di Apple)
gerdavax AT gmail DOT comBluetooth Low Energy
GATT: Generic Attribute Profile (2)
gerdavax AT gmail DOT comBluetooth Low Energy
Profili, Servizi, Caratteristiche, Descrittori…
Profile
Service Service Service
Characteristic
Characteristic
Characteristic
Value
Descriptor Descriptor Descriptor
gerdavax AT gmail DOT comBluetooth Low Energy
UUID
Entità UUIDBase Bluetooth 00000000-0000-1000-8000-00805F9B34FB
Generic Access 00001800-0000-1000-8000-00805F9B34FB
GATT 00001801-0000-1000-8000-00805F9B34FB
Immediate Alert 00001802-0000-1000-8000-00805F9B34FB
Health Thermometer 00001809-0000-1000-8000-00805F9B34FB
Heart Rate 0000180D-0000-1000-8000-00805F9B34FB
Battery Service 0000180F-0000-1000-8000-00805F9B34FB
Battery Level 00002A19-0000-1000-8000-00805F9B34FB
Blood Pressure 00001810-0000-1000-8000-00805F9B34FB
Current Time 00001805-0000-1000-8000-00805F9B34FB
TX Power 00001804-0000-1000-8000-00805F9B34FB
• Desktop • Linux • Mac OS X • Windows
• Mobile • Android 4.3 • iOS 5 • Windows Phone 8 • BlackBerry 10
gerdavax AT gmail DOT comBluetooth Low Energy
Sistemi operativi e supporto BLE
• Introdotta in Android 4.3 Jelly Bean
• Consente di gestire GATT client e server, accesso a caratteristiche e descrittori, registrazione per le notifiche
• Non supporta la modalità “peripheral”: attualmente, dunque, non è possibile creare un beacon (come su iPhone)
• Android 4.4 KitKat offre un logger dei pacchetti a livello HCI
gerdavax AT gmail DOT comBluetooth Low Energy
Android API per Bluetooth Low Energy
• Google: Nexus 4, Nexus 5, Nexus 7 “2013” • Samsung: Galaxy S3, S3 mini, Galaxy S4, S4 mini; Note 2, Note 3, Note
8”, Note 10” 2014 Edition • LG: Optimus G, 4X, G2
• Grandi assenti: Galaxy Nexus, Nexus 7, Nexus 10
• Altri dispositivi con Android 4.2.x possono disporre di API proprietarie per l’accesso al modulo BLE
• Importante: non basta un semplice aggiornamento software per avere BLE, occorre un hardware dedicato!
gerdavax AT gmail DOT comBluetooth Low Energy
Device Android con supporto Bluetooth Low Energy
• Il dispatching degli eventi avviene attraverso normali “listener” anziché via Intent e BroadcastReceiver
• I componenti che modellano GATT client, caratteristiche e descrittori non sono Parcelable: l’intera gestione della comunicazione avviene all’interno di un singolo Fragment
• Lettura e scrittura sono asincrone (addio I/O stream)
• La stabilità (pre KitKat) lascia un po’ a desiderare…
gerdavax AT gmail DOT comBluetooth Low Energy
Principali differenze nelle API “Classic” e LE
gerdavax AT gmail DOT comBluetooth Low Energy
API Android
BluetoothManager manager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
adapter = manager.getAdapter();
boolean started = adapter.startLeScan(mLeScanCallback);
BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, int rssi, final byte[] scanRecord) {
! // gestione device…
! }
};
gerdavax AT gmail DOT comBluetooth Low Energy
API Android
BluetoothGatt mGatt = device.connectGatt(getActivity(), true, new BluetoothGattCallback() {
String SERVICE = “0000180F-0000-1000-8000-00805F9B34FB”;
String LEVEL = “00002A19-0000-1000-8000-00805F9B34FB”;
! @Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
if (status == BluetoothGatt.GATT_SUCCESS && newState == BluetoothGatt.STATE_CONNECTED) {
BluetoothGattService service = gatt.getService(UUID.fromString(SERVICE));
BluetoothGattCharacteristic ch = service.getCharacteristics(UUID.fromString(LEVEL));
gatt.readCharacteristic(ch);
}
}
@Override
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic ch, int st) {
final int batteryLevel = ch.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0);
}
});
1
2
3
gerdavax AT gmail DOT comBluetooth Low Energy
Playground: sperimentare con BLE
TI SensorTagWTEK Heartrate Monitor
Kontakt Beacon
Tracer Keyfob
Pebble Keyfob
estimote
TI CC2450 miniDK geektech Arduino BLE shield
• Linux • Qualsiasi dongle USB Bluetooth 4.0 dual-mode (su ebay a 10 euro!) • bluez 5.1 • Suggerimento: scaricare (e compilare) l’ultima release dal repository
ufficiale, anziché usare la build presente nelle distribuzioni
• Mac OS X • Tutti i (nuovi) Mac usciti sul mercato dal 2012 dovrebbero avere
Bluetooth Low Energy a bordo • LightBlue (utility gratuita su Mac App Store)
gerdavax AT gmail DOT comBluetooth Low Energy
Linux e Mac OS X
gerdavax AT gmail DOT comBluetooth Low Energy
Linux: discovery dei dispositivi
output di:
hcitool -i hci0 lescan
gerdavax AT gmail DOT comBluetooth Low Energy
Linux: lista servizi e lettura caratteristicheSi seleziona il device con cui
comunicare in modalità interattiva
Lista servizi primari
lettura della caratteristica “battery level” esposta dal
“battery service”La batteria è carica (0x64 = 100%)
gerdavax AT gmail DOT comBluetooth Low Energy
Mac OS X: LightBlue per far tutto!La batteria è carica (0x64 = 100%)
gerdavax AT gmail DOT comBluetooth Low Energy
BLE Scanner su Android
• Per usare BLE è sufficiente un aggiornamento software? • No!
• BLE sostituisce Bluetooth Classic? • No!
• BLE rimpiazzerà NFC? • Bella domanda :-) • Hanno casi d’uso piuttosto diversi (malgrado ciò che si dice…)
• BLE sarà lo standard del futuro? • Se lo sapessi, non sarei qui! (comunque, credo di si)
gerdavax AT gmail DOT comBluetooth Low Energy
FAQ su Bluetooth Low Energy vs resto del mondo
• Bluetooth Low Energy sarà la più grande ed invadente evoluzione delle comunicazioni wireless a corto raggio
• Parallelamente alla diffusione di oggetti direttamente connessi alla Rete (IoT), lo smartphone diventerà centro-stella e gateway di una moltitudine di dispositivi invisibili che circondano gli utenti
• Importanti implicazioni per la privacy: chi raccoglie i dati sulla nostra vita li userà per condizionarci?
• “abbiamo rilevato che sei fiacco, perché non prendi un po’ di XYZ?”
gerdavax AT gmail DOT comBluetooth Low Energy
Conclusioni
• Mara Marzocchi, Marco Casario, Gosia Sosnowska e tutto il team di Codemotion per l’enorme lavoro che fanno ogni anno per la community italiana degli sviluppatori software
• Matteo @mbonifazi Bonifazi per avermi prestato il suo Nexus 7 2013 mentre ero in attesa del Nexus 5 e dell’aggiornamento ad Android 4.3 sul Galaxy S3
• Roberto @frabons Fraboni per aver condiviso scenari e curiosità sull’uso degli iBeacon
gerdavax AT gmail DOT comBluetooth Low Energy
Ringraziamenti
• https://developer.bluetooth.org/TechnologyOverview/Pages/BLE.aspx
• http://developer.android.com/guide/topics/connectivity/bluetooth-le.html
• https://developer.apple.com/library/ios/documentation/NetworkingInternetWeb/Conceptual/CoreBluetooth_concepts/AboutCoreBluetooth/Introduction.html
• Bluetooth Low Energy: The Developer’s Handbook (Robin Heydon, Prentice-Hall)
gerdavax AT gmail DOT comBluetooth Low Energy
Riferimenti e risorse
• NFC Tecnologia e applicazioni (2012)
• Android Bluetooth Hacking (2010)
gerdavax AT gmail DOT comBluetooth Low Energy
Approfondimenti dai precedenti Codemotion
!
• mail: gerdavax AT gmail DOT com • blog: http://www.gerdavax.it • twitter: @gerdavax • slideshare: http://www.slideshare.net/gerdavax • Linkedin: http://www.linkedin.com/in/gerdavax
gerdavax AT gmail DOT comBluetooth Low Energy
Contatti