81
Getting Physical with Web Bluetooth in the browser Dan Jenkins @dan_jenkins

Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

Embed Size (px)

Citation preview

Page 1: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

Gett ing Physical with Web Bluetooth

in the browserDan Jenkins

@dan_jenkins

Page 2: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

Dan Jenkins@dan_jenkins

nimblea.pe

[email protected]

@nimbleapeltd

Page 3: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

Wouldn't i t be cool to interact

with real l i fe using JavaScr ipt In the Browser?

Page 4: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

Imagine walk ing up to something...

Page 5: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

And being able to use i t . With the Web.

Page 6: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

Drones, BB8, Fi tb i ts, Adult toys... Bluetooth Low

Energy

Page 7: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

Let 's take a step back

Page 8: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

What is this magic?

Page 9: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

Physical Web

Page 10: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

Web Bluetooth

Page 11: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

Gett ing Physical

Physical

Page 12: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

Ever wished you could "click" on things in real life and find

out more about them?

Page 13: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

Been somewhere

that had these?

Page 14: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

They're everywhere!

Page 15: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

But there are many other

places where we want to

interact with something around us

Page 16: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

The Physical Web

Page 17: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

Page 18: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

Page 19: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

How do I use this magic?

Page 20: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

Seeing Physical Web Not i f icat ions

Page 21: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

Android or iOS

Page 22: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

Android iOS• Location & Bluetooth turned on

• Android "Nearby" enabled (default)

https://android.googleblog.com/2016/06/introducing-nearby-new-way-to-discover.html

• Bluetooth turned on

• Chrome Notifications enabled

(Allow the Chrome widget in Notification Center)

https://docs.pushmote.com/docs/how-to-enable-chromes-physical-web-extension-on-the-ios

Page 23: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

Android caveatThey've just rolled back to Chrome doing the Physical

Web notifications in latest Chrome for Android

https://goo.gl/MrAXUw

https://goo.gl/PMwBY2

Page 24: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

Android caveatBut that'll only be for 6 or so weeks...

Page 25: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

Page 26: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

Transmitt ing Physical

Web URLs

Page 27: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

Beacons

Page 28: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

Apps

Page 29: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

Anything that can emit using Bluetooth Low Energy

Page 30: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

What makes up the Physical Web?

Page 31: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

Eddystone• Open Message Format • Eddystone URLs have 18 bytes worth of data that

can be broadcast • https://developers.google.com/beacons/eddystone

Page 32: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

Chrome• Chrome on iOS and Nearby on Android scans for BLE beacons with

Eddystone URLs

• Chrome only supports HTTPS URLs

• Chrome then sends those URLs up to Google's Proxy service

(does some extra magic)

• The Proxy service then sends those URLs back to Chrome

• Chrome/Nearby shows a notification

All of this doesn't work unless you have data & location turned on

Page 33: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

Page 34: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

And they're being used today

Page 35: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkinshttp://www.proxama.com/news/proxama-partners-with-google-to-deliver-worlds-first-physical-web-experience-for-consumers/

Page 36: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkinshttp://www.proxama.com/news/proxama-partners-with-google-to-deliver-worlds-first-physical-web-experience-for-consumers/

Page 37: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

And there are beacons here today

Page 38: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

And you don't have to use Chrome to get

themhttps://www.phy.net/physical-web-browsers/

Page 39: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

That 's great for l ink ing the

Physical Wor ld to your Phone

Page 40: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

BUT, There's more...

Page 41: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

Web Bluetooth

Page 42: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

Your browser can connect to

Bluetooth Low Energy (BLE) devices directly

Page 43: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

Using JavaScript, we can now connect to

physical devices using BLE and control them

Page 44: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

Page 45: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

BLE (not just toys)

45

Page 46: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

BLE (not just toys)

46

Page 47: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

BLE (okay... a fair few toys)

47

Page 48: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

Stepping back, what is BLE?• Low Bandwidth

• Bluetooth v4 - Bluetooth Low Energy (0.3 Mbps) • Bluetooth v3 (54 Mbps)

• A set of Standard Services • But you can also build your own services

Page 49: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

BLE GATT ServerGatt Server

Custom ServiceBattery Service

Battery Level Characteristic

Custom Characteristic #1

Custom Characteristic #1

Page 50: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

Let's take a look at the Web Bluetooth API

Page 51: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

var options = { filters: [{ services: ['heart_rate'] }] };

navigator.bluetooth.requestDevice(options) .then(device => device.gatt.connect()) .then(server => server.getPrimaryService('heart_rate')) .then(service => service.getCharacteristic('heart_rate_measurement')) .then(characteristic => { characteristic.addEventListener('characteristicvaluechanged', beep); return characteristic.startNotifications(); }) .catch(error => { console.log(error); });

function beep(event) { console.log(event.target.value); }

Page 52: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

var options = { filters: [{ services: ['heart_rate'] }] };

navigator.bluetooth.requestDevice(options) .then(device => device.gatt.connect()) .then(server => server.getPrimaryService('heart_rate')) .then(service => service.getCharacteristic('heart_rate_measurement')) .then(characteristic => { characteristic.addEventListener('characteristicvaluechanged', beep); return characteristic.startNotifications(); }) .catch(error => { console.log(error); });

function beep(event) { console.log(event.target.value); }

Page 53: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

var options = { filters: [{ services: ['heart_rate'] }] };

