Slide 1

Slide 1 text

What’s all that hype about BLE? Software Engineer relayr Hugo Doménech Juárez @hudomju 1

Slide 2

Slide 2 text

relayr bring things to life 2

Slide 3

Slide 3 text

3

Slide 4

Slide 4 text

4

Slide 5

Slide 5 text

5

Slide 6

Slide 6 text

6

Slide 7

Slide 7 text

7

Slide 8

Slide 8 text

Why are startups shifting to hardware products? - Sensors are cheap! 8

Slide 9

Slide 9 text

Prototyping tools -3d Printer -CNC Machine -Laser Cutter -3d Scanner 9

Slide 10

Slide 10 text

10

Slide 11

Slide 11 text

11

Slide 12

Slide 12 text

12

Slide 13

Slide 13 text

What do all these products have in common? -The way they communicate! 13

Slide 14

Slide 14 text

Bluetooth Low Energy 14

Slide 15

Slide 15 text

Bluetooth Classic 15

Slide 16

Slide 16 text

Relationship between Bluetooth and Android 16

Slide 17

Slide 17 text

Eclair 2.0 (API 5) BlueZ Android 1.x 4.3 (API 18) Bluedroid JellyBean Bluetooth Smart 5.0 (API 20) Lollipop Full BLE Older Broadcom stack - Samsung & HTC < 4.0 Motorola Bluetooth Stack 17

Slide 18

Slide 18 text

Bluetooth Low Energy 18

Slide 19

Slide 19 text

Central Peripheral Observer Broadcaster Android 4.3+ Android 5.0+ 19

Slide 20

Slide 20 text

20

Slide 21

Slide 21 text

BLE Scanning example 21

Slide 22

Slide 22 text

BLE Scanning 22

Slide 23

Slide 23 text

BLE Scanning boolean hasBleFeature() { return getPackageManager().hasSystemFeature(FEATURE_BLUETOOTH_LE)); } 23

Slide 24

Slide 24 text

BLE Scanning mBluetoothAdapter.startLeScan(mLeScanCallback); mBluetoothAdapter.stopLeScan(mLeScanCallback); BluetoothManager manager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE); mBluetoothAdapter = bluetoothManager.getAdapter(); 24

Slide 25

Slide 25 text

BLE Scanning private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() { @Override public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) { // You are not in the UI thread // or in the thread you subscribed for what matters! } }; 25

Slide 26

Slide 26 text

BLE Scanning /** * Decodes the services uuid from the advertisement packet since * {@link android.bluetooth.BluetoothDevice#getUuids()} returns null most of * the times. */ public static List decodeServicesUuid(byte[] data); /** * Decodes the device name from the Complete Local Name or Shortened * Local Name field in the * Advertisement packet. {@link * android.bluetooth.BluetoothDevice#getName()} does it already, * although some phones skip it. i.e. Sony Xperia Z1 (C6903) with Android 4.3 * where getName() always returns null. */ public static String decodeDeviceName(byte[] data); 26

Slide 27

Slide 27 text

BLE Scanning: Lollipop improvements mBluetoothAdapter.startScan(filters, scanSettings, scanCallback) name deviceAddress uuid serviceDataUuid serviceData manufacturerId manufacturerData Low Power Balanced Low Latency 27

Slide 28

Slide 28 text

interface ScanCallback { public void onScanResult(int callbackType, ScanResult result); public void onBatchScanResults(List results); public void onScanFailed(int errorCode) } BLE Scanning: Lollipop improvements 28

Slide 29

Slide 29 text

Advertisement GATT server Hardware feature - chipset needs to support it - nexus 9 vs nexus 5 & 7 BluetoothAdapter.isMultipleAdvertisementSupported() Peripheral mode: Lollipop features 29

Slide 30

Slide 30 text

Let’s connect to other BLE devices! 30

Slide 31

Slide 31 text

The GATT Protocol Peripheral Central Read - Write - Subscribe Respond - Push GATT Peripheral GATT protocol Service Service Characteristic Characteristic Value Indications Notifications Descriptor Descriptor Descriptor 31 Server Client Generic Attribute Profile

Slide 32

Slide 32 text

The GATT Protocol class BluetoothAdapterLeScanCallback { void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) { device.connectGatt(context, false, mBluetoothGattReceiver); } } class BluetoothGattReceiver { @Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {} } 32

