Fun with Bluetooth

A presentation at Food ‘n Code in September 2019 in Amsterdam, Netherlands by Niels Leenheer

Slide 1

Slide 1

fun with bluetooth

Slide 2

Slide 2

n e b k i o l l ha t s e t 5 l m t @h

Slide 3

Slide 3

Slide 4

Slide 4

Slide 5

Slide 5

fun with bluetooth

Slide 6

Slide 6

waarom?

Slide 7

Slide 7

progressive web apps

Slide 8

Slide 8

pwa’s zijn geweldig !

Slide 9

Slide 9

maar…

Slide 10

Slide 10

bluetooth

Slide 11

Slide 11

bluetooth t g i zu

Slide 12

Slide 12

classic bluetooth vs. m o r a t a a w a h n e h d t e r o o e d bluet n e e r e d ie bedien dro n es en ande bluetooth low energy re coole sh i t

Slide 13

Slide 13

bluetooth low energy ook bekend als B l Bluetooth Smar t uetooth LE B l u e t o o t h 4 BLE

Slide 14

Slide 14

bluetooth low energy ook bekend als B l Bluetooth Smar t uetooth LE B l u e t o o t h 4 and 5 BLE

Slide 15

Slide 15

10 miljoen apparaten met bluetooth worden elke dag gemaakt

Slide 16

Slide 16

n o o f e l e t e l e i b o m

Slide 17

Slide 17

computer

Slide 18

Slide 18

r e t e m e s o c glu iem d n a h s d an

Slide 19

Slide 19

spor thorloge

Slide 20

Slide 20

u b y a pl s lb e r e h p b l u b y a l p

Slide 21

Slide 21

8 b b o i r e h p s

Slide 22

Slide 22

e n o r d i n parrot mi

Slide 23

Slide 23

r e n n i p s t fidge

Slide 24

Slide 24

de saaie theorie

Slide 25

Slide 25

central peripheral

Slide 26

Slide 26

central

Slide 27

Slide 27

generic attribute profile

Slide 28

Slide 28

generic attribute profile ?

Slide 29

Slide 29

generic attribute profile m o , t gat n i l a p a g dat s a w k i u r geb

Slide 30

Slide 30

central peripheral client server

Slide 31

Slide 31

§ i device information light e s e r e d r e e m t a a r a p p a r e p s rvice

Slide 32

Slide 32

i device information battery flight control

Slide 33

Slide 33

i device information battery steering control

Slide 34

Slide 34

i device information battery heart rate

Slide 35

Slide 35

i device information battery heart rate

Slide 36

Slide 36

i device information battery heart rate

Slide 37

Slide 37

i device information manufacturer model number serial number hardware revision firmware revision software revision … s c i t s i r e t c a r a h c e r e e c i d v r r e s r mee e p

Slide 38

Slide 38

server service characteristic value array of objects object property value

Slide 39

Slide 39

services en characteristics hebben altijd een uuid 16 bit of 12 8 bit

Slide 40

Slide 40

i device information 16 bit 128 bit 0x180A 0000180A-0000-1000-8000-00805F9B34FB

Slide 41

Slide 41

battery 16 bit 128 bit 0x180F 0000180F-0000-1000-8000-00805F9B34FB

Slide 42

Slide 42

steering control light maar iede flight c reen doet h 16 bit 128 bit et toch wordt afgeraden elke UUID behalve de reeks xxxxxxxx-0000-1000-8000-00805F9B34FB

Slide 43

Slide 43

i device information manufacturer model number serial number hardware revision firmware revision software revision …

Slide 44

Slide 44

i 0x180A 0x2A29 0x2A24 0x2A25 0x2A27 0x2A26 0x2A28 … s l e c goed vo ht voor de or het b duidelij esparen kheid, van dat a

Slide 45

Slide 45

d n u e t s r e d n o c i t e s i z r e e d t c n a r a a v le ke ch een of meer lezen schrijven schrijven zonder bevestiging notificatie

Slide 46

Slide 46

elke waarde is een array van bytes p y t a t a n e ge d e r e g o h e e l l a es, s e t y b n

