Bluetooth Low Energy

14
Bluetooth Low Energy Pietro Alberto Rossi Android Community Outreach

description

Little presentation about Bluetooth Low Energy in Android. New version of bluetooth (4.0).

Transcript of Bluetooth Low Energy

Page 1: Bluetooth Low Energy

Bluetooth Low Energy Pietro Alberto Rossi Android Community Outreach

Page 2: Bluetooth Low Energy

Chi sono? •  Sviluppatore Android • Appassionato di nuove

tecnologie • Decima conferenza! • Kebab dipendente •  Perennemente a dieta

Page 3: Bluetooth Low Energy

Android 4.3

In Android 4.3 Jelly Bean, è stato aggiunto il supporto al Bluetooth LE

Page 4: Bluetooth Low Energy

Meno energia…

Bluetooth LE utilizza meno energia rispetto allo standard precedente. Conseguenza naturale è la possibilità di poterlo utilizzare su quei dispositivi a batteria per garantirne una lunga durata.

Page 5: Bluetooth Low Energy

Meno dati…

Il protocollo di comunicazione è stato ottimizzato per inviare e ricevere meno byte possibili. Ideale per dispositivi come sensori, controller remoti, ecc. Naturalmente non è adatto ad audio streaming e simili.

Page 6: Bluetooth Low Energy

Specifiche

Bluetooth LE è basato su delle specifiche chiamate General Attribute Profile (GATT) che descrivono come inviare e ricevere piccoli pezzi di dati chiamati Attributi (ATT). Un’ applicazione Android può essere utilizzata sia da client sia da server. Un server GATT supporta una collezione di servizi che implementano le varie feature del device. Ogni device è univocamente rintracciabile tramite UUID.

Page 7: Bluetooth Low Energy

Specifiche Service

GATT Server

GATT Client Characteristic

Value Descriptor Descriptor

Characteristic

Value Descriptor Descriptor

Page 8: Bluetooth Low Energy

Heart Rate Measurement Profile Heart Rate Service

GATT Server

GATT Client Heart Rate Measurement

Body Sensor Location

Altre caratteristiche

Page 9: Bluetooth Low Energy

Android SDK

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

Android Manifest

Controllo presenza SDK

if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) { ! Toast.makeText(this, R.string.ble_not_supported, " "

" Toast.LENGTH_SHORT).show(); !"finish(); !

}

Page 10: Bluetooth Low Energy

Android SDK Start scanning

mBluetoothAdapter.startLeScan(mLeScanCallback);

@Override!public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) { ! runOnUiThread(new Runnable() { ! @Override! public void run() { ! mLeDeviceListAdapter.addDevice(device); ! mLeDeviceListAdapter.notifyDataSetChanged(); ! } ! }); !} !

Device trovato

Page 11: Bluetooth Low Energy

Android SDK

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

mBluetoothGatt.discoverServices()

@Override!public void onServicesDiscovered(BluetoothGatt gatt, int status) { ! if (status == BluetoothGatt.GATT_SUCCESS) { ! broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED); ! } else { ! Log.w(TAG, "onServicesDiscovered received: " + status); ! } !}

Servizio trovato

Connessione server GATT

Lettura servizio

Page 12: Bluetooth Low Energy

Android SDK Lettura caratteristica @Override!public void onCharacteristicRead(BluetoothGatt gatt, !

" BluetoothGattCharacteristic characteristic, int status) { ! if (status == BluetoothGatt.GATT_SUCCESS) { ! broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic); ! } !}

Disconnessione

mBluetoothGatt.close();

Page 13: Bluetooth Low Energy

Esempio pratico…

Page 14: Bluetooth Low Energy

Conclusioni

Sito web www.sprik.it

Mail [email protected]