Share
Embed
Transcript
46 slides
-
Bluetooth 2.0+ Classic Original iPhone (2007) RelaFvely high bandwidth RelaFvely
high power consumpFon ⚠ Require MFi cer9fica9on 3 -
Bluetooth 4.0+ Low energy iPhone 4s (2011) Handle a small
amount of data Low power consumpFon Available with Core Bluetooth 4 “Always off” -
Generic Access Profile Define how devices can be discovered and
connected together. 6 -
Devices can act as Central or Peripheral. iDevices will most
of Fme act as Centrals. 7 -
Hello, I’m a super cool device, can you see me?
I want to connect! Adver9ses PERIPHERAL 8 -
Hello, I’m a super cool device, can you see me?
I want to connect! Adver9ses PERIPHERAL Listens Is anyone around? CENTRAL 8 -
Ini9alize 9 import CoreBluetooth class BluetoothDiscoveryManager: NSObject, CBCentralManagerDelegate { init(peripheralPairingDelegate:
PeripheralPairingDelegation) { centralManager = CBCentralManager(delegate: self, queue: nil) } } -
Scan // MARK: - CBCentralManagerDelegate func centralManager(_ central: CBCentralManager, didDiscover
peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) centralManager.scanForPeripherals(withServices: nil, options: nil) centralManager.stopScan() 1 2 3 10 -
Connect centralManager.connect(peripheral, options: nil) // MARK: - CBCentralManagerDelegate func centralManager(_
central: CBCentralManager, didConnect peripheral: CBPeripheral) 1 2 11 -
Generic ATTribute profile Define how devices can exchange data once
they are connected together. 12 -
Adver9se SERVICE 128f47c1-1fee-4c42-9d93-edd0b88f3291 CHARACTERISTIC fd31fe71-b944-4248-aa04-aef43fc69add CHARACTERISTIC fd31fe72-b944-4248-aa04-aef43fc69add CHARACTERISTIC fd31fe73-b944-4248-aa04-aef43fc69add SERVICE
128f47c2-1fee-4c42-9d93-edd0b88f3291 CHARACTERISTIC fd31fe74-b944-4248-aa04-aef43fc69add CHARACTERISTIC fd31fe75-b944-4248-aa04-aef43fc69add Characteris7cs and services are idenFfied with UUIDs. 14 GATT SUMMARY -
CharacterisFcs are a]ributes which can be read and wriBen to.
15 GATT SUMMARY -
CharacterisFcs are a]ributes which can be read and wriBen to.
AddiFonally, peripherals can also no7fy the central when an a]ribute changes. 15 GATT SUMMARY -
Data is exchanged through frames which must be smaller or
equal to 20 bytes. GATT SUMMARY 16 -
Some characterisFcs are predefined in the BLE Standard. Data is
exchanged through frames which must be smaller or equal to 20 bytes. GATT SUMMARY 16 -
00002A08-0000-1000-8000-00805F9B34FB Date Time (0x2A08) org.bluetooth.characterisFc.date_Fme 128bits UUID equivalent: Byte 0
1 2 3 4 5 6 Descrip9on Year Month Day Hours Minutes Seconds Minimum 1582 1 1 0 0 0 Maximum 9999 12 31 23 59 59 GATT SUMMARY 17 -
ProTip™ Everyone is usually expecFng liBle-endian. Be nice, use li]le-endian
for words longer than 8 bits! ⚠ UInt16 here GATT SUMMARY 18 Byte 0 1 2 3 4 5 6 Descrip9on Year Month Day Hours Minutes Seconds Minimum 1582 1 1 0 0 0 Maximum 9999 12 31 23 59 59 -
ProTip™ CBUUID automagically converts 16-bits/32-bits UUIDs to 128-bits UUIDs. let
uuid32 = CBUUID(string: "2a08") let uuid128 = CBUUID(string: "00002a08-0000-1000-8000-00805f9b34fb") uuid32 == uuid128 // true GATT SUMMARY 19 -
Discover Before wriFng and reading characterisFcs, you need to discover
them on the peripheral. (Even if you own the hardware and already know which characterisFcs are available.) GATT DISCOVERY 20 -
Discover Services class BluetoothPeripheralManager: NSObject, CBPeripheralDelegate { } peripheral.delegate =
self // BluetoothPeripheralManager peripheral.discoverServices(nil) // MARK: - CBPeripheralDelegate func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) 1 2 3 GATT DISCOVERY 21 -
Discover Characteris9cs // MARK: - CBPeripheralDelegate func peripheral(_ peripheral: CBPeripheral,
didDiscoverServices error: Error?) { If let error = error { fatalError(error.localizedDescription) } guard let services = peripheral.services else { fatalError("") } for service in services { peripheral.discoverCharacteristics(nil, for: service) } } 1 2 // MARK: - CBPeripheralDelegate peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) GATT DISCOVERY 22 -
When scanning, or discovering, you can always filter specific UUIDs.
let serviceUUID = CBUUID(string: "128f47ce-1fee-4c42-9d93-edd0b88f3291") let characteristicUUID = CBUUID(string: "fd31fe78-b944-4248-aa04-aef43fc69add") central.scanForPeripherals(withServices: [serviceUUID], options: nil) peripheral.discoverServices([serviceUUID]) peripheral.discoverCharacteristics([serviceUUID], for: service) 23 ProTip™ GATT DISCOVERY -
Read/Write Values are read or wriBen using the Data struct.
READ/WRITE CHARACTERISTICS 24 -
peripheral.writeValue(value, for: characteristic, type: .withResponse) peripheral.readValue(for: characteristic) 1 2 25
Read/Write READ/WRITE CHARACTERISTICS func peripheral(_ peripheral: CBPeripheral, didWriteValueFor characteristic: CBCharacteristic, error: Error?) func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) characteristic.value // Data -
26 No9fica9ons NOTIFIED CHARACTERISTICS To limit baBery drain, it’s be]er
to wait for the peripheral to no7fy a change, rather than periodically read a characterisFc. -
27 No9fica9ons NOTIFIED CHARACTERISTICS peripheral.setNotifyValue(true, for: characteristic) // Enable notification
Requested reads and noFfied changes are both reported in peripheral(_:didUpdateValueFor:error:) -
Broadcast 28 No9fica9ons NOTIFIED CHARACTERISTICS Not all characterisFcs support noFficaFons.
It’s up to the hardware to decide which characterisFcs can be no7fied. -
Broadcast 30 Security SECURITY The process of defining a secure,
encrypted communicaFon channel is called bonding. Bonding is also oken referred as pairing. -
Broadcast 31 Security SECURITY There are four types of pairing
methods: Just Works Passkey Numeric comparison Out-of-Band Core Bluetooth supports only two pairing methods. -
Broadcast 31 Security SECURITY There are four types of pairing
methods: Just Works Passkey Numeric comparison Out-of-Band Core Bluetooth supports only two pairing methods. -
Broadcast 32 Security SECURITY iOS handles the pairing automa7cally, when
required. Just Works Passkey -
Background Modes In prac7ce, a lot of the communicaFon will
happen in the background. 34 -
Background Modes 37 Virtually everything can be done in the
background. BACKGROUND MODES Scanning ConnecFng Reading WriFng Discovering -
Background Modes 38 iOS will also wake the process when
a no7fica7on is sent. BACKGROUND MODES -
Background Modes 39 Good Guy iOS makes our lives easier!
BACKGROUND MODES -
iOS supports state preserva7on and restora7on, in case your app
is killed and later relaunched in the background. 40 Apple also provides many useful tools to debug bluetooth exchanges. (But that’s beyond the scope of this talk.)