Bluetooth Low Energy
-
Upload
pietro-alberto-rossi -
Category
Mobile
-
view
202 -
download
1
description
Transcript of Bluetooth Low Energy
Bluetooth Low Energy Pietro Alberto Rossi Android Community Outreach
Chi sono? • Sviluppatore Android • Appassionato di nuove
tecnologie • Decima conferenza! • Kebab dipendente • Perennemente a dieta
Android 4.3
In Android 4.3 Jelly Bean, è stato aggiunto il supporto al Bluetooth LE
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.
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.
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.
Specifiche Service
GATT Server
GATT Client Characteristic
Value Descriptor Descriptor
Characteristic
Value Descriptor Descriptor
Heart Rate Measurement Profile Heart Rate Service
GATT Server
GATT Client Heart Rate Measurement
Body Sensor Location
Altre caratteristiche
…
…
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(); !
}
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
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
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();
Esempio pratico…