77

Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

Embed Size (px)

Citation preview

Page 1: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf
Page 2: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

DEMYSTIFYING ANDROID'S BLUETOOTH LOW ENERGY

Paweł UrbanSenior Software Engineer

Dariusz SewerynSenior Software Engineer

Page 3: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

BLUETOOTH LOW ENERGY

Page 4: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

BLUETOOTH LOW ENERGY

ANDROID’S NATIVE SUPPORT

Page 5: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

BLUETOOTH LOW ENERGY

ANDROID’S NATIVE SUPPORT

YOUR SECRET WEAPON

Page 6: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

WHY SHOULD I CARE ABOUT BLE?

Page 7: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf
Page 8: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

The Internet of Things – infographic The Connectivist based on Cisco data

Page 9: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

2006

Page 10: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

2006 2010

Page 11: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

2006 2010 2013

Page 12: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

A DEVELOPER PERSPECTIVE

Page 13: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

Jelly Bean

Page 14: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

BACKWARD SUPPORT

Page 15: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

BACKWARD SUPPORT

Page 16: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

BACKWARD SUPPORTBETA

Page 17: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

BACKWARD SUPPORTBETA

Page 18: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

LOW ENERGY

Page 19: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

GAP (GENERIC ACCESS PROFILE)

Page 20: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

PERIPHERAL

Page 21: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

PERIPHERAL CENTRAL

Page 22: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

PERIPHERAL CENTRAL

ADVERTISE PRESENCE

Page 23: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

PERIPHERAL CENTRAL

ADVERTISE PRESENCECONNECTION HANDSHAKE

Page 24: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

CLIENT

PERIPHERAL CENTRAL

ADVERTISE PRESENCECONNECTION HANDSHAKE

SERVER

Page 25: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

CLIENT

PERIPHERAL CENTRAL

ADVERTISE PRESENCECONNECTION HANDSHAKE

SERVER

READ/WRITE DATA REQ

RESPOND WITH DATA

Page 26: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

GATT (Generic Attribute Profile)

Page 27: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

Texas Instruments SensorTAG

Page 28: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

Peripheral device

Service A

Characteristic 1 Characteristic 2

DEVICE

Page 29: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

Peripheral deviceDEVICE

SENSOR

Service A

Characteristic 1 Characteristic 2

Page 30: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

Peripheral deviceDEVICE

SENSOR

36.6°C SENSOR VALUE

Service A

Characteristic 1 Characteristic 2

Page 31: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

Peripheral deviceDEVICE

SENSOR

36.6°C SENSOR VALUE

Service A

Characteristic 1 Characteristic 2

SENSOR CONFIGURATION

Page 32: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

Peripheral deviceDEVICE

SENSOR

36.6°C SENSOR VALUE

Service A

Characteristic 1 Characteristic 2

SENSOR CONFIGURATION

READ WRITE NOTIFY

Page 33: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

Peripheral device

Service A

Characteristic 1 Characteristic 2

Service B

Characteristic 3 Characteristic 4

Service C

Characteristic 5 Characteristic 6

DISCOVERY

Page 34: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

UUID