Slide 47

Slide 47

pfew…

Slide 48

Slide 48

Slide 49

Slide 49

n e t i e f e i a a s r e v o fun with bluetooth

Slide 50

Slide 50

fun with bluetooth

Slide 51

Slide 51

web bluetooth nog steed s niet het leuke ged eelte :-( api

Slide 52

Slide 52

verbinden met een apparaat

Slide 53

Slide 53

navigator.bluetooth.requestDevice({ filters: [ { namePrefix: ‘PLAYBULB’ } ], optionalServices: [ 0xff0f ] }) .then(device => device.gatt.connect()) .then(server => server.getPrimaryService(0xff0f)) .then(service => service.getCharacteristic(0xfffc)) t a w r e s w o r b e d n e l l e we ver t n e l l i w e w t a a r a p p a voor soor t .then(characteristic => { return characteristic.writeValue( new Uint8Array([ 0x00, r, g, b ); }) ])

Slide 54

Slide 54

r e k i u r b de ge ecteer t sel aat r a p p a t e h

Slide 55

Slide 55

navigator.bluetooth.requestDevice({ filters: [ { namePrefix: ‘PLAYBULB’ } ], optionalServices: [ 0xff0f ] }) .then(device => { …. }) .then(device => device.gatt.connect()) .then(server => server.getPrimaryService(0xff0f)) .then(service => service.getCharacteristic(0xfffc)) z s e s i m o r p 7 1 0 2 o ij z .then(characteristic => { return characteristic.writeValue( new Uint8Array([ 0x00, r, g, b ); ])

Slide 56

Slide 56

let device = await navigator.bluetooth.requestDevice({ filters: [ { namePrefix: ‘PLAYBULB’ } ], optionalServices: [ 0xff0f ] }); .then(device => device.gatt.connect()) .then(server => server.getPrimaryService(0xff0f)) .then(service => service.getCharacteristic(0xfffc)) .then(characteristic => { return characteristic.writeValue( new Uint8Array([ 0x00, r, g, b ); }) ])

Slide 57

Slide 57

let device = await navigator.bluetooth.requestDevice({ filters: [ { namePrefix: ‘PLAYBULB’ } ], optionalServices: [ 0xff0f ] }); r e v r e s e d t e nd m verbi let server = await device.gatt.connect()); let service = await server.getPrimaryService(0xff0f)); let characteristic = await service.getCharacteristic(0xfffc)); e c i v r e s e haal d .then(characteristic => { return characteristic.writeValue( new Uint8Array([ 0x00, r, g, b ); }) ]) c i t s i r e t c a r a h c e d haal

Slide 58

Slide 58

data schrijven

Slide 59

Slide 59

n e e f j i r sch bytes r a pa let let let let device = await navigator.bluetooth.requestDevice({ … }); server = await device.gatt.connect()); service = await server.getPrimaryService(0xff0f)); characteristic = await service.getCharacteristic(0xfffc)); characteristic.writeValue( new Uint8Array([ 0x00, r, g, b ); ])

Slide 60

Slide 60

data lezen

Slide 61

Slide 61

s e le n e e r a pa s e t by let let let let device = await navigator.bluetooth.requestDevice({ … }); server = await device.gatt.connect()); service = await server.getPrimaryService(0xff0f)); characteristic = await service.getCharacteristic(0xfffc)); let value = await characteristic.readValue(); let r = value.getUint8(1); let g = value.getUint8(2); let b = value.getUint8(3);

Slide 62

Slide 62

krijg bericht van wijzigingen

Slide 63

Slide 63

r e n e t s i l t n e v e d d a let let let let device = await navigator.bluetooth.requestDevice({ … }); server = await device.gatt.connect()); service = await server.getPrimaryService(0xff0f)); characteristic = await service.getCharacteristic(0xfffc)); characteristic.addEventListener( ‘characteristicvaluechanged’, e => { let r = e.target.value.getUint8(1); let g = e.target.value.getUint8(2); let b = e.target.value.getUint8(3); } ); vergeet niet characteristic.startNotifications(); om te luister en

Slide 64

Slide 64

