Slide 1

Slide 1 text

fun with bluetooth

Slide 2

Slide 2 text

bluetooth sucks

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

bluetooth low energy also known as BLE Bluetooth LE Bluetooth Smart Bluetooth 4

Slide 5

Slide 5 text

playbulb sphere playbulb

Slide 6

Slide 6 text

spherio bb-8

Slide 7

Slide 7 text

parrot mini drone

Slide 8

Slide 8 text

activity tracker

Slide 9

Slide 9 text

the boring theoretical stuff

Slide 10

Slide 10 text

central peripheral

Slide 11

Slide 11 text

central

Slide 12

Slide 12 text

generic attribute profile gatt, because gap was already taken

Slide 13

Slide 13 text

central peripheral client server

Slide 14

Slide 14 text

§ i device information light multiple services per device

Slide 15

Slide 15 text

i device information battery flight control

Slide 16

Slide 16 text

i device information battery steering control

Slide 17

Slide 17 text

i device information battery heart rate

Slide 18

Slide 18 text

heart rate i device information battery

Slide 19

Slide 19 text

i device information battery heart rate

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

server service characteristic value array of objects object property value

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

pfew...

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

fun with bluetooth boring facts
 about

Slide 33

Slide 33 text

fun with bluetooth

Slide 34

Slide 34 text

web
 bluetooth api still not the fun part :-(

Slide 35

Slide 35 text

connecting to a device

Slide 36

Slide 36 text

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 37

Slide 37 text

the user selects the actual device

Slide 38

Slide 38 text

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 39

Slide 39 text

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 40

Slide 40 text

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 41

Slide 41 text

writing data

Slide 42

Slide 42 text

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 43

Slide 43 text

reading data

Slide 44

Slide 44 text

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 45

Slide 45 text

get notified of changes

Slide 46

Slide 46 text

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 47

Slide 47 text

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

Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

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

Slide 50

Slide 50 text

and... npm install node-web-bluetooth

Slide 51

Slide 51 text

and... the puck.js

Slide 52

Slide 52 text

command-
 line tools Oh, come on! (linux)

Slide 53

Slide 53 text

hcitool lescan
 LE Scan ... 82:4C:4B:17:AC:E6 PLAYBULB sphere 11:75:58:1B:52:85 TimeBox-mini-light D9:97:A2:35:42:2C BB-422C D5:72:4A:3F:C2:1F Puck.js c21f scan for 
 ble devices

Slide 54

Slide 54 text

gatttool --device=82:4C:4B:17:AC:E6 --characteristics
 
 handle = 0x0002, char properties = 0x20, 
 char value handle = 0x0003, 
 uuid = 00002a05-0000-1000-8000-00805f9b34fb
 handle = 0x0028, char properties = 0x06, 
 char value handle = 0x0029, 
 uuid = 0000fffc-0000-1000-8000-00805f9b34fb handle = 0x004a, char properties = 0x02, 
 char value handle = 0x004b, 
 uuid = 00002a50-0000-1000-8000-00805f9b34fb get a list 
 of all 
 characteristics

Slide 55

Slide 55 text

gatttool --device=82:4C:4B:17:AC:E6 
 --char-read --handle=0x0029
 
 Characteristic value/descriptor: 00 00 00 ff reading the value 
 of a characteristic

Slide 56

Slide 56 text

gatttool --device=82:4C:4B:17:AC:E6 
 --char-write --handle=0x0029 --value=0000ff00
 writing the value 
 of a characteristic

Slide 57

Slide 57 text

custom characteristics. wtf!

Slide 58

Slide 58 text

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 59

Slide 59 text

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 60

Slide 60 text

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 61

Slide 61 text

reading a value: add event listener write a specific value get the event with the color 1 2 3 service 0xffe0
 characteristic 0xffe4 service 0xffe5
 characteristic 0xffe9

Slide 62

Slide 62 text

adafruit
 bluetooth 
 sniffer

Slide 63

Slide 63 text

No content

Slide 64

Slide 64 text

No content

Slide 65

Slide 65 text

demo finally the fun part

Slide 66

Slide 66 text

warning experimental technology
 setting low expectations

Slide 67

Slide 67 text

warning wifi interference
 lowering them even further

Slide 68

Slide 68 text

No content

Slide 69

Slide 69 text

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

Slide 70

Slide 70 text

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

Slide 71

Slide 71 text

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

Slide 72

Slide 72 text

https:/ /bluetooth.rocks/pixel pixel matrix display

Slide 73

Slide 73 text

https:/ /bluetooth.rocks/pulse
 https:/ /github.com/NielsLeenheer/BluetoothPulse find out your 
 current heartbeat

Slide 74

Slide 74 text

fun with bluetooth !

Slide 75

Slide 75 text

questions? @html5test