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. Self introduction • Shingo Tamaki • iOS Engeneer! • Hobby"

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

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

    (Bluetooth low energy) • GATT Profile
  4. 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
  5. 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
  6. What is micro:bit The BBC micro:bit is a pocket-sized codeable

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

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

    Central: iOS Device • Peripheral: chibi:bit
  11. 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/
  12. 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) } }
  13. 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) }) }
  14. 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) } }) }}
  15. 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") } } }
  16. 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
  17. 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()) ]