microbitとの連携話

 microbitとの連携話

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

Db5cf21a75ebe2484b95ea9dec5f573b?s=128

Shingo Tamaki

January 30, 2017
Tweet

Transcript

  1. micro:bitͱͷ࿈ܞ࿩

  2. Self introduction • Shingo Tamaki • iOS Engeneer! • Hobby"

    • Electronic works⚡ • Muscle training$ • Gadget%
  3. Devices connectable to iOS • Wired! • Lightning connector •

    " MFi • Earphone jack • " iPhone 7 later model does not include a headphone jack.
  4. Devices connectable to iOS • Wireless! • Bluetooth 3.x •

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

    (Bluetooth low energy) • GATT Profile
  6. How to connect BLEh Devices to iOS • Use Core

    Bluetooth Framework
  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
  8. ! Not too expensive

  9. ! Not too expensive

  10. ! Not cheap "

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

  12. micro:bit/chibi:bit

  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
  14. What is micro:bit The BBC micro:bit is a pocket-sized codeable

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

    computer - Software - Web IDE like scratch - OTA
  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/
  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
  18. Relationship between iOS Device and chibibit in Core Bluetooth •

    Central: iOS Device • Peripheral: chibi:bit
  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/
  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) } }
  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) }) }
  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) } }) }}
  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") } } }
  24. DEMO

  25. About UUID on microbit

  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
  27. Microbit.swift Discovering services Before let services = [ CBUUID(string:"E95D9882-251D-470A-A062-FA1922DFA9A8")] After

    let services = [CBUUID(string:MicroBit.Service.button.uuid())]
  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()) ]
  29. Ҏ্