Slide 1

Slide 1 text

Hiromu Tsuruta / Shinichiro Oba Getting started with controlling LEGO using Swift try! Swift Tokyo 2024 Workshop

Slide 2

Slide 2 text

                              ・Rakuten Group, Inc                   ・Rakuma        ・iOS Developer    ・Developer Relations                              ・Twitter:@hcrane14 Tech Lead

Slide 3

Slide 3 text

Workshop Contents • About Bluetooth • About Bluetooth LE • About GATT • About Core Bluetooth • Demonstrate littleBits STAR WARS R2-D2

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

What do you associate with Bluetooth ? ϒϧʔτΡʔε͔ΒԿΛ࿈૝͢Δʁ

Slide 6

Slide 6 text

What is Bluetooth ?

Slide 7

Slide 7 text

About Bluetooth • Wireless communication technology ɹແઢ௨৴ٕज़ͷͻͱͭ Quoteɿhttps://k-tai.sharp.co.jp/appli/useful/10sen/01bluetooth.html • Communication standard designed for short distances ɹ୹ڑ཭޲͖ͷ௨৴ن֨

Slide 8

Slide 8 text

In 1997, Jim Kardach of Intel proposed the name “Bluetooth”. 1997೥ɺΠϯςϧͷδϜɾέϧμοΫ͕
 ʮϒϧʔτΡʔεʯͱ͍͏໊લΛఏҊͨ͠ɻ Quoteɿhttps://commons.wikimedia.org/wiki/File:Jim_Kardach_Mr_Bluetooh.png

Slide 9

Slide 9 text

Why ?

Slide 10

Slide 10 text

Nordic Countries ๺Ԥ

Slide 11

Slide 11 text

Nordic Countries Quoteɿhttps://nordiclifescience.org/different-covid-19-strategies-in-the-nordic-countries/

Slide 12

Slide 12 text

Harald Gormsen Blåtand Quoteɿhttps://www.thoughtco.com/harald-bluetooth-profile-1788985 • King of Denmark in the 10th century ɹ̍̌ੈلͷσϯϚʔΫԦ • Unified Norway and Denmark ɹϊϧ΢ΣʔͱσϯϚʔΫΛ౷Ұͨ͠ ϋϥϧυɾΰϧϜηϯɾϒϩλϯ

Slide 13

Slide 13 text

Quoteɿhttps://www.thoughtco.com/harald-bluetooth-profile-1788985 Harald had a blue tooth Harald Gormsen Blåtand ϋϥϧυɾΰϧϜηϯɾϒϩλϯ

Slide 14

Slide 14 text

Harald Gormsen Blåtand Quoteɿhttps://www.thoughtco.com/harald-bluetooth-profile-1788985 His nickname is ’Blåtan’ χοΫωʔϜ͸੨ࣃԦͩͬͨ ϋϥϧυɾΰϧϜηϯɾϒϩλϯ → ’Blåtan’ = blue tooth ɹϒϩλϯ = ੨͍ࣃ

Slide 15

Slide 15 text

Quoteɿhttps://commons.wikimedia.org/wiki/File:Jim_Kardach_Mr_Bluetooh.png He chose this name because he hoped that Bluetooth would unify communication protocols like King Harald united countries. ൴͕͜ͷ໊લΛબΜͩͷ͸ɺϋϥϧυԦ͕ࠃΛ౷ Ұͨ͠Α͏ʹɺϒϧʔτΡʔε͕௨৴ϓϩτίϧ Λ౷Ұ͢Δ͜ͱΛ๬Μ͔ͩΒͰ͋Δɻ

Slide 16

Slide 16 text

Quoteɿhttps://twitter.com/Bluetooth_Japan/status/1138135930974326784 The logo is a combination of the runic letters "H" and "B" from Harald Blåtand initials. ϩΰ͸ϋϥϧυɾϒϩλϯͷΠχγϟϧͰɺϧʔϯจࣈͷʮHʯͱʮBʯΛ૊Έ߹Θͤͨ΋ͷɻ Bluetooth logo

Slide 17

Slide 17 text

Bluetooth History

Slide 18

Slide 18 text