dingen die je moet weten: • de webbluetooth api • promises (of async await) • typed arrays d ! h u

Slide 65

Slide 65

browser support Chrome Edge (soon) Samsung Servo (soon)

Slide 66

Slide 66

browser support Chrome Edge (soon) Samsung Servo (soon) iOS

Slide 67

Slide 67

browser support Chrome Edge (soon) Samsung Servo (soon) iOS

Slide 68

Slide 68

browser support a d kin Chrome Edge (soon) Samsung s k r o w Servo WebBLE (soon) for iOS

Slide 69

Slide 69

en…

Slide 70

Slide 70

en… npm install node-web-bluetooth

Slide 71

Slide 71

en… npm install cordova-plugin-webbluetooth

Slide 72

Slide 72

en… espruino m m 8 2 x 8 1

Slide 73

Slide 73

custom characteristics. wtf!

Slide 74

Slide 74

writing a value: function(r, g, b) { return new Uint8Array([ 0x00, r, g, b } ]); reading a value: function(buffer) { return { r: buffer.getUint8(1), g: buffer.getUint8(2), b: buffer.getUint8(3) } } fro g n i d a e r d n a o t g writin c i t s i r e t c a r a h c e m a s e h t m

Slide 75

Slide 75

writing a value: function(r, g, b) { return new Uint8Array([ 0x01, g, 0x01, 0x00, 0x01, b, 0x01, r, 0x01, 0x00 ]); } t n e r r u c e h t g n i read e l b i s s o p t o n s i r colo

Slide 76

Slide 76

writing a value: function(r, g, b) { var buffer = new Uint8Array([ 0xaa, 0x0a, 0xfc, 0x3a, 0x86, 0x01, 0x0d, 0x06, 0x01, r, g, b, 0x00, 0x00, (Math.random() * 1000) & 0xff, 0x55, 0x0d ]); for (var i = 1; i < buffer.length - 2; i++) { buffer[15] += buffer[i]; } } return buffer; t n e r r u c e h t g n i read e l b i s s o p t o n s i r colo

Slide 77

Slide 77

writing a value: function(r, g, b, position) { let buffer = new Uint8Array([ 0x07, 0x02, position + 1, r, g, b ]); } return buffer;

Slide 78

Slide 78

writing a value: function(r, g, b, position) { let buffer = new Uint8Array([ 0x58, r, g, b, 0x01, position ]); …

Slide 79

Slide 79

writing a value: function(r, g, b, position) { let buffer = new Uint8Array([ 0x58, r, g, b, 0x01, position ]); let payload = new Uint8Array(buffer.length + 4); payload[0] = payload.length - 2; payload[1] = payload.length - 2 >>> 8; payload.set(buffer, 2); let checksum = payload.reduce((a, b) => a + b, 0); payload[payload.length - 2] = checksum; payload[payload.length - 1] = checksum >>> 8; let extra = payload.filter(value => {

Slide 80

Slide 80

message[m] = 0x03; message[m + 1] = 0x05; m += 2; } } else if (payload[i] === 0x03) { message[m] = 0x03; message[m + 1] = 0x06; m += 2; } else { message[m] = payload[i]; m++; } message[0] = 0x01; message[message.length - 1] = 0x02; } return message;

Slide 81

Slide 81

adafruit bluetooth sniffer

Slide 82

Slide 82

log alle bluetooth packets op jouw telefoon b d a k i u r r e b v e o g en om ze etten z e t

Slide 83

Slide 83

m o k r a n h e s e k j r i i k w e b k i e u t r a b ge de dat

Slide 84

Slide 84

de apk decompilen nie l k r e tv ! n e p ap

Slide 85

Slide 85

eindelijk demo het leuk e gedeelt e

Slide 86

Slide 86

waarschuwing experimentele technologie n i t h c a w r e v lage n e p p e h c s gen

Slide 87

Slide 87

waarschuwing wifi interferentie ! r e g a l t a w r a a m g o n t a l e d t e z

Slide 88

Slide 88

Slide 89

Slide 89

fun with bluetooth !

Slide 90

Slide 90

vragen? @htm l5test