Slide 1

Slide 1 text

micro:bitͱͷ࿈ܞ࿩

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

! Not too expensive

Slide 9

Slide 9 text

! Not too expensive

Slide 10

Slide 10 text

! Not cheap "

Slide 11

Slide 11 text

We want to try BLE more easily, right?

Slide 12

Slide 12 text

micro:bit/chibi:bit

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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/

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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/

Slide 20

Slide 20 text

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) } }

Slide 21

Slide 21 text

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) }) }

Slide 22

Slide 22 text

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) } }) }}

Slide 23

Slide 23 text

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") } } }

Slide 24

Slide 24 text

DEMO

Slide 25

Slide 25 text

About UUID on microbit

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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()) ]

Slide 29

Slide 29 text

Ҏ্