1 TEMPERATURE(„4E99FFF1-4B3E-11E5-9D65-0002A5D5C51B"); 2 HUMIDITY(„4E99FFF2-4B3E-11E5-9D65-0002A5D5C51B"); 3 TIME(„5E99FFC1-4B3E-11E5-9D65-0002A5D5C51B"); 4 DEVICE_NAME(„00002a26-0000-1000-8000-00805f9b34fb”);

Page 35: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

UUID

1 TEMPERATURE(„4E99FFF1-4B3E-11E5-9D65-0002A5D5C51B"); 2 HUMIDITY(„4E99FFF2-4B3E-11E5-9D65-0002A5D5C51B"); 3 TIME(„5E99FFC1-4B3E-11E5-9D65-0002A5D5C51B"); 4 DEVICE_NAME(„00002a26-0000-1000-8000-00805f9b34fb”);

128-bit

Page 36: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

UUID

1 TEMPERATURE(„4E99FFF1-4B3E-11E5-9D65-0002A5D5C51B"); 2 HUMIDITY(„4E99FFF2-4B3E-11E5-9D65-0002A5D5C51B"); 3 TIME(„5E99FFC1-4B3E-11E5-9D65-0002A5D5C51B"); 4 DEVICE_NAME(„00002a26-0000-1000-8000-00805f9b34fb”);

16-bit

Page 37: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

THE SCARY PART

Page 38: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();final boolean startLeScan = bluetoothAdapter.startLeScan(leScanCallback);

BluetoothAdapter.LeScanCallback leScanCallback = new BluetoothAdapter.LeScanCallback() { @Override public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) { // processing results }};

bluetoothAdapter.stopLeScan(leScanCallback);

SCAN

Page 39: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();final boolean startLeScan = bluetoothAdapter.startLeScan(leScanCallback);

BluetoothAdapter.LeScanCallback leScanCallback = new BluetoothAdapter.LeScanCallback() { @Override public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) { // processing results }};

bluetoothAdapter.stopLeScan(leScanCallback);

SCAN

Page 40: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();final boolean startLeScan = bluetoothAdapter.startLeScan(leScanCallback);

BluetoothAdapter.LeScanCallback leScanCallback = new BluetoothAdapter.LeScanCallback() { @Override public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) { // processing results }};

bluetoothAdapter.stopLeScan(leScanCallback);

SCAN

Page 41: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();final boolean startLeScan = bluetoothAdapter.startLeScan(leScanCallback);

BluetoothAdapter.LeScanCallback leScanCallback = new BluetoothAdapter.LeScanCallback() { @Override public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) { // processing results }};

bluetoothAdapter.stopLeScan(leScanCallback);

SCAN

Page 42: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();final boolean startLeScan = bluetoothAdapter.startLeScan(

new UUID[]{ UUID.fromString(…) }, leScanCallback);

BluetoothAdapter.LeScanCallback leScanCallback = new BluetoothAdapter.LeScanCallback() { @Override public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) { // processing results }};

bluetoothAdapter.stopLeScan(leScanCallback);

SCAN

Page 43: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

BluetoothDevice remoteDevice = bluetoothAdapter.getRemoteDevice(getEnteredMacAddress());BluetoothGatt bluetoothGatt = remoteDevice.connectGatt(context, false, callback);BluetoothGattCallback callback = new BluetoothGattCallback() { @Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {

if (status == BluetoothGatt.GATT_SUCCESS) { if (newState == BluetoothProfile.STATE_CONNECTED) { // You are connected! } else if (newState == BluetoothProfile.STATE_DISCONNECTED) { // Disconnected bluetoothGatt.close(); } } else { // Maybe disconnected? onDisconnectRequested(); } }};

CONNECTION

Page 44: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

BluetoothDevice remoteDevice = bluetoothAdapter.getRemoteDevice(getEnteredMacAddress());BluetoothGatt bluetoothGatt = remoteDevice.connectGatt(context, false, callback);BluetoothGattCallback callback = new BluetoothGattCallback() { @Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {

if (status == BluetoothGatt.GATT_SUCCESS) { if (newState == BluetoothProfile.STATE_CONNECTED) { // You are connected! } else if (newState == BluetoothProfile.STATE_DISCONNECTED) { // Disconnected bluetoothGatt.close(); } } else { // Maybe disconnected? onDisconnectRequested(); } }};

CONNECTION

Page 45: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

BluetoothDevice remoteDevice = bluetoothAdapter.getRemoteDevice(getEnteredMacAddress());BluetoothGatt bluetoothGatt = remoteDevice.connectGatt(context, false, callback);BluetoothGattCallback callback = new BluetoothGattCallback() { @Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {

if (status == BluetoothGatt.GATT_SUCCESS) { if (newState == BluetoothProfile.STATE_CONNECTED) { // You are connected! } else if (newState == BluetoothProfile.STATE_DISCONNECTED) { // Disconnected bluetoothGatt.close(); } } else { // Maybe disconnected? onDisconnectRequested(); } }};

CONNECTION

Page 46: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

BluetoothDevice remoteDevice = bluetoothAdapter.getRemoteDevice(getEnteredMacAddress());BluetoothGatt bluetoothGatt = remoteDevice.connectGatt(context, false, callback);BluetoothGattCallback callback = new BluetoothGattCallback() { @Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {

if (status == BluetoothGatt.GATT_SUCCESS) { if (newState == BluetoothProfile.STATE_CONNECTED) { // You are connected! } else if (newState == BluetoothProfile.STATE_DISCONNECTED) { // Disconnected bluetoothGatt.close(); } } else { // Maybe disconnected? onDisconnectRequested(); } }};

CONNECTION

Page 47: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

BluetoothDevice remoteDevice = bluetoothAdapter.getRemoteDevice(getEnteredMacAddress());BluetoothGatt bluetoothGatt = remoteDevice.connectGatt(context, false, callback);BluetoothGattCallback callback = new BluetoothGattCallback() { @Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {

if (status == BluetoothGatt.GATT_SUCCESS) { if (newState == BluetoothProfile.STATE_CONNECTED) { // You are connected! } else if (newState == BluetoothProfile.STATE_DISCONNECTED) { // Disconnected bluetoothGatt.close(); } } else { // Maybe disconnected? onDisconnectRequested(); } }};

CONNECTION

Page 48: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

boolean startedDiscovering = bluetoothGatt.discoverServices();

@Overridepublic void onServicesDiscovered(BluetoothGatt gatt, int status) { if (status == BluetoothGatt.GATT_SUCCESS) { BluetoothGattService service = gatt.getService(serviceUUID); BluetoothGattCharacteristic characteristic = service.getCharacteristic(characteristicUUID); boolean startedReadOperation = gatt.readCharacteristic(characteristic); // ... } else { // ... }}

@Overridepublic void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { if (status == BluetoothGatt.GATT_SUCCESS) { byte[] characteristicValue = characteristic.getValue(); } else { // ... }}

READING DATA

Page 49: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

boolean startedDiscovering = bluetoothGatt.discoverServices();

@Overridepublic void onServicesDiscovered(BluetoothGatt gatt, int status) { if (status == BluetoothGatt.GATT_SUCCESS) { BluetoothGattService service = gatt.getService(serviceUUID); BluetoothGattCharacteristic characteristic = service.getCharacteristic(characteristicUUID); boolean startedReadOperation = gatt.readCharacteristic(characteristic); // ... } else { // ... }}

@Overridepublic void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { if (status == BluetoothGatt.GATT_SUCCESS) { byte[] characteristicValue = characteristic.getValue(); } else { // ... }}

READING DATA

Page 50: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

boolean startedDiscovering = bluetoothGatt.discoverServices();

@Overridepublic void onServicesDiscovered(BluetoothGatt gatt, int status) { if (status == BluetoothGatt.GATT_SUCCESS) { BluetoothGattService service = gatt.getService(serviceUUID); BluetoothGattCharacteristic characteristic = service.getCharacteristic(characteristicUUID); boolean startedReadOperation = gatt.readCharacteristic(characteristic); // ... } else { // ... }}

@Overridepublic void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { if (status == BluetoothGatt.GATT_SUCCESS) { byte[] characteristicValue = characteristic.getValue(); } else { // ... }}

READING DATA

Page 51: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

boolean startedDiscovering = bluetoothGatt.discoverServices();

@Overridepublic void onServicesDiscovered(BluetoothGatt gatt, int status) { if (status == BluetoothGatt.GATT_SUCCESS) { BluetoothGattService service = gatt.getService(serviceUUID); BluetoothGattCharacteristic characteristic = service.getCharacteristic(characteristicUUID); boolean startedReadOperation = gatt.readCharacteristic(characteristic); // ... } else { // ... }}

@Overridepublic void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { if (status == BluetoothGatt.GATT_SUCCESS) { byte[] characteristicValue = characteristic.getValue(); } else { // ... }}

READING DATA

Page 52: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

@Override void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic)@Overridevoid onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status)@Overridevoid onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status)@Overridevoid onConnectionStateChange(BluetoothGatt gatt, int status, int newState)@Overridevoid onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status)@Overridevoid onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status)@Overridevoid onServicesDiscovered(BluetoothGatt gatt, int status)@Overridevoid onMtuChanged(BluetoothGatt gatt, int mtu, int status)@Overridevoid onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status)@Overridevoid onReliableWriteCompleted(BluetoothGatt gatt, int status)

Page 53: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

DOES IT HAVE TO BE HARD?

Page 54: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

RxAndroidBLENo! Just use

Page 55: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

RXJAVA

Page 56: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

OBSERVABLE

RXJAVA

Page 57: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

Device Scanning

OBSERVABLE

Page 58: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

Device Scanning

Scanned device

Page 59: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

Device Scanning

Scanned device

Page 60: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

Device Scanning

Scanned device

Page 61: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

Device Scanning

Page 62: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

Device Scanning

Establish Connection

Page 63: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

Device Scanning

Connecting to deviceEstablish Connection

Page 64: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

Device Scanning

Connecting to deviceEstablish Connection

Page 65: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

Read Characteristic

Device Scanning

Connecting to deviceEstablish Connection

Page 66: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

Device Scanning

Connecting to device

Reading Characteristic ValueRead Characteristic

Establish Connection

Page 67: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

Device Scanning

Connecting to device

Reading Characteristic ValueRead Characteristic

Establish Connection

Page 68: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

Declaration of what we want to achieve

Device Scanning

Connecting to device

Reading Characteristic ValueRead Characteristic

Establish Connection

Page 69: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

Device Scanning

Connecting to device

Reading Characteristic ValueRead Characteristic

Establish Connection

Page 70: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

Device Scanning

Connecting to device

Some action

Do something

Establish Connection

Page 71: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

Device Scanning

Connecting to device

Some action

Do something

Establish Connection

Page 72: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

Device Scanning

Connecting to device

Some action

Do something

Establish Connection

Page 73: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

THE DEMO

Page 74: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

SAY THAT AGAIN?

Page 75: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

Q&As?

Page 76: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

LET’S CHAT!

@DSeweryn

+PawełUrban

https://git.io/vw8PM

Page 77: Demystifying Android's Bluetooth Low Energy at MCE^3 Conf

Thank You!@DSeweryn +PawełUrban

https://git.io/vw8PM