iOSDC2023-作って学ぶBluetoothの基本攻略 〜スマートキーアプリを作ってみよう〜

11.6K Views

September 03, 23

スライド概要

iOSDC2023
9/3 Track D 13:00〜
登壇資料

シェア

またはPlayer版

埋め込む »CMSなどでJSが使えない場合

関連スライド

各ページのテキスト
1.

作って学ぶBluetoothの基本攻略 〜スマートキーアプリを作ってみよう〜 iOSDC Japan 2023 Day2 - Track D 13:00〜 oka yuji

2.

完成イメージ

3.

自己紹介 • oka yuji(おか ゆうじ) • @oka̲yuuji • 工業高校教諭(電気・電子・情報) → NRIネットコム株式会社 → サイボウズ株式会社

4.

トークの概要 話すこと ・Bluetoothの基本的な話 ・できる限り分かりやすく噛み砕いて話します ・スマートキーを作成しながら解説 ・体験的に学べるように話します

5.

トークの概要 話さないこと ・マイコン側の詳細な話 ・アーキテクチャに関する話

6.

アジェンダ • Bluetoothについて • スマートキーの開発 • 材料 • 実装 • 最後に

7.

Bluetooth

8.

Bluetoothの概要 Bluetoothとは? https://www.apple.com/jp/shop/buy-iphone/ https://www.apple.com/jp/ios/ iphone-14-pro health/ https://www.apple.com/jp/shop/buy-watch/ https://www.apple.com/jp/ apple-watch healthcare/

9.

Bluetoothの概要 Bluetoothとは? https:// www.apple.com/jp/ shop/buy-watch/ apple-watch https://www.apple.com/jp/shop/buy-iphone/ iphone-14-pro https://www.apple.com/jp/shop/ buy-ipad https://www.apple.com/ jp/shop/product/ MQD83J/A/airpods-pro

10.

Bluetoothの概要 Bluetoothとは? https://www.apple.com/jp/shop/buy-iphone/ iphone-14-pro

11.

Bluetoothの概要 Bluetoothとは? 無線通信規格 近距離無線通信規格 Wi-Fi Bluetooth NFC

12.

Bluetoothの概要 近距離無線通信規格について 無線通信規格の一つ 厳密に〇〇m〜〇〇mは近距離無線通信規格みたいな明確なものはない ただ10m〜100mぐらいを近距離と定義しているものが多い 近距離無線通信規格として分類される

13.

https://www.bluetooth.com

14.

https://ja.wikipedia.org/wiki/Bluetooth

15.

Bluetoothの概要 Bluetoothとは? iPhone 3G iPhone 4S Bluetooth 2.0 Bluetooth 4.0 → BLE(Bluetooth Low Energy) iPhone 8 Bluetooth 5.0 iPhone 14 Bluetooth 5.3

16.

Bluetoothの概要 Bluetoothとは? iPhone 3G iPhone 4S Bluetooth 2.0 Bluetooth 4.0 → BLE(Bluetooth Low Energy) iPhone 8 Bluetooth 5.0 iPhone 14 Bluetooth 5.3

17.

Bluetoothの概要 Bluetoothとは? iPhone 3G iPhone 4S Bluetooth 2.0 Bluetooth 4.0 → BLE(Bluetooth Low Energy) iPhone 8 Bluetooth 5.0 iPhone 14 Bluetooth 5.3

18.

https://www.bluetooth.com/ja-jp/learn-about-bluetooth/tech-overview/

19.

Bluetoothの概要 Bluetoothとは? Bluetooth Basic Rate/Enhanced Data Rate (BR/EDR) → Bluetooth Classic 音楽ストリーミングやファイル転送など、連続的で高帯域幅 のデータストリームが必要なアプリケーションに最適 BLEに比べ消費電力が高い https://www.bluetooth.com/jajp/learn-about-bluetooth/techoverview/

20.

Bluetoothの概要 Bluetoothとは? Bluetooth Low Energy (LE) → BLE Bluetooth 4.0から導入 エネルギー効率を大幅に向上 電力消費を大幅に抑えつつ、他のデバイス との接続を維持することができる https://www.bluetooth.com/ja-jp/learn-about-bluetooth/techoverview/

