Fun with Bluetooth

A presentation at GDG DevFest NL in November 2017 in Amsterdam, Netherlands by Niels Leenheer

Slide 1

Slide 1

fun with bluetooth GDG DevFestNL

Slide 2

Slide 2

why?

Slide 3

Slide 3

progressive 
 web apps

Slide 4

Slide 4

pwa’s are great !

Slide 5

Slide 5

but...

Slide 6

Slide 6

but...

Slide 7

Slide 7

Slide 8

Slide 8

Slide 9

Slide 9

ethernet 
 & wifi

Slide 10

Slide 10

wifi

Slide 11

Slide 11

zigbee? 


Slide 12

Slide 12

ethernet

Slide 13

Slide 13

runs a webserver use fetch or 
 xmlhttprequest

Slide 14

Slide 14

[{ "id":"001788fffe100491", "internalipaddress":"192.168.2.23", "macaddress":"00:17:88:10:04:91", "name":"Philips Hue" }, { "id":"001788fffe09a168", "internalipaddress":"192.168.88.252" }, { https://www.meethue.com/api/nupnp 1

Slide 15

Slide 15

<root xmlns="urn:schemas-upnp-org:device-1-0"> <specVersion> <major>1</major> <minor>0</minor> </specVersion> <URLBase>http://192.168.2.23:80/</URLBase> <device> <deviceType>urn:schemas-upnp-org:device:Basic:1</deviceType> <friendlyName>Philips hue (192.168.2.23)</friendlyName> <manufacturer>Royal Philips Electronics</manufacturer> <manufacturerURL>http://www.philips.com</manufacturerURL> http://192.168.2.23/description.xml 2

Slide 16

Slide 16

http://192.168.2.23/ api 
 {"devicetype": "hue_pwa#pixel_xl" } 3 [{"success":{"username": "........" }}] User presses physical button on the hub click save to local storage

Slide 17

Slide 17

4 { "1": { "state": { "on": true, "bri": 144, "hue": 13088, "sat": 212, "xy": [0.5128,0.4147], "ct": 467, "alert": "none", "effect": "none", http://192.168.2.23/api/........ /lights

Slide 18

Slide 18

http://192.168.2.23/api/......../lights/1/state 
 { "hue": 50000, "on": true, "bri": 200 } 5 [ {"success":{"/lights/1/state/bri":200}}, {"success":{"/lights/1/state/on":true}}, {"success":{"/lights/1/state/hue":50000}} ]

Slide 19

Slide 19

clickclickreloadclick

Slide 20

Slide 20

this does 
 not work !

Slide 21

Slide 21

https only http only no certificate 
 for you

Slide 22

Slide 22

= network use remote api’s ≠ access to local devices

Slide 23

Slide 23

hue remote api Also allow control from 
 outside the local network

Slide 24

Slide 24

We are currently preparing for release of this remote API which will initially be available to a limited number of partners. “ — Philips

Slide 25

Slide 25

We are currently preparing for release of this remote API which will initially be available to a limited number of partners. “ — Philips two years ago very not yet

Slide 26

Slide 26

fun with bluetooth problems local networks

Slide 27

Slide 27

fun with bluetooth

Slide 28

Slide 28

bluetooth

Slide 29

Slide 29

bluetooth sucks

Slide 30

Slide 30

classic bluetoot h the reason everybody 
 hates bluetooth bluetoot h low energy vs. control drones and other cool shit

Slide 31

Slide 31

bluetoot h low energy also known as BLE Bluetooth LE Bluetooth Smart Bluetooth 4

Slide 32

Slide 32

playbulb sphere playbulb

Slide 33

Slide 33

spherio bb-8

Slide 34

Slide 34

parrot mini drone

Slide 35

Slide 35

activity tracker

Slide 36

Slide 36

the boring theoretical stuff

Slide 37

Slide 37

central peripheral

Slide 38

Slide 38

central

Slide 39

Slide 39

generic attribute profile

Slide 40

Slide 40

generic attribute profile ?

Slide 41

Slide 41

generic attribute profile gatt, because gap was already taken

Slide 42

Slide 42

client server

central

peripheral

Slide 43

Slide 43

§ i device information light multiple services per device

Slide 44

Slide 44

i device information battery flight control

Slide 45

Slide 45

i device information battery steering control

Slide 46

Slide 46

i device information battery heart rate

Slide 47

Slide 47

heart rate

i

device information

battery

Slide 48

Slide 48

i device information battery heart rate

Slide 49

Slide 49

i device information manufacturer
model number serial number hardware revision firmware revision software revision ... multiple characteristics 
 per service

Slide 50

Slide 50

server service characteristic value array of objects object property value

Slide 51

Slide 51

services and characteristics are identified by uuid’s 16 bit or 128 bit

Slide 52

Slide 52

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

Slide 53

Slide 53

0x180 F 0000 180F -0000-1000-8000-00805F9B34FB 16 bit 128 bit battery

Slide 54

Slide 54

not recommended any UUID outside of the range 
 xxxxxxxx -0000-1000-8000-00805F9B34FB 16 bit 128 bit light steering control flight control still, everybody does this

Slide 55

Slide 55

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

Slide 56

Slide 56

i 0x180A 0x2A29
0x2A24 0x2A25 0x2A27 0x2A26 0x2A28 ... bad for readability, 
 good for saving bandwidth

Slide 57

Slide 57

read write write without response notify each characteristic supports
one or more of these

Slide 58

Slide 58

every value is an array of bytes no fancy datatypes, just bytes

Slide 59

Slide 59

pfew...

Slide 60

Slide 60

Slide 61

Slide 61

fun

with

bluetooth boring facts 
 about

Slide 62

Slide 62

fun with bluetooth

Slide 63

Slide 63

 web


 bluetooth

api

still not the fun part :-(

Slide 64

Slide 64

connecting to a device

Slide 65

Slide 65

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 ]) ); }) 1 we tell the browser what 
 kind of device we want

Slide 66

Slide 66

the user selects
the actual device

Slide 67

Slide 67

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 ]) ); }) 2 connect to the server

