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

Getting started with controlling LEGO using Swift

h.crane
March 23, 2024

Getting started with controlling LEGO using Swift

h.crane

March 23, 2024
Tweet

More Decks by h.crane

Other Decks in Technology

Transcript

  1.                <Company>                ・Rakuten Group, Inc          <Application>          ・Rakuma

       <Role>    ・iOS Developer    ・Developer Relations               <Account>                ・Twitter:@hcrane14 Tech Lead
  2. Workshop Contents • About Bluetooth • About Bluetooth LE •

    About GATT • About Core Bluetooth • Demonstrate littleBits STAR WARS R2-D2
  3. In 1997, Jim Kardach of Intel proposed the name “Bluetooth”.

    1997೥ɺΠϯςϧͷδϜɾέϧμοΫ͕
 ʮϒϧʔτΡʔεʯͱ͍͏໊લΛఏҊͨ͠ɻ Quoteɿhttps://commons.wikimedia.org/wiki/File:Jim_Kardach_Mr_Bluetooh.png
  4. Harald Gormsen Blåtand Quoteɿhttps://www.thoughtco.com/harald-bluetooth-profile-1788985 • King of Denmark in the

    10th century ɹ̍̌ੈلͷσϯϚʔΫԦ • Unified Norway and Denmark ɹϊϧ΢ΣʔͱσϯϚʔΫΛ౷Ұͨ͠ ϋϥϧυɾΰϧϜηϯɾϒϩλϯ
  5. 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. ൴͕͜ͷ໊લΛબΜͩͷ͸ɺϋϥϧυԦ͕ࠃΛ౷ Ұͨ͠Α͏ʹɺϒϧʔτΡʔε͕௨৴ϓϩτίϧ Λ౷Ұ͢Δ͜ͱΛ๬Μ͔ͩΒͰ͋Δɻ
  6. 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
  7. 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΋ؚ·Ε͍ͯΔɻ
  8. Ϛελʔ εϨʔϒ 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
  9. 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
  10. Swift can handle Bluetooth easily than before They look the

    same in the diagram, but... Swift ͰҎલΑΓ΋؆୯ʹϒϧʔτΡʔεΛѻ͑ΔΑ͏ʹͳͬͨ ਤͰ͸ಉ͡Α͏ʹݟ͑·͕͢...
  11. Bluetooth Classic (1.0~) Pairing Connection One device can only establish

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

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

    have multiple pairings, and multiple connection at a time. 1୆ͷσόΠεͰෳ਺ͷϖΞϦϯά͕ՄೳͰɺҰ౓ʹෳ਺ͷ઀ଓ͕Ͱ͖Δɻ
  14. 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 ͍͔ͭ͘ͷϓϩϑΝΠϧ͕͋Γɺن֨͝ͱʹݸผʹ࣮૷͕ඞཁͩͬͨɻ
  15. 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ʹن͕֨౷Ұ͞Εͯɺ؆୯ʹͳͬͨɻ
  16. About GATT GATT Service Characteristic Declaration Value Descriptor (Optional) Feature

    Attribute Bluetooth௨৴ʹඞཁͳ৘ใ Information required for communication between Bluetooth devices ʨ ػೳ ଐੑ
  17. 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 ϦτϧϏοπ͸ങऩ͞ΕɺΞϓϦ͸μ΢ϯϩʔυͰ͖ͳ͘ͳͬͯ͠·ͬͨ
  18. littleBits STAR WARS R2-D2 - GATT GATT Service FW Version

    Bitsnap Control Audio Upload ʹ Operation Feature ʹ ʹ Firmware Feature Voice Record Feature ΦϖϨʔγϣϯػೳ ϑΝʔϜ΢ΣΞػೳ ΦʔσΟΦػೳ Demo
  19. 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’ ηϯτϥϧͱͯ͠ಈ࡞͢ΔσόΠεʹؔ࿈͢ΔΠϕϯτ Λॲཧ͢ΔͨΊͷϓϩτίϧ
  20. 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.
  21. Service FW Version Bitsnap Control Audio Upload ʹ Operation Feature

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

    ʹ ʹ Firmware Feature Voice Record Feature ΦϖϨʔγϣϯػೳ ϑΝʔϜ΢ΣΞػೳ ΦʔσΟΦػೳ Step 4 - Discover characteristics
  23. Peripheral (Child) ϖϦϑΣϥϧ ηϯτϥϧ Central (Parent) Step 4 - Discover

    characteristics FW Version Bitsnap Control Audio Upload ! ! " Filter UUID
  24. Example Write Value - Sound [0x1E, 0x01, 0x00, 0xE1, 0xF0]

    [0x1E, 0x01, 0x01, 0xF1, 0xD1] [0x1E, 0x01, 0x02, 0xC1, 0xB2] Sound Type Sound Command
  25. 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