41
Getting Started with iBeacons

Getting Started with iBeacons (Designers of Things 2014)

Embed Size (px)

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

Page 1: Getting Started with iBeacons (Designers of Things 2014)

Getting  Started  with  iBeacons

Page 2: Getting Started with iBeacons (Designers of Things 2014)

Get  started  by  building.

Page 3: Getting Started with iBeacons (Designers of Things 2014)

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

Getting  Started  with  iBeacons

Page 4: Getting Started with iBeacons (Designers of Things 2014)

� 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

Page 5: Getting Started with iBeacons (Designers of Things 2014)

Getting  Started  with  iBeacons:  Estimotes

Page 6: Getting Started with iBeacons (Designers of Things 2014)

!

It’s  not  just  about  the  beacons    !

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

Page 7: Getting Started with iBeacons (Designers of Things 2014)

Going  Beyond  the  Phone

Page 8: Getting Started with iBeacons (Designers of Things 2014)

Going  Beyond  the  Phone

Page 9: Getting Started with iBeacons (Designers of Things 2014)

� 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

Page 10: Getting Started with iBeacons (Designers of Things 2014)

!!“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

Page 11: Getting Started with iBeacons (Designers of Things 2014)

1  2  3  4  5

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

Page 12: Getting Started with iBeacons (Designers of Things 2014)

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()

Page 13: Getting Started with iBeacons (Designers of Things 2014)

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()

Page 14: Getting Started with iBeacons (Designers of Things 2014)

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()

Page 15: Getting Started with iBeacons (Designers of Things 2014)

Callbacks

Page 16: Getting Started with iBeacons (Designers of Things 2014)

� 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

Page 17: Getting Started with iBeacons (Designers of Things 2014)

� 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

Page 18: Getting Started with iBeacons (Designers of Things 2014)

� 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

Page 19: Getting Started with iBeacons (Designers of Things 2014)

$ 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

Page 20: Getting Started with iBeacons (Designers of Things 2014)

$  !!

ssh  [email protected]  [email protected]'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

Page 21: Getting Started with iBeacons (Designers of Things 2014)

$  !!

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

Page 22: Getting Started with iBeacons (Designers of Things 2014)

$  $  !

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

Page 23: Getting Started with iBeacons (Designers of Things 2014)

$  !!

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

Running  Our  Program

Page 24: Getting Started with iBeacons (Designers of Things 2014)

� Adafruit  Blue&White  16x2  LCD+Keypad  Kit  

Having  Our  Thing  Talk  to  Us

Page 25: Getting Started with iBeacons (Designers of Things 2014)

!

This  is  still  all  text  

!

Things  are  interesting  because   they  can  talk  in  other  ways  too

Page 26: Getting Started with iBeacons (Designers of Things 2014)

� ThingM  blink(1)  mk2  USB  RGB  LED

Having  Our  Thing  “Talk”  to  Us

Page 27: Getting Started with iBeacons (Designers of Things 2014)

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

Page 28: Getting Started with iBeacons (Designers of Things 2014)

Dependency  Injection

Page 29: Getting Started with iBeacons (Designers of Things 2014)

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

Page 30: Getting Started with iBeacons (Designers of Things 2014)

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

Page 31: Getting Started with iBeacons (Designers of Things 2014)

!

This  is  still  an  extension  of  my  computer  

!

Things  are  interesting  because they  can  live  on  their  own  too

Page 32: Getting Started with iBeacons (Designers of Things 2014)

� 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

Page 33: Getting Started with iBeacons (Designers of Things 2014)

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

Page 34: Getting Started with iBeacons (Designers of Things 2014)

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

Page 35: Getting Started with iBeacons (Designers of Things 2014)

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

Page 36: Getting Started with iBeacons (Designers of Things 2014)

$ /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

Page 37: Getting Started with iBeacons (Designers of Things 2014)

System  Utilities

Page 38: Getting Started with iBeacons (Designers of Things 2014)

Learn  by  building?

Page 39: Getting Started with iBeacons (Designers of Things 2014)

• Things  can  both  produce  and    consume  context  

• Things  can  communicate  in  new  and  different  ways  

• Things  can  exist  autonomously

Observations

Page 40: Getting Started with iBeacons (Designers of Things 2014)

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

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

Advice

Page 41: Getting Started with iBeacons (Designers of Things 2014)

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

Thanks!