Slide 68

Slide 68

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 ]) ); }) 3 get the service

Slide 69

Slide 69

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 ]) ); }) 4 get the characteristic

Slide 70

Slide 70

writing data

Slide 71

Slide 71

navigator.bluetooth.requestDevice({ ... }) .then(device => device.gatt.connect()) .then(server => server.getPrimaryService(0xff0f)) .then(service => service.getCharacteristic(0xfffc)) .then(c => { return c.writeValue( new Uint8Array([ 0x00, r, g, b ]) ); }) write some bytes

Slide 72

Slide 72

reading data

Slide 73

Slide 73

navigator.bluetooth.requestDevice({ ... })

.then(device => device.gatt.connect()) .then(server => server.getPrimaryService(0xff0f)) .then(service => service.getCharacteristic(0xfffc)) .then(c => c .readValue()) .then(value => { let r = value.getUint8(1);
let g = value.getUint8(2); let b = value.getUint8(3); }) read some bytes

Slide 74

Slide 74

get notified of changes

Slide 75

Slide 75

navigator.bluetooth.requestDevice({ ... })

.then(device => device.gatt.connect()) .then(server => server.getPrimaryService(0xff0f)) .then(service => service.getCharacteristic(0xfffc))

.then(c => { c.addEventListener('characteristicvaluechanged', e => { let r = e.target.value.getUint8(1);
let g = e.target.value.getUint8(2); let b = e.target.value.getUint8(3); }); c.startNotifications(); }) add event listener don't forget to start listening

Slide 76

Slide 76

things you need to know: • the webbluetooth api • promises • typed arrays duh!

Slide 77

Slide 77

browser support Chrome Opera Samsung 
 (behind flag) Servo 
 (soon)

Slide 78

Slide 78

browser support Chrome Opera Samsung 
 (behind flag) kinda works Servo 
 (soon) WebBLE 
 for iOS

Slide 79

Slide 79

and... npm install node-web-bluetooth

Slide 80

Slide 80

and... the puck.js

Slide 81

Slide 81

custom
characteristics . wtf!

Slide 82

Slide 82

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) 


} } writing to and reading 
 from the same characteristic

Slide 83

Slide 83

writing a value: function(r, g, b) { return new Uint8Array([

    0x01, g, 0x01, 0x00, 0x01, 


 b, 0x01, r, 0x01, 0x00

]); } reading the current 
 color is not possible

Slide 84

Slide 84

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; } reading the current 
 color is not possible

Slide 85

Slide 85

writing a value: 
 function(r, g, b, position ) { let buffer = new Uint8Array([

    0x07, 0x02, position + 1, r, g, b  

]);

return buffer; 

}

Slide 86

Slide 86

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

Slide 87

Slide 87

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 => { 
 value === 0x01 || value === 0x02 || value == 0x03 


Slide 88

Slide 88

        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 89

Slide 89

adafruit 
 bluetooth 
 sniffer

Slide 90

Slide 90

decompiling


 the apk don't tell anyone!

Slide 91

Slide 91

demofinally the fun part

Slide 92

Slide 92

warning experimental technology 
 setting low expectations

Slide 93

Slide 93

warning wifi interference 
 lowering them even further

Slide 94

Slide 94

#devfestnl

Slide 95

Slide 95

Slide 96

Slide 96

https://bluetooth.rocks/lightbulb 
 https://github.com/NielsLeenheer/BluetoothBulb change the colour 
 of a lightbulb

Slide 97

Slide 97

https://bluetooth.rocks/pixel draw pixel art on 
 a led matrix display

Slide 98

Slide 98

https://bluetooth.rocks/racer

https://github.com/NielsLeenheer/BluetoothRacer control a lego racer 
 using a gamepad use css animations to 
 define a path

Slide 99

Slide 99

https://bluetooth.rocks/drone 
 https://github.com/poshaughnessy/web-bluetooth-parrot-drone control a drone 
 from your browser

Slide 100

Slide 100

https://bluetooth.rocks/ printer print on a receipt printer

Slide 101

Slide 101

https://bluetooth.rocks/pulse 
 https://github.com/NielsLeenheer/Bluetooth Pulsefind out your 
 current heartbeat

Slide 102

Slide 102

fun with bluetooth !

Slide 103

Slide 103

questions? @html5test