Slide 33

Slide 33 text

The BluetoothGatt discoverServices writeCharacteristic readCharacteristic writeDescriptor setCharacteristicNotification readDescriptor readRemoteRssi beginReliableWrite executeReliableWrite abortReliableWrite readMtu 33

Slide 34

Slide 34 text

The BluetoothGattCallback onServicesDiscovered onCharacteristicWrite onCharacteristicRead onDescriptorWrite onCharacteristicChanged onDescriptorRead onRemoteRssiRead onReliableWriteCompleted onMtuChanged 34

Slide 35

Slide 35 text

Why would you write characteristics? A practical example 35

Slide 36

Slide 36 text

36

Slide 37

Slide 37 text

37

Slide 38

Slide 38 text

38

Slide 39

Slide 39 text

Data subscriptions: Indication / Notification gatt.setCharacteristicNotification(characteristic, enable); descriptor.setValue(ENABLE_NOTIFICATION_VALUE); gatt.writeDescriptor(descriptor); gatt.setCharacteristicNotification(characteristic, enable); descriptor.setValue(ENABLE_INDICATION_VALUE); gatt.writeDescriptor(descriptor); gatt.setCharacteristicNotification(characteristic, enable); descriptor.setValue(DISABLE_NOTIFICATION_VALUE); gatt.writeDescriptor(descriptor); @Override public void onCharacteristicChanged( BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { } 39

Slide 40

Slide 40 text

Reliable Write ?= Long Write gatt.beginReliableWrite(); characteristic.setValue(data); gatt.reliableWriteCharacteristic(mBluetoothGatt, characteristic, subscriber); gatt.executeReliableWrite(); 40 18 byte: 16 bytes content + 1 offset + 1 length package Regular Write supports 18 bytes

Slide 41

Slide 41 text

Disconnecting GATT vs Closing GATT gatt.disconnect(); @Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { if (newState == STATE_DISCONNECTED && noLongerInterestedInTheDevice) { gatt.close(); } } gatt.close(); 41

Slide 42

Slide 42 text

Stack metrics: why you should close your GATT - Constants defined in BlueDroid - Max concurrent active notifications - BTA_GATTC_NOTIF_REG_MAX - (4) Android 4.3 - (7) Android 4.4 - (15) Android 5.0+ - Max concurrent GATT connections - BTA_GATTC_CON_MAX - (4) Android 4.3 - (7) Android 4.4+ 42

Slide 43

Slide 43 text

… and one last thing Security 43

Slide 44

Slide 44 text

Security Pairing Bonding Encryption Re-establishment - Just Works - Passkey Entry - Out Of Band 44

Slide 45

Slide 45 text

Security: Instability static class UndocumentedBleStuff { static boolean isUndocumentedErrorStatus(int status) { return status == 133 || status == 137; } static void fixUndocumentedBleStatusProblem(BluetoothGatt gatt, BluetoothGattReceiver receiver) { DeviceCompatibilityUtils.refresh(gatt); gatt.getDevice().connectGatt(RelayrApp.get(), false, receiver); } } GATT_INSUFFICIENT_AUTHENTICATION GATT_INSUFFICIENT_ENCRYPTION 45

Slide 46

Slide 46 text

Security: Instability boolean refresh(BluetoothGatt gatt) { try { Method localMethod = gatt.getClass().getMethod("refresh", new Class[0]); if (localMethod != null) { return (Boolean) localMethod.invoke(gatt); } } catch (Exception localException) { Log.e(TAG, "An exception occurred while performing: refresh”, localException.getCause()); } return false; } 46

Slide 47

Slide 47 text

Security: Instability class DeviceCompatibilityUtils { boolean createBond(BluetoothDevice device) { if (isSdk19()) return doCreateBond(device); return callMethod(device, "createBond"); } @TargetApi(Build.VERSION_CODES.KITKAT) boolean doCreateBond(BluetoothDevice device) { return device.createBond(); } boolean removeBond(BluetoothDevice device) { return callMethod(device, "removeBond"); } } 47

Slide 48

Slide 48 text

So we can develop now for Android Wearables right? 48

Slide 49

Slide 49 text

Bluetooth SIG (Special Interest Group) 49

Slide 50

Slide 50 text

50

Slide 51

Slide 51 text

Questions? Hugo Doménech @hudomju 51

Slide 52

Slide 52 text

52