Bluetooth History Quoteɿhttps://www.tjsys.co.jp/focuson/clme-bluetooth/index_j.htm Bluetooth 4.0

Slide 19

Slide 19 text

Bluetooth Low Energy (Bluetooth LE)

Slide 20

Slide 20 text

Bluetooth Classic and Bluetooth Low Energy Quoteɿhttps://www.global-tag.com/bluetooth-low-energy-ble-overview/ Bluetooth Classic Bluetooth Low Energy Versions 4.x and later are generally referred to as Bluetooth LE, but also include Bluetooth Classic. 4.xҎ߱ͷόʔδϣϯ͸ҰൠʹBluetooth LEͱݺ͹ΕΔ͕ɺBluetooth Classic΋ؚ·Ε͍ͯΔɻ

Slide 21

Slide 21 text

Bluetooth LE is fantastic

Slide 22

Slide 22 text

What is the difference ? Bluetooth Classic Bluetooth Low Energy

Slide 23

Slide 23 text

ᶃ Connectivity ௨৴

Slide 24

Slide 24 text

Quoteɿhttps://www.musen-connect.co.jp/blog/course/product/analyze-bluetooth-pairing-problems-with-protocol-analyzer/ Bluetooth 1.x, 2.x 3.x Bluetooth 4.x, 5.x Active Scan Passive Scan ΞΫςΟϒεΩϟϯ ύογϒεΩϟϯ Communication flow of Bluetooth

Slide 25

Slide 25 text

Ϛελʔ εϨʔϒ Bluetooth Classic (1.x, 2.x, 3.x) ᶃ Inquiry Slave (Child) Master (Parent) ᶄ Response Note: The flow is a bit more complex, but has been omitted. ࣮ࡍͷϑϩʔ͸΋͏গ͠ෳࡶ͕ͩলུ͍ͯ͠Δɻ Active Scan ᶅ Connection

Slide 26

Slide 26 text

Central (Parent) Peripheral (Child) ηϯτϥϧ ϖϦϑΣϥϧ Bluetooth LE (4.x, 5.x) Note: The flow is a bit more complex, but has been omitted. ࣮ࡍͷϑϩʔ͸΋͏গ͠ෳࡶ͕ͩলུ͍ͯ͠Δɻ Passive Scan ᶄ Receive ᶅ Connection ᶃ Broadcast

Slide 27

Slide 27 text

Swift can handle Bluetooth easily than before They look the same in the diagram, but... Swift ͰҎલΑΓ΋؆୯ʹϒϧʔτΡʔεΛѻ͑ΔΑ͏ʹͳͬͨ ਤͰ͸ಉ͡Α͏ʹݟ͑·͕͢...

Slide 28

Slide 28 text

ᶄ Simultaneous Connections ಉ࣌઀ଓ

Slide 29

Slide 29 text

Bluetooth Classic (1.0~) Pairing Connection One device can only establish one pairing. 1ͭͷσόΠεཱ͕֬Ͱ͖ΔϖΞϦϯά͸1͚ͭͩɻ

Slide 30

Slide 30 text

Bluetooth Classic (2.1~) Pairing Connection Pairing One device can have multiple pairings (limited), but only one connection at a time. 1୆ͷσόΠεͰෳ਺ͷϖΞϦϯάʢ੍ݶ͋Γʣ͕Մೳ͕ͩɺҰ౓ʹ઀ଓͰ͖Δͷ͸1ͭͷΈɻ

Slide 31

Slide 31 text

Bluetooth LE (4.x~) Pairing Connection Pairing Connection One device can have multiple pairings, and multiple connection at a time. 1୆ͷσόΠεͰෳ਺ͷϖΞϦϯά͕ՄೳͰɺҰ౓ʹෳ਺ͷ઀ଓ͕Ͱ͖Δɻ

Slide 32

Slide 32 text

ᶅ Communication Profile ௨৴ϓϩϑΝΠϧ

Slide 33

Slide 33 text

Connection Communication Profile ϓϩϑΝΠϧɿBluetoothσόΠεؒͷ௨৴΍σʔλަ׵Λఆٛ͢Δ࢓༷ Profile: Specifications for communication and data exchange between Bluetooth devices

Slide 34

Slide 34 text

