Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Bluetooth LE - #inspect 2013 - Brussels, Belgiu...

Bluetooth LE - #inspect 2013 - Brussels, Belgium - 28 March 2013

This was from my talk at the RubyMotion #inspect 2013 conference on using Bluetooth LE from RubyMotion. It was a wonderful conference organized by HipByte and there was amazing beer!

Rich Kilmer

March 28, 2013
Tweet

Other Decks in Technology

Transcript

  1. Bluetooth History 98 99 00 01 02 03 04 05

    06 07 08 09 10 11 12 13 SIG Form ed Bluetooth 1.0 Headset, U SB Laptop/M ouse G PS, Cam era Bluetooth 1.2 Bluetooth 2.0 4k SIG M em bers 1B Devices TV/Radio 2B Devices Bluetooth 3.0 Bluetooth 4.0 M acM ini/A ir iPhone 5/iPad M ini 18k SIG M em bers 1mb/s 3mb/s 54mb/s ~260kb/s Our focus
  2. Broadcaster Ad Ad Ad Ad Ad Scanning Observer (iPhone) (Sensor)

    advertising interval Advertising 3 advertisements per second powered off: 99.9999%
  3. #wait for CBCentralManager to be powered on and ready def

    centralManagerDidUpdateState(manager) if manager.state == CBCentralManagerStatePoweredOn # do something now! end end
  4. #scan for peripherals options = { CBCentralManagerScanOptionAllowDuplicatesKey => true }

    services = nil # services = [ServiceUUID1, ServiceUUID2] @manager.scanForPeripheralsWithServices services, options:options
  5. #peripheral discovery delegate method def centralManager(manager, didDiscoverPeripheral: peripheral, # CBPeripheral

    advertisementData: data, # Hash RSSI: rssi #Signal strength...distance ) #do something with the discovered peripheral #NOTE: peripheral name/uuid may or may not be set... end
  6. #connect to peripheral manager.stopScan @peripheral = peripheral #prevent GC! manager.connectPeripheral(peripheral,

    options:{ CBConnectPeripheralOptionNotifyOnConnectionKey: true, CBConnectPeripheralOptionNotifyOnDisconnectionKey: true, CBConnectPeripheralOptionNotifyOnNotificationKey: false })
  7. #connected peripheral def centralManager(central_manager, didConnectPeripheral:peripheral) #peripheral.name and peripheral.UUID ARE set

    end def centralManager(central_manager, didFailToConnectPeripheral:peripheral, error:error) #could not connect to peripheral (toggle BT in Sim?) end
  8. #discovered services now discover characteristics def peripheral(peripheral, didDiscoverServices:error) return if

    error || !peripheral.services peripheral.services.each do |service| # if service.UUID == CBUUID.UUIDWithString("XXXX") # do something specific # end peripheral.discoverCharacteristics nil, forService:service end end
  9. #discovered characteristics, now read them def peripheral(peripheral, didDiscoverCharacteristicsForService:service, error:error) return

    if error || !service.characteristics service.characteristics.each do |char| if char.UUID == CBUUID.UUIDWithString("XXXX") peripheral.readValueForCharacteristic char end if char.UUID == CBUUID.UUIDWithString("YYYY") peripheral.setNotifyValue(true, forCharacteristic:char) end end end
  10. #retrieved values def def peripheral(peripheral, didUpdateValueForCharacteristic:char, error:error) return if error

    if char.UUID == CBUUID.UUIDWithString("XXXX") #do something char.value.bytes... end end
  11. #write values value = Pointer.new(:uchar) value[0] = 32 data =

    NSData.dataWithBytes(value[0], length:8) peripheral.writeValue data, forCharacteristic:char, type:CBCharacteristicWriteWithResponse