22.

Bluetoothの概要 Bluetoothの特徴 ・近距離で安定したワイヤレス通信を行うことができる ・複数同時接続が可能 ・BLEやBluetooth Classicなど用途に特化した規格がある ・ペアリング機能でセキュリティを高めることできる などなど

23.

Bluetoothの概要 Bluetoothの特徴 ・素早い接続でのデータのやりとり ・ほぼ電力が無い環境 ・長距離間での通信

24.

BLEの通信接続

25.

BLEの通信接続 Peripheral Central Advertise スキャン・接続要求 接続・GATT通信 Service Characteristic Characteristic 切断 Advertise ・ ・ ・ ・・・

26.

BLEの通信接続 Peripheral Central Advertise スキャン・接続要求 接続・GATT通信 Service Characteristic Characteristic 切断 Advertise ・ ・ ・ ・・・

27.

BLEの通信接続 Peripheral Central Advertise スキャン・接続要求 接続・GATT通信 Service Characteristic Characteristic 切断 Advertise ・ ・ ・ ・・・

28.

BLEの通信接続 Peripheral Central Advertise スキャン・接続要求 接続・GATT通信 Service Characteristic Characteristic 切断 Advertise ・ ・ ・ ・・・

29.

BLEの通信接続 Peripheral Central Advertise スキャン・接続要求 接続・GATT通信 Service Characteristic Characteristic 切断 Advertise ・ ・ ・ ・・・

30.

BLEの通信接続 Peripheral Central Advertise スキャン・接続要求 接続・GATT通信 Service Characteristic Characteristic 切断 Advertise ・ ・ ・ ・・・

31.

BLEの通信接続 Peripheral Central Advertise スキャン・接続要求 接続・GATT通信 Service Characteristic Characteristic 切断 Advertise ・ ・ ・ ・・・

32.

BLEの通信接続 Peripheral Central Advertise スキャン・接続要求 接続・GATT通信 Service Characteristic Characteristic 切断 Advertise ・ ・ ・ ・・・

33.

BLEの基本 ペリフェラル側の実装 ・アドバタイズを行う ・セントラルから値を受け取る ・受け取った値によって鍵の開閉を行う →鍵の開閉にサーボモータを使用 ・受け取った値が正常に受け取れていることをセントラルに通知する

34.

BLEの基本 セントラル側の実装 ・スキャンし特定のペリフェラルを見つけたら接続する ・鍵の開閉を行う為、値を送る ・送った値の結果を受け取る ・接続の解除も任意のタイミングでできる

35.

BLEの基本 実装の流れ Advertise スキャン・接続 接続・GATT通信 Characteristic(Write) Characteristic(Notify) 鍵の開閉

36.

材料

40.

材料 サーボモータとは •制御ができる類のモータ •回転速度や回転位置の制御も可能 →産業ロボットや精密機械の制御で幅広く使用

42.

材料 ESP32とは • BluetoothやWi-Fiを搭載したMCU(Micro Controller Unit) • 言語 Arduino、C、MicroPyhon • 開発環境(Editor) ESP-IDF Arduino IDE • 価格 安価で1000円前後 VSCode

43.

材料 ESP32とは •他のマイコンに比べて小さい •標準でBluetoothとWi-Fiモジュールを搭載 •セットアップなど不要 •低消費電力 •安価

44.

材料 ESP32とは •他のマイコンに比べて小さい •標準でBluetoothとWi-Fiモジュールを搭載 •セットアップなど不要 •低消費電力 •安価

45.

材料 • • • • • • • • ESP-WROOM-32 サーボモータ(SG92R) ブレッドボード 取り付け用金具 クリップ 結束バンド 両面テープ モバイルバッテリー などなど

46.

実装

47.

BLEの通信接続 Peripheral Central Advertise スキャン・接続要求 接続・GATT通信 Service Characteristic Characteristic 切断 Advertise ・ ・ ・ ・・・