Before Bluetooth LE emerged… Hands-Free Profile Advanced Audio Distribution Profile Human Interface Device Profile Serial Port Profile Bluetooth LE ͕ग़Δલ͸… There were several profiles, each requiring customized implementation ͍͔ͭ͘ͷϓϩϑΝΠϧ͕͋Γɺن֨͝ͱʹݸผʹ࣮૷͕ඞཁͩͬͨɻ

Slide 35

Slide 35 text

After Bluetooth LE emerged… Generic Attribute Profile ʢGATTʣ Note: Not all Bluetooth LE uses GATT. ͢΂ͯͷBluetooth LE͕GATTΛ࢖༻͍ͯ͠ΔΘ͚Ͱ͸ͳ͍ɻ Bluetooth LE ͕ग़͔ͯΒ͸… GATT has unified profiles and made implementation easier. GATTʹن͕֨౷Ұ͞Εͯɺ؆୯ʹͳͬͨɻ

Slide 36

Slide 36 text

What is GATT ?

Slide 37

Slide 37 text

About GATT GATT Service Characteristic Declaration Value Descriptor (Optional) Feature Attribute Bluetooth௨৴ʹඞཁͳ৘ใ Information required for communication between Bluetooth devices ʨ ػೳ ଐੑ

Slide 38

Slide 38 text

Permissions Quoteɿhttps://devzone.nordicsemi.com/f/nordic-q-a/64286/basic-concept-of-sending-and-receiving-data-with-ble Read Write Notify

Slide 39

Slide 39 text

GATT Example Quoteɿhttps://www.microchip.com/en-us/solutions/medical/demonstrations-and-design-files/wearable-heart-rate-monitor Peripheral (Child) Central (Parent) GATT Wearable Heart Rate Monitor

Slide 40

Slide 40 text

GATT Example Quoteɿhttps://developerhelp.microchip.com/xwiki/bin/view/applications/ble/introduction/bluetooth-architecture/bluetooth-host-layer/bluetooth-generic-attribute-profile-gatt/Data-Organization/ ʨ ʨ ʨ

Slide 41

Slide 41 text

littleBits STAR WARS R2-D2 Quoteɿ https://www.littlebits-jp.com/single-post/2018/04/10

Slide 42

Slide 42 text

Quoteɿhttps://www.littlebits-jp.com/single-post/2018/04/10 littleBits STAR WARS R2-D2 littleBits was acquired, and the app is no longer downloadable ϦτϧϏοπ͸ങऩ͞ΕɺΞϓϦ͸μ΢ϯϩʔυͰ͖ͳ͘ͳͬͯ͠·ͬͨ

Slide 43

Slide 43 text

DroidKit / crane-hiromu @ GitHub

Slide 44

Slide 44 text

littleBits STAR WARS R2-D2 - GATT GATT Service FW Version Bitsnap Control Audio Upload ʹ Operation Feature ʹ ʹ Firmware Feature Voice Record Feature ΦϖϨʔγϣϯػೳ ϑΝʔϜ΢ΣΞػೳ ΦʔσΟΦػೳ Demo

Slide 45

Slide 45 text

Core Bluetooth iOS͓ΑͼApple͕ఏڙ͢ΔଞͷΦϖϨʔςΟϯάɾγεςϜʹ͓͚ΔBluetooth௨৴ͷͨΊͷϑϨʔϜϫʔΫ Framework for Bluetooth communication in iOS and other Apple-provided operating systems Peripheral (Child) ϖϦϑΣϥϧ ηϯτϥϧ Central (Parent)

Slide 46

Slide 46 text

CBPeripheralDelegate Core Bluetooth Protocols for handling events related to devices acting as a ‘Peripheral’ ϖϦϑΣϥϧͱͯ͠ಈ࡞͢ΔσόΠεʹؔ࿈͢ΔΠϕϯτ Λॲཧ͢ΔͨΊͷϓϩτίϧ Peripheral (Child) ϖϦϑΣϥϧ ηϯτϥϧ Central (Parent) CBCentralManagerDelegate Protocols for handling events related to devices acting as a ‘Central’ ηϯτϥϧͱͯ͠ಈ࡞͢ΔσόΠεʹؔ࿈͢ΔΠϕϯτ Λॲཧ͢ΔͨΊͷϓϩτίϧ

