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

microbitとの連携話

Shingo Tamaki
January 30, 2017

 microbitとの連携話

iOSとmicro:bitをCoreBluetoothで連携してみた話。

Shingo Tamaki

January 30, 2017
Tweet

More Decks by Shingo Tamaki

Other Decks in Technology

Transcript

  1. micro:bitͱͷ࿈ܞ࿩

    View Slide

  2. Self introduction
    • Shingo Tamaki
    • iOS Engeneer!
    • Hobby"
    • Electronic works⚡
    • Muscle training$
    • Gadget%

    View Slide

  3. Devices connectable to iOS
    • Wired!
    • Lightning connector
    • " MFi
    • Earphone jack
    • " iPhone 7 later model does not include a
    headphone jack.

    View Slide

  4. Devices connectable to iOS
    • Wireless!
    • Bluetooth 3.x
    • HID(ex: Keyboard)
    • SPP
    • " MFi

    View Slide

  5. Devices connectable to iOS
    • Wireless!
    • " Bluetooth 4.x (Bluetooth low energy)
    • GATT Profile

    View Slide

  6. How to connect BLEh Devices to
    iOS
    • Use Core Bluetooth Framework

    View Slide

  7. Popular BLE devices in Japan
    • Konashi (¥4,298 + α)
    • Raspberry Pi + BT Dongle (about pi/¥6,210~3,780 +
    dongle/¥2,000 + α )
    • Edison (about ¥10,000 + α)
    • Switch Science mbed HRM1017r3 (about ¥4,000ɹ+ α)
    • RedBearLab BLE Nano (about ¥5,000 + α)
    α: PowerUnit/Cable/SD Card/etc

    View Slide

  8. ! Not too expensive

    View Slide

  9. ! Not too expensive

    View Slide

  10. ! Not cheap "

    View Slide

  11. We want to try BLE
    more easily, right?

    View Slide

  12. micro:bit/chibi:bit

    View Slide

  13. What is micro:bit
    The BBC micro:bit is a pocket-sized
    codeable computer
    The BBC MicroBit will be given to 1 million UK children in October,
    and units will also be available for purchase
    through distributors towards the end of the year.
    The goal of the MicroBit is to give a tangible device
    that can be carried and attached to mobile projects,
    or even as a daughter board for a Raspberry Pi or Arduino,
    as well as teaching programming language skills.
    https://www.infoq.com/news/2015/07/
    bbc-microbit

    View Slide

  14. What is micro:bit
    The BBC micro:bit is a pocket-sized codeable computer
    - Hardware
    - Accelerometer/Compass/5x5LED/2 button/GPIO/BLE

    View Slide

  15. What is micro:bit
    The BBC micro:bit is a pocket-sized codeable computer
    - Software
    - Web IDE like scratch
    - OTA

    View Slide

  16. What is chibi:bit
    chibi:bit is a micro:bit compatible machine released from
    Switch-Science.
    ¥3,456 !
    https://www.switch-science.com/catalog/2900/

    View Slide

  17. Development Tool
    • chibi:bit IDE http://chibibit.io/ide/
    • micro:bit Microsoft PXT https://
    pxt.microbit.org/
    • mbed IDE https://
    developer.mbed.org/compiler/
    • and more http://microbit.org/code/
    • python/javascript
    • Microsoft Block Editor/Microsoft
    Touch Develop

    View Slide

  18. Relationship
    between iOS
    Device and chibibit
    in Core Bluetooth
    • Central: iOS Device
    • Peripheral: chibi:bit

    View Slide

  19. Data Structure
    • Button Service:
    "E95D9882-251D-470A-A062-
    FA1922DFA9A8"
    • Characteristic
    • Button 1 State:
    "E95DDA90-251D-470A-A062-
    FA1922DFA9A8"
    • Button 2 State:
    "E95DDA91-251D-470A-A062-
    FA1922DFA9A8"
    https://lancaster-university.github.io/

    View Slide

  20. Discover and
    Connect
    let manager: CBCentralManager = CBCentralManager()
    private func scanStart() {
    manager.scanForPeripherals(withServices: nil, options: nil)
    }
    func centralManager(_ central: CBCentralManager,
    didDiscover peripheral: CBPeripheral,
    advertisementData: [String : Any],
    rssi RSSI: NSNumber) {
    if ϖϦϑΣϥϧΛಛఆ {
    peripherals.append(peripheral)
    peripheral.delegate = self
    manager.connect(peripheral, options: nil)
    }
    }

    View Slide

  21. Explore
    func centralManager(_ central: CBCentralManager,
    didConnect peripheral: CBPeripheral) {
    let services = [
    CBUUID(string:"E95D9882-251D-470A-A062-FA1922DFA9A8")
    ]
    peripheral.discoverServices(services)
    }
    func peripheral(_ peripheral: CBPeripheral,
    didDiscoverServices error: Error?) {
    peripheral.services?.forEach({ (service) in
    let characteristics: [CBUUID] = [
    CBUUID(string: "E95DDA90-251D-470A-A062-FA1922DFA9A8"),
    CBUUID(string: "E95DDA91-251D-470A-A062-FA1922DFA9A8")
    ]
    peripheral.discoverCharacteristics(characteristics, for: service)
    })
    }

    View Slide

  22. Explore
    func peripheral(_ peripheral: CBPeripheral,
    didDiscoverCharacteristicsFor service: CBService, error: Error?) {
    let targetCharacteristics
    = service.characteristics?.filter({ (characteristic) -> Bool in
    if characteristic.uuid.uuidString
    == MicroBit.Characteristic.Button.button1State.uuid() {
    return true
    }
    if characteristic.uuid.uuidString
    == MicroBit.Characteristic.Button.button2State.uuid() {
    return true
    }
    return false
    });
    if let targetCharacteristics = targetCharacteristics {
    targetCharacteristics.forEach({ (characteristic) in
    if characteristic.uuid.uuidString
    == "E95DDA90-251D-470A-A062-FA1922DFA9A8" {
    peripheral.setNotifyValue(true, for: characteristic)
    }
    if characteristic.uuid.uuidString
    == "E95DDA90-251D-470A-A062-FA1922DFA9A8" {
    peripheral.setNotifyValue(true, for: characteristic)
    }
    })
    }}

    View Slide

  23. Interact
    func peripheral(_ peripheral: CBPeripheral,
    didUpdateValueFor characteristic:
    CBCharacteristic, error: Error?) {
    if let value = characteristic.value {
    var buttonState:UInt8 = 0
    value.copyBytes(to: &buttonState, count: value.count)
    var button = ""
    if characteristic.uuid.uuidString
    == "E95DDA90-251D-470A-A062-FA1922DFA9A8" {
    button = "A"
    }
    if characteristic.uuid.uuidString
    == "E95DDA91-251D-470A-A062-FA1922DFA9A8" {
    button = "B"
    }
    switch buttonState {
    case 0:
    debugPrint("Released \(button) Button")
    case 1:
    debugPrint("Pushed \(button) Button")
    case 2:
    debugPrint("Long Press \(button) Button")
    default:
    debugPrint("unknown")
    }
    }
    }

    View Slide

  24. DEMO

    View Slide

  25. About UUID on
    microbit

    View Slide

  26. Microbit.swift
    This structure summarizes the service UUID and micro:bit
    characteristics so that it can be handled easily by BLE.
    https://github.com/tamaki-shingo/Microbit.swift

    View Slide

  27. Microbit.swift
    Discovering services
    Before
    let services = [ CBUUID(string:"E95D9882-251D-470A-A062-FA1922DFA9A8")]
    After
    let services = [CBUUID(string:MicroBit.Service.button.uuid())]

    View Slide

  28. Microbit.swift
    Discovering characteristics
    Before
    let characteristics: [CBUUID] = [
    CBUUID(string: "E95DDA90-251D-470A-A062-FA1922DFA9A8"),
    CBUUID(string: "E95DDA91-251D-470A-A062-FA1922DFA9A8")
    ]
    After
    let characteristics: [CBUUID] = [
    CBUUID(string: MicroBit.Characteristic.Button.button1State.uuid()),
    CBUUID(string: MicroBit.Characteristic.Button.button2State.uuid())
    ]

    View Slide

  29. Ҏ্

    View Slide