48.

実装 Core Bluetooth

49.

実装 Core Bluetooth

50.

BLEManagerの実装 実装イメージ

51.

BLEManagerの実装 実装イメージ

52.

BLEManagerの実装 実装イメージ

53.

BLEManagerの実装 実装コード

54.

BLEManagerの実装 実装コード

55.

BLEManagerの実装 実装コード

56.

BLEManagerの実装 実装コード

58.

BLEManagerの実装 実装コード

59.

BLEManagerの実装 実装コード

60.

BLEManagerの実装 実装コード

61.

BLEManagerの実装 実装コード

62.

BLEManagerの実装 実装コード

63.

BLEManagerの実装 実装コード

64.

BLEManagerの実装 実装コード

65.

CBCentralManagerDelegate

67.

CBCentralManagerDelegateの実装 実装コード

68.

CBCentralManagerDelegateの実装 実装コード

69.

CBCentralManagerDelegateの実装 実装コード

70.

CBCentralManagerDelegateの実装 実装コード

71.

CBCentralManagerDelegateの実装 実装コード

72.

CBCentralManagerDelegateの実装 実装コード

73.

CBCentralManagerDelegateの実装 実装コード

74.

CBCentralManagerDelegateの実装 実装コード

75.

CBCentralManagerDelegateの実装 実装コード

76.

CBCentralManagerDelegateの実装 実装コード

77.

CBPeripheralDelegate

79.

CBPeripheralDelegateの実装 実装コード

80.

CBPeripheralDelegateの実装 実装コード

81.

CBPeripheralDelegateの実装 実装コード

82.

CBPeripheralDelegateの実装 実装コード

83.

動作確認

85.

BLEの通信接続 Peripheral Central Advertise スキャン・接続要求 接続・GATT通信 Service Characteristic Characteristic 切断 Advertise ・ ・ ・ ・・・

87.

Peripheralの実装 主要箇所の解説

88.

Peripheralの実装 主要箇所の解説

89.

Peripheralの実装 主要箇所の解説 • NimBLEAdvertisementData • setSecurityAuth(false, false, false) • NimBLEService • NimBLECharacteristic

90.

Peripheralの実装 主要箇所の解説

91.

Peripheralの実装 主要箇所の解説

92.

Peripheralの実装 主要箇所の解説

97.

Peripheralの実装 主要箇所の解説 •servo.write(0); •servo.write(90);

98.

配線・組み立て

105.

完成

107.

最後に 所感 • 日常的に使用するものを自分で作るのは楽しい • 全部で3000円以下で揃えらる • Bluetoothに関してはCentralとPeripheralの関係が分かれば、簡単な実装な らサクッとできる

108.

最後に 改善点 • このままでは待機時の消費電力が大きい • 3Dプリンターなどでカバーを作って隠蔽する • 外出中にも鍵の施錠をチェックしたり制御できるようにする

109.

ご静聴ありがとうございました

110.

その他 参考資料 • Online UUID Generator https://www.uuidgenerator.net/ • Core Bluetooth https://developer.apple.com/documentation/corebluetooth • ESP32を使用してスマートキーを自作する https://tech.nri-net.com/entry/smartkey̲with̲esp32 • WOKWI https://wokwi.com/

111.

その他 参考資料 • 秋月電子 https://akizukidenshi.com/catalog/g/gM-08761/ https://akizukidenshi.com/catalog/g/gM-08914/ • Bluetooth SIG https://www.bluetooth.com/ja-jp/ • GitHub https://github.com/oka-yuji/SmartKeySamplerWithESP32 • iOS BLE Core Bluetoothプログラミング 著:堤 修一, 松村 礼央

112.

その他 免責事項 • 公開している資料やコードについてはあくまで個人で楽しむ範囲を想定した もので、全て自己責任のもと、ご利用ください。 • トークを中心に作成した資料となりますので一部正確性に欠ける表現もあり ます。 • スライドの資料は2023年8月頃に作成されたもので、将来的に内容に変更が 生じる場合があります。