Slide 47

Slide 47 text

CBCentralManagerDelegate CBPeripheralDelegate Example code Central (Parent) Peripheral (Child) ηϯτϥϧ ϖϦϑΣϥϧ Control peripheral Handle peripheral Handle central Control central

Slide 48

Slide 48 text

Quoteɿhttps://medium.com/macoclock/core-bluetooth-ble-swift-d2e7b84ea98e

Slide 49

Slide 49 text

Peripheral (Child) ϖϦϑΣϥϧ Quoteɿhttps://www.musen-connect.co.jp/blog/course/trial-production/ble-beginner-2/ Step 1 - Receive advertisement packet ηϯτϥϧ Central (Parent) packet packet packet packet packet

Slide 50

Slide 50 text

Quoteɿhttps://www.musen-connect.co.jp/blog/course/trial-production/ble-beginner-2/ Step 1 - Receive advertisement packet packet packet packet packet packet Scan Result

Slide 51

Slide 51 text

Quoteɿhttps://www.musen-connect.co.jp/blog/course/trial-production/ble-beginner-2/ Step 1 - Receive advertisement packet packet packet packet packet packet ηϯτϥϧͷपғʹ͋Δ͢΂ͯͷBluetooth LEσόΠε͕ݕग़͞ΕΔͷͰɺඞཁͳσόΠε͚ͩΛ୳͢ɻ It detects all Bluetooth LE devices around Central, so it only looks for the ones it needs.

Slide 52

Slide 52 text

Step 2 - Establish a connection Peripheral (Child) ϖϦϑΣϥϧ ηϯτϥϧ Central (Parent)

Slide 53

Slide 53 text

Step 2 - Establish a connection Peripheral (Child) ϖϦϑΣϥϧ ηϯτϥϧ Central (Parent) CBPeripheralDelegate

Slide 54

Slide 54 text

Service FW Version Bitsnap Control Audio Upload ʹ Operation Feature ʹ ʹ Firmware Feature Voice Record Feature ΦϖϨʔγϣϯػೳ ϑΝʔϜ΢ΣΞػೳ ΦʔσΟΦػೳ Step 3 - Discover Service

Slide 55

Slide 55 text

Peripheral (Child) ϖϦϑΣϥϧ ηϯτϥϧ Central (Parent) Step 3 - Discover service

Slide 56

Slide 56 text

Peripheral (Child) ϖϦϑΣϥϧ ηϯτϥϧ Central (Parent) Step 3 - Discover service

Slide 57

Slide 57 text

Service FW Version Bitsnap Control Audio Upload ʹ Operation Feature ʹ ʹ Firmware Feature Voice Record Feature ΦϖϨʔγϣϯػೳ ϑΝʔϜ΢ΣΞػೳ ΦʔσΟΦػೳ Step 4 - Discover characteristics

Slide 58

Slide 58 text

Peripheral (Child) ϖϦϑΣϥϧ ηϯτϥϧ Central (Parent) Step 4 - Discover characteristics

Slide 59

Slide 59 text

Peripheral (Child) ϖϦϑΣϥϧ ηϯτϥϧ Central (Parent) Step 4 - Discover characteristics FW Version Bitsnap Control Audio Upload ! ! " Filter UUID

Slide 60

Slide 60 text

Peripheral (Child) ϖϦϑΣϥϧ ηϯτϥϧ Central (Parent) Permission - Write value

Slide 61

Slide 61 text

Example Write Value - Sound [0x1E, 0x01, 0x00, 0xE1, 0xF0] [0x1E, 0x01, 0x01, 0xF1, 0xD1] [0x1E, 0x01, 0x02, 0xC1, 0xB2] Sound Type Sound Command

Slide 62

Slide 62 text

Other examples [0x12, 0x03, 0x00, 0x00, 0xFF, 0xD2, 0x6C] [0x12, 0x03, 0xFF, 0x00, 0x00, 0x03, 0xFF] Color Command Color Type [0x14, 0x02, 0x02, 0xFF, 0x65, 0x9D] [0x14, 0x02, 0x02, 0x99, 0x69, 0xFD] Move Command Move Type Move Speed