navigator.bluetooth.requestDevice(options) .then(device => device.gatt.connect()) .then(server => server.getPrimaryService('heart_rate')) .then(service => service.getCharacteristic('heart_rate_measurement')) .then(characteristic => { characteristic.addEventListener('characteristicvaluechanged', beep); return characteristic.startNotifications(); }) .catch(error => { console.log(error); });

function beep(event) { console.log(event.target.value); }

Page 54: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

var options = { filters: [{ services: ['heart_rate'] }] };

navigator.bluetooth.requestDevice(options) .then(device => device.gatt.connect()) .then(server => server.getPrimaryService('heart_rate')) .then(service => service.getCharacteristic('heart_rate_measurement')) .then(characteristic => { characteristic.addEventListener('characteristicvaluechanged', beep); return characteristic.startNotifications(); }) .catch(error => { console.log(error); });

function beep(event) { console.log(event.target.value); }

Page 55: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

var options = { filters: [{ services: ['heart_rate'] }] };

navigator.bluetooth.requestDevice(options) .then(device => device.gatt.connect()) .then(server => server.getPrimaryService('heart_rate')) .then(service => service.getCharacteristic('heart_rate_measurement')) .then(characteristic => { characteristic.addEventListener('characteristicvaluechanged', beep); return characteristic.startNotifications(); }) .catch(error => { console.log(error); });

function beep(event) { console.log(event.target.value); }

Page 56: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

var options = { filters: [{ services: ['heart_rate'] }] };

navigator.bluetooth.requestDevice(options) .then(device => device.gatt.connect()) .then(server => server.getPrimaryService('heart_rate')) .then(service => service.getCharacteristic('heart_rate_measurement')) .then(characteristic => { characteristic.addEventListener('characteristicvaluechanged', beep); return characteristic.startNotifications(); }) .catch(error => { console.log(error); });

function beep(event) { console.log(event.target.value); }

Page 57: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

var options = { filters: [{ services: ['heart_rate'] }] };

navigator.bluetooth.requestDevice(options) .then(device => device.gatt.connect()) .then(server => server.getPrimaryService('heart_rate')) .then(service => service.getCharacteristic('heart_rate_measurement')) .then(characteristic => { characteristic.addEventListener('characteristicvaluechanged', boop); return characteristic.startNotifications(); }) .catch(error => { console.log(error); });

function boop(event) { console.log(event.target.value); }

Page 58: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

var options = { filters: [{ services: ['heart_rate'] }] };

navigator.bluetooth.requestDevice(options) .then(device => device.gatt.connect()) .then(server => server.getPrimaryService('heart_rate')) .then(service => service.getCharacteristic('heart_rate_measurement')) .then(characteristic => { characteristic.addEventListener('characteristicvaluechanged', beep); return characteristic.startNotifications(); }) .catch(error => { console.log(error); });

function beep(event) { console.log(event.target.value); }

Page 59: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

var options = { filters: [{ services: ['heart_rate'] }] };

navigator.bluetooth.requestDevice(options) .then(device => device.gatt.connect()) .then(server => server.getPrimaryService('heart_rate')) .then(service => service.getCharacteristic('heart_rate_measurement')) .then(characteristic => { characteristic.addEventListener('characteristicvaluechanged', beep); return characteristic.startNotifications(); }) .catch(error => { console.log(error); });

function beep(event) { console.log(event.target.value); }

Page 60: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

But what if the device was broadcasting a URL

where it can be controlled from?

Page 61: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

Page 62: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

Today, there's no way to bypass that device

selection

Which kinda sucks

Page 63: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

But that's changing soon toovar referringDevice = navigator.bluetooth.referringDevice;

if (referringDevice) { referringDevice.gatt.connect() .then(server => { ... }) .catch(error => { console.log(error); }); }

Page 64: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

But that's changing soon toovar referringDevice = navigator.bluetooth.referringDevice;

if (referringDevice) { referringDevice.gatt.connect() .then(server => { ... }) .catch(error => { console.log(error); }); }

Page 65: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

Where can I play with Web Bluetooth today?

In short.... Chrome/Chromium on...

Android M & N ChromeOS Linux

Android L with Chromium

OSX (Chrome 53)

Windows (a little)

Page 66: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

But for more info...

https://goo.gl/rDu2UT

Page 67: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

And you have to enable it... chrome://flags

Page 68: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

But there is a Google Chrome Origin Trial ongoing

https://goo.gl/hzczDR

Page 69: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

And the Chrome team has announced their intent to ship in 56

https://goo.gl/KMmjda

Page 70: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

Oh and only on Localhost & HTTPS...

There are quite a few hoops!

Page 71: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

Loads of demos out there

https://webbluetoothcg.github.io/demos/

Page 72: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

And you can make your own peripherals

BBC micro:bit

Arduino

Tessel

Raspberry Pi 3

Page 73: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

Developing for Web Bluetooth has become

easier, for sure; eh.

Page 74: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

Unless you're developing on

Windows... but that's changing too

Page 75: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

Hardware with BLE is cheap

Page 76: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

Your dev environment just needs to have BLE

(Bluetooth 4)

Page 77: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

One last demo

Page 78: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

Build th ings with the Web

Page 79: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

There are less and less

reasons to build native apps

Page 80: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

goo.gl/7uAtkY

3 great Google I/O videos about the Physical Web

and Web Bluetooth

Page 81: Getting Physical with Web Bluetooth in the Browser Full Stack Toronto

@dan_jenkins

Thanks!@dan_jenkins