Getting Started with iBeacons (Designers of Things 2014)

Preview:

DESCRIPTION

iBeacon is a system for device-to-device communication based on Bluetooth Low Energy. Applications running on phones, computers, or other types of device can detect and respond to the presence of nearby iBeacons. This technology opens the doors for new and creative interactions between networked objects like wearables and the surrounding environment. In this session we'll explore the potential of iBeacons by building an iBeacon detection device from scratch and using it to interact with a set of iBeacon nodes from Estimote. See code examples and demo application on GitHub here: https://github.com/dluxemburg/ibeacon-detector

Citation preview

Getting  Started  with  iBeacons

Get  started  by  building.

�What  they  are  �How  they  talk  �How  to  work  with  them  �What  this  means  for   designing  things

Getting  Started  with  iBeacons

� Transmitters  that  notify  devices  of  their  presence  � They  can  notify  about  other  things  too  

� Enabling  technology:  Bluetooth  Low  Energy  � iBeacon,  AltBeacon,  maybe  more  to  come  � Lots  of  creative  potential:  

� Context-­‐aware  applications  � High-­‐fidelity  positioning  (“Indoor  GPS”)  � “Smart”  (or  at  least  identifiable)  objects

Getting  Started  with  iBeacons

Getting  Started  with  iBeacons:  Estimotes

!

It’s  not  just  about  the  beacons    !

It’s  about  the  devices  that  sense and    communicate  with  them  too

Going  Beyond  the  Phone

Going  Beyond  the  Phone

� Is  there  a  future  for  single-­‐purpose  devices?  � Of  course,  that’s  why  we’re  here  � At  a  minimum,  there’s  a  “movement”  worth  of  people  who  seem  to  think  there’s  room  to  make  new  things  

� Robots,  drones,  toys,  appliances,  tools,  personal  trackers…  

� It  doesn’t  matter—it’s  still  a  great  way  to  learn  and  explore

Going  Beyond  the  Phone

!!“Node.js®   is   a   platform   built   on   Chrome's  JavaScript   runtime   for   easily   building   fast,          scalable   network   applications.   Node.js   uses   an  event-­‐driven,   non-­‐blocking   I/O  model   that  makes  it   lightweight   and   efficient,   perfect   for   data-­‐intensive   real-­‐time   applications   that   run   across  distributed  devices.”  

—nodejs.org

Application  Platform:  Node.js

1  2  3  4  5

var  noble  =  require('noble')noble.on('discover',  function(per){      console.log(per.advertisement.localName+'  '+per.uuid)  })  noble.startScanning()

1  2  3  4  5  6  7  8  9  

10  11  12  13  14

var  noble  =  require('noble') noble.on('discover',  function(per){      console.log(per.advertisement.localName+'  '+per.uuid)      per.on('connect',  function(){          per.on('servicesDiscover',  function(sers){              sers.forEach(function(ser){                  if(ser.name)  console.log('  '+ser.name)              })          })          per.discoverServices()      })      per.connect()  })  noble.startScanning()

1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20

var  noble  =  require('noble') noble.on('discover',  function(per){      console.log(per.advertisement.localName+'  '+per.uuid)      per.on('connect',  function(){          per.on('servicesDiscover',  function(sers){              sers.forEach(function(ser){                  if(ser.name)  console.log('  '+ser.name)                  ser.on('characteristicsDiscover',  function(chs){                      chs.forEach(function(ch){                          if(ch.name)  console.log('    '+ch.name)                      })                  })                  ser.discoverCharacteristics()              })          })          per.discoverServices()      })      per.connect()  })  noble.startScanning()

1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26

var  noble  =  require('noble') noble.on('discover',  function(per){      console.log(per.advertisement.localName+'  '+per.uuid)      per.on('connect',  function(){          per.on('servicesDiscover',  function(sers){              sers.forEach(function(ser){                  if(ser.name)  console.log('  '+ser.name)                  ser.on('characteristicsDiscover',  function(chs){                      chs.forEach(function(ch){                          if(ch.name)  console.log('    ‘+ch.name)                          ch.on('descriptorsDiscover',  function(descs){                              descs.forEach(function(desc){                                  if(desc.name)  console.log('      '+desc.name)                              })                          })                          ch.discoverDescriptors()                      })                  })                  ser.discoverCharacteristics()              })          })          per.discoverServices()      })      per.connect()  })  noble.startScanning()

Callbacks

� Programming  for  iBeacons,  or  any  similar  technology,  is  inherently  asynchronous  � Programs  respond  to  changes  in  the  environment  

� It’s  the  appropriate  mental  model—set  up  handlers  for  particular  scenarios  

� The  structure  of  programs  have  lessons  to  teach  beyond  their  technical  details

Callbacks

� Peripheral  broadcasts  advertisement  � Central  receives  advertisement,  discovers  services  

� Peripheral  reports  services  � Central  discovers  characteristics  

� Peripheral  reports  characteristics  � Central  reads  value  � Central  discovers  descriptors  

� Peripheral  reports  descriptors    � Central  reads  value(s)

Callbacks  in  Plain  Language

� Finding  the  Raspberry  Pi  (nmap)  � Lists  the  local  IPs  of  available  devices  

� Connecting  to  the  Raspberry  pi  (ssh)  � Creates  a  secure  tunnel  for  controlling  another  computer  

� Turning  on  Bluetooth  (hciconfig)  � Enable  connections  through  the  Bluetooth  USB  dongle  

� Running  our  program  (node)

Talking  to  Our  Thing

$ nmap  -­‐sn  10.0.1.0/24

Starting  Nmap  6.46  (  http://nmap.org  )...     ...at  2014-­‐09-­‐05  17:38  EDT  Nmap  scan  report  for  10.0.1.1  Host  is  up  (0.0026s  latency).  Nmap  scan  report  for  10.0.1.3  Host  is  up  (0.047s  latency).  Nmap  scan  report  for  10.0.1.18  Host  is  up  (0.00011s  latency).  Nmap  scan  report  for  10.0.1.24  Host  is  up  (0.0058s  latency).  Nmap  done:  256  IP  addresses  (4  hosts  up)...       ...scanned  in  3.08  seconds

Finding  the  Raspberry  Pi

$  !!

ssh  pi@10.0.1.24  !pi@10.0.1.24's  password:  !Linux  raspberrypi  3.10.25+  #622...  ...  Last  login:  Mon  Jun  30  00:12:34  2014  from  10.0.1.18

Connecting  to  the  Raspberry  Pi

$  !!

sudo  hciconfig  !hci0:  Type:  BR/EDR    Bus:  USB     BD  Address:  00:1B:DC:06:5D:2A...     DOWN     RX  bytes:1150  acl:0  sco:0  events:58...     TX  bytes:788  acl:0  sco:0  commands:57...

Turning  on  Bluetooth

$  $  !

sudo  hciconfig  hci0  up  sudo  hciconfig  !hci0:  Type:  BR/EDR    Bus:  USB     BD  Address:  00:1B:DC:06:5D:2A...     UP  RUNNING     RX  bytes:1703  acl:0  sco:0  events:86...     TX  bytes:1182  acl:0  sco:0  commands:85...

Turning  on  Bluetooth

$  !!

node  index1.js  !scanning...  estimote  5404e4f46332620002f4  ...

Running  Our  Program

� Adafruit  Blue&White  16x2  LCD+Keypad  Kit  

Having  Our  Thing  Talk  to  Us

!

This  is  still  all  text  

!

Things  are  interesting  because   they  can  talk  in  other  ways  too

� ThingM  blink(1)  mk2  USB  RGB  LED

Having  Our  Thing  “Talk”  to  Us

1  2  3  4  5  6  7  8  9  

10  11

var  detector  =  new  Detector({},{noble:  noble})var  blinker  =  new  Blinker({},{blink1:  new  Blink1()})  var  display  =  new  Display()  !var  app  =  require('./lib/app').create({},{     detector:  detector,     display:  display,     blinker:  blinker  })  !app.run()

Having  Our  Thing  “Talk”  to  Us

Dependency  Injection

1  2  3  4  5  6

exports.Detector  =  function(options,  injected){      this.options  =  options      this.noble  =  injected.noble      this.noble.on('discover',  this.discover.bind(this))      ...  }

Dependency  Injection

1  2  3  4  5  6  7  8  9  

10  11

var  expect  =  require('expect.js'),          sinon  =  require('sinon'),          Detector  =  require('../lib/detector').Detector  !describe('new  Detector()',  function(){      it('calls  noble#on',  function(){          var  noble  =  {on:  sinon.spy()}          var  detector  =  new  Detector({},  {noble:  noble})          expect(noble.on.called).to.be.ok()      })  })

Dependency  Injection

!

This  is  still  an  extension  of  my  computer  

!

Things  are  interesting  because they  can  live  on  their  own  too

� Setup  script  (bash/shell)  � Ensures  environment  is  properly  configured  

� Process  launcher  (init.d)  � Kicks  off  application  and  process  monitoring  

� Process  monitor  (forever)  � Keeps  application  running  if  something  goes  wrong

Having  Our  Thing  Do  Its  Own  Thing

1  2  3  4  5  6  7  8

#!  /bin/sh  !sudo  npm  install  forever  -­‐g  sudo  rm  -­‐rf  /usr/local/ibeacon-­‐detector  sudo  ln  -­‐s  `readlink  -­‐m  .`  /usr/local  sudo  rm  -­‐rf  /etc/init.d/ibeacon-­‐detector  sudo  cp  ibeacon-­‐detector  /etc/init.d/ibeacon-­‐detector  sudo  update-­‐rc.d  ibeacon-­‐detector  defaults

Setup  Script

1  2  3  4  5  6  7  8  9  

10  11  12  13  14

#  /etc/init.d/ibeacon-­‐detector  !###  BEGIN  INIT  INFO  #  Provides:                    ibeacon-­‐detector  #  Required-­‐Start:        $all  #  X-­‐UnitedLinux-­‐Should-­‐Start:  #  Required-­‐Stop:          $all  #  X-­‐UnitedLinux-­‐Should-­‐Stop:  #  User:                            root  #  Default-­‐Start:          2  3  4  5  #  Default-­‐Stop:            0  1  6  #  Short-­‐Description:  iBeacon  Detector  autostart  #  Description:              iBeacon  Detector  autostart...  ###  END  INIT  INFO  

Process  Launcher

15  16  17  18  19  20  21  22  23  24  25  26  27  28

case  "$1"  in      start)          /usr/local/bin/hciconfig  hci0  up          /usr/local/bin/forever  start…          ;;      stop)        /usr/local/bin/forever  stopall          ;;      *)          echo  "Usage:  /ibeacon-­‐detector  {start|stop}"          exit  1          ;;  esac  exit  0

Process  Launcher

$ /usr/local/bin/forever  start  \     -­‐al  /var/log/forever.log  \     -­‐o  /var/log/ibeacon-­‐detector.log  \     -­‐e  /var/log/ibeacon-­‐detector-­‐error.log  \     —sourceDir=/usr/local/ibeacon-­‐detector  index.js

Process  Monitor

System  Utilities

Learn  by  building?

• Things  can  both  produce  and    consume  context  

• Things  can  communicate  in  new  and  different  ways  

• Things  can  exist  autonomously

Observations

• Think  in  callbacks  • Program  with  dependency  injection  (and  mocked  interfaces)  

• Get  familiar  with  relevant  system  utilities  (and  help  others  to  as  well)

Advice

Code:        github.com/dluxemburg/ibeacon-­‐detector  GitHub:  dluxemburg  Twitter:  @dluxemburg

Thanks!