iosアプリ開発者のための bluetooth low energy体験講座
DESCRIPTION
2013年3月22日に大垣市ドリームコアで開催したiOS開発者向けBluetooth Low Energyハンズオンの資料です。 BTLEデバイスと連携するアプリケーション開発、また企画立案に役立つ開発情報を盛り込みました。 実際に動くコードを使って、CoreBluetoothフレームワークおよびBTLEのプロトコルスタック(GATT,ATT)を詳しく、掘り下げています。TRANSCRIPT
![Page 1: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/1.jpg)
Bluetooth LE体験講座
ドリームコア 1F モバイル・コア3月22日
(合)わふう 上原 昭宏
13年3月25日月曜日
![Page 2: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/2.jpg)
目次
• Bluetooth Low Energyって?
• キーホルダーをいじってみる
• iOSらしいBluetooth LEの使い方?
13年3月25日月曜日
![Page 3: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/3.jpg)
Bluetooth Low Energyとは
13年3月25日月曜日
![Page 4: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/4.jpg)
Bluetooth Low Energyとは
• 超低消費電力無線通信• コイン電池(CR2032、直径2cm、3.2mm厚)で1~2年の無線通信
13年3月25日月曜日
![Page 5: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/5.jpg)
Bluetooth Low Energyとは
• 超低消費電力無線通信• コイン電池(CR2032、直径2cm、3.2mm厚)で1~2年の無線通信
• Bluetooth4で統合
• Bluetooth4 = Bluetooth3 + Low Energy (BTLE)
• 2.4GHz(2400~2480MHz)。クラシックBTと異なる変調方式、チャネル。
13年3月25日月曜日
![Page 6: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/6.jpg)
Bluetooth Low Energyとは
• 超低消費電力無線通信• コイン電池(CR2032、直径2cm、3.2mm厚)で1~2年の無線通信
• Bluetooth4で統合
• Bluetooth4 = Bluetooth3 + Low Energy (BTLE)
• 2.4GHz(2400~2480MHz)。クラシックBTと異なる変調方式、チャネル。
• 少量データ、低頻度• 例:<20バイト、秒に1回。フィットネス、医療
13年3月25日月曜日
![Page 7: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/7.jpg)
BTLEの使いどころ
13年3月25日月曜日
![Page 8: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/8.jpg)
BTLEの使いどころ
13年3月25日月曜日
![Page 9: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/9.jpg)
BTLEの使いどころ
13年3月25日月曜日
![Page 10: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/10.jpg)
BTLEの使いどころ• 運動、健康、近接...
13年3月25日月曜日
![Page 11: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/11.jpg)
BTLEの使いどころ• 運動、健康、近接...
• Appcessory
13年3月25日月曜日
![Page 12: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/12.jpg)
BTLEの使いどころ• 運動、健康、近接...
• Appcessory
• ハード、スマフォ、ネット
13年3月25日月曜日
![Page 13: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/13.jpg)
BTLEの使いどころ• 運動、健康、近接...
• Appcessory
• ハード、スマフォ、ネット• ぶっちゃけ
13年3月25日月曜日
![Page 14: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/14.jpg)
BTLEの使いどころ• 運動、健康、近接...
• Appcessory
• ハード、スマフォ、ネット• ぶっちゃけ• ハードメーカがスマフォで売れるぜは死亡フラグ
13年3月25日月曜日
![Page 15: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/15.jpg)
BTLEの使いどころ• 運動、健康、近接...
• Appcessory
• ハード、スマフォ、ネット• ぶっちゃけ• ハードメーカがスマフォで売れるぜは死亡フラグ
• 異世界にようこそ
13年3月25日月曜日
![Page 16: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/16.jpg)
iOSとBTLE• iOSデバイスのBluetooth4対応• iPhone4S (2011年10月発売) 以降のApple社製品 、iOS5 / iOS6
• BTLE対応アプリのストア承認• Made for iPhone(MFi) NDA締結不要 (ロゴ掲載には必要)
• ハードウェアの提出は求められるかも
• 開発環境• CoreBluetoothフレームワーク (一般開発者が使える)
• 従来BT
• MFi必須。
13年3月25日月曜日
![Page 17: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/17.jpg)
実際に動かしてみる
13年3月25日月曜日
![Page 18: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/18.jpg)
デバイスの確認
• iOS5/6の iPhone4S以降のiOSデバイス
• BTLE キーホルダ(BSHSBTPT01BK)
• Xcode 4.5.1以上
13年3月25日月曜日
![Page 19: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/19.jpg)
サンプルコードを開く
• ソースコード• https://github.com/reinforce-lab/CoreBluetooth_samples
• プロジェクト first_sample/KeyFobSample.xcodeproj
13年3月25日月曜日
![Page 20: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/20.jpg)
keyfobと接続• デバイス一覧• RSSIが一番大きい(マイナスの数字が小さい)
行を選択
• Immediate alert
• Buzzerを押す
13年3月25日月曜日
![Page 21: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/21.jpg)
keyfobと接続• デバイス一覧• RSSIが一番大きい(マイナスの数字が小さい)
行を選択
• Immediate alert
• Buzzerを押す
13年3月25日月曜日
![Page 22: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/22.jpg)
keyfobと接続• デバイス一覧• RSSIが一番大きい(マイナスの数字が小さい)
行を選択
• Immediate alert
• Buzzerを押す
13年3月25日月曜日
![Page 23: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/23.jpg)
BTLEデバイスとの通信手順
• デバイスの発見
• 接続
• 読み書き
• 切断
13年3月25日月曜日
![Page 24: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/24.jpg)
コードを読んでみる
13年3月25日月曜日
![Page 25: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/25.jpg)
フレームワークの追加
13年3月25日月曜日
![Page 26: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/26.jpg)
フレームワークの追加
13年3月25日月曜日
![Page 27: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/27.jpg)
フレームワークの追加
13年3月25日月曜日
![Page 28: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/28.jpg)
フレームワークの追加
#import "KeyFobController.h"#import <CoreBluetooth/CoreBluetooth.h>
@interface KeyFobController() <CBCentralManagerDelegate, CBPeripheralDelegate> { CBCentralManager *_centralManager;
13年3月25日月曜日
![Page 29: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/29.jpg)
フレームワークの追加
• CoreBluetooth.frameworkを追加
• #import <CoreBluetooth/CoreBluetooth.h>
• CBCentralManagerDelegate, CBPeripheralDelegate を実装
#import "KeyFobController.h"#import <CoreBluetooth/CoreBluetooth.h>
@interface KeyFobController() <CBCentralManagerDelegate, CBPeripheralDelegate> { CBCentralManager *_centralManager;
13年3月25日月曜日
![Page 30: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/30.jpg)
デバイスの発見
13年3月25日月曜日
![Page 31: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/31.jpg)
ネットワーク・トポロジ• スター型• 接続数の上限がない• クラシックBTは7
• 役割が非対称• ペリフェラルがより低消費電力
セントラル(Central)
ペリフェラル(Peripheral)
13年3月25日月曜日
![Page 32: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/32.jpg)
データモデル
セントラル(Central)
ペリフェラル(Peripheral)
CBCentralManager
CBPeripheral
13年3月25日月曜日
![Page 33: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/33.jpg)
ペリフェラル・デバイスの発見
• アドバタイズメント
• パケットを送信
• 20ミリ秒~1.3秒周期
• デバイス名などの情報
Ad
Ad
Ad
Ad
13年3月25日月曜日
![Page 34: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/34.jpg)
13年3月25日月曜日
![Page 35: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/35.jpg)
13年3月25日月曜日
![Page 36: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/36.jpg)
13年3月25日月曜日
![Page 37: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/37.jpg)
-(void)startScanning { // BLEデバイスのスキャン時には、検索対象とするサービスを指定することが推奨です NSArray *scanServices = [NSArray arrayWithObjects:_linkLossServiceUUID, _immediateAlertServiceUUID, nil]; // スキャンにはオプションが指定できます。 // いまあるオプションは、ペリフェラルを見つけた時に重複して通知するか、の指定です。 // 近接検出など、コネクションレスでデバイスの状態を取得する用途などでは、これをYESに設定します。 // デフォルトでNOです。 NSDictionary *scanOptions = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:CBCentralManagerScanOptionAllowDuplicatesKey]; // デバイスのスキャン開始。iPhoneはアドバタイズメント・パケットの受信を開始します。 // このスキャンは、明示的に停止しない限り、スキャンし続けます。このスキャンは、アドバタイズメント・パケットの(2.4GHzの)受信処理で、RF回路は電力を消費します。 [_centralManager scanForPeripheralsWithServices:scanServices options:scanOptions];}
13年3月25日月曜日
![Page 38: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/38.jpg)
-(void)startScanning { // BLEデバイスのスキャン時には、検索対象とするサービスを指定することが推奨です NSArray *scanServices = [NSArray arrayWithObjects:_linkLossServiceUUID, _immediateAlertServiceUUID, nil]; // スキャンにはオプションが指定できます。 // いまあるオプションは、ペリフェラルを見つけた時に重複して通知するか、の指定です。 // 近接検出など、コネクションレスでデバイスの状態を取得する用途などでは、これをYESに設定します。 // デフォルトでNOです。 NSDictionary *scanOptions = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:CBCentralManagerScanOptionAllowDuplicatesKey]; // デバイスのスキャン開始。iPhoneはアドバタイズメント・パケットの受信を開始します。 // このスキャンは、明示的に停止しない限り、スキャンし続けます。このスキャンは、アドバタイズメント・パケットの(2.4GHzの)受信処理で、RF回路は電力を消費します。 [_centralManager scanForPeripheralsWithServices:scanServices options:scanOptions];}
13年3月25日月曜日
![Page 39: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/39.jpg)
-(void)startScanning { // BLEデバイスのスキャン時には、検索対象とするサービスを指定することが推奨です NSArray *scanServices = [NSArray arrayWithObjects:_linkLossServiceUUID, _immediateAlertServiceUUID, nil]; // スキャンにはオプションが指定できます。 // いまあるオプションは、ペリフェラルを見つけた時に重複して通知するか、の指定です。 // 近接検出など、コネクションレスでデバイスの状態を取得する用途などでは、これをYESに設定します。 // デフォルトでNOです。 NSDictionary *scanOptions = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:CBCentralManagerScanOptionAllowDuplicatesKey]; // デバイスのスキャン開始。iPhoneはアドバタイズメント・パケットの受信を開始します。 // このスキャンは、明示的に停止しない限り、スキャンし続けます。このスキャンは、アドバタイズメント・パケットの(2.4GHzの)受信処理で、RF回路は電力を消費します。 [_centralManager scanForPeripheralsWithServices:scanServices options:scanOptions];}
13年3月25日月曜日
![Page 40: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/40.jpg)
-(void)startScanning { // BLEデバイスのスキャン時には、検索対象とするサービスを指定することが推奨です NSArray *scanServices = [NSArray arrayWithObjects:_linkLossServiceUUID, _immediateAlertServiceUUID, nil]; // スキャンにはオプションが指定できます。 // いまあるオプションは、ペリフェラルを見つけた時に重複して通知するか、の指定です。 // 近接検出など、コネクションレスでデバイスの状態を取得する用途などでは、これをYESに設定します。 // デフォルトでNOです。 NSDictionary *scanOptions = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:CBCentralManagerScanOptionAllowDuplicatesKey]; // デバイスのスキャン開始。iPhoneはアドバタイズメント・パケットの受信を開始します。 // このスキャンは、明示的に停止しない限り、スキャンし続けます。このスキャンは、アドバタイズメント・パケットの(2.4GHzの)受信処理で、RF回路は電力を消費します。 [_centralManager scanForPeripheralsWithServices:scanServices options:scanOptions];}
13年3月25日月曜日
![Page 41: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/41.jpg)
アドバタイズメントのフィルタ
• 重複を許すか• 通常はNO
• 近接ならYES
• ”受信したとき”に通知
• サービス(機能)でフィルタ
Ad
Ad
Ad
Ad
13年3月25日月曜日
![Page 42: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/42.jpg)
// デバイスの発見時処理を行います。// スキャンで指定したサービスがあるデバイスのみが通知されますが、同じような機能を持った対象外の装置を発見するかもしれません。// アドバタイズメント・パケットに含まれる情報は、advertisementDataから得られます。デバイス名などで判別します。- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)p advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI{ // 同一機種が周囲に複数あるとき、自分がもっているものだけに接続するには、UUIDが同じかを判定します。 // !!注意!! iOS6では、一度も接続したことがない装置のUUIDはnilになります。一度でも接続すればUUIDが取れます。ここでは、一度は接続したことがある前提でUUIDを比較しています。 /* CBUUID *targetUUID = [CBUUID UUIDWithString:@"00000000-0000-0000-2F0C-F0289947EA35"]; if(peripheral.UUID == nil || ! [[CBUUID UUIDWithCFUUID:peripheral.UUID].data isEqualToData:targetUUID.data]) return; */ NSString *localName = [advertisementData objectForKey:CBAdvertisementDataLocalNameKey]; if(localName != nil && [localName isEqualToString:kTargetDeviceName] ) { PeripheralContainer *c = [[PeripheralContainer alloc] init]; c.peripheral = p; c.RSSI = RSSI; [self findPeripheral:c]; }}
13年3月25日月曜日
![Page 43: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/43.jpg)
// デバイスの発見時処理を行います。// スキャンで指定したサービスがあるデバイスのみが通知されますが、同じような機能を持った対象外の装置を発見するかもしれません。// アドバタイズメント・パケットに含まれる情報は、advertisementDataから得られます。デバイス名などで判別します。- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)p advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI{ // 同一機種が周囲に複数あるとき、自分がもっているものだけに接続するには、UUIDが同じかを判定します。 // !!注意!! iOS6では、一度も接続したことがない装置のUUIDはnilになります。一度でも接続すればUUIDが取れます。ここでは、一度は接続したことがある前提でUUIDを比較しています。 /* CBUUID *targetUUID = [CBUUID UUIDWithString:@"00000000-0000-0000-2F0C-F0289947EA35"]; if(peripheral.UUID == nil || ! [[CBUUID UUIDWithCFUUID:peripheral.UUID].data isEqualToData:targetUUID.data]) return; */ NSString *localName = [advertisementData objectForKey:CBAdvertisementDataLocalNameKey]; if(localName != nil && [localName isEqualToString:kTargetDeviceName] ) { PeripheralContainer *c = [[PeripheralContainer alloc] init]; c.peripheral = p; c.RSSI = RSSI; [self findPeripheral:c]; }}
13年3月25日月曜日
![Page 44: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/44.jpg)
// デバイスの発見時処理を行います。// スキャンで指定したサービスがあるデバイスのみが通知されますが、同じような機能を持った対象外の装置を発見するかもしれません。// アドバタイズメント・パケットに含まれる情報は、advertisementDataから得られます。デバイス名などで判別します。- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)p advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI{ // 同一機種が周囲に複数あるとき、自分がもっているものだけに接続するには、UUIDが同じかを判定します。 // !!注意!! iOS6では、一度も接続したことがない装置のUUIDはnilになります。一度でも接続すればUUIDが取れます。ここでは、一度は接続したことがある前提でUUIDを比較しています。 /* CBUUID *targetUUID = [CBUUID UUIDWithString:@"00000000-0000-0000-2F0C-F0289947EA35"]; if(peripheral.UUID == nil || ! [[CBUUID UUIDWithCFUUID:peripheral.UUID].data isEqualToData:targetUUID.data]) return; */ NSString *localName = [advertisementData objectForKey:CBAdvertisementDataLocalNameKey]; if(localName != nil && [localName isEqualToString:kTargetDeviceName] ) { PeripheralContainer *c = [[PeripheralContainer alloc] init]; c.peripheral = p; c.RSSI = RSSI; [self findPeripheral:c]; }}
受信信号強度パケット・データ
13年3月25日月曜日
![Page 45: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/45.jpg)
// デバイスの発見時処理を行います。// スキャンで指定したサービスがあるデバイスのみが通知されますが、同じような機能を持った対象外の装置を発見するかもしれません。// アドバタイズメント・パケットに含まれる情報は、advertisementDataから得られます。デバイス名などで判別します。- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)p advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI{ // 同一機種が周囲に複数あるとき、自分がもっているものだけに接続するには、UUIDが同じかを判定します。 // !!注意!! iOS6では、一度も接続したことがない装置のUUIDはnilになります。一度でも接続すればUUIDが取れます。ここでは、一度は接続したことがある前提でUUIDを比較しています。 /* CBUUID *targetUUID = [CBUUID UUIDWithString:@"00000000-0000-0000-2F0C-F0289947EA35"]; if(peripheral.UUID == nil || ! [[CBUUID UUIDWithCFUUID:peripheral.UUID].data isEqualToData:targetUUID.data]) return; */ NSString *localName = [advertisementData objectForKey:CBAdvertisementDataLocalNameKey]; if(localName != nil && [localName isEqualToString:kTargetDeviceName] ) { PeripheralContainer *c = [[PeripheralContainer alloc] init]; c.peripheral = p; c.RSSI = RSSI; [self findPeripheral:c]; }}
受信信号強度パケット・データ
13年3月25日月曜日
![Page 46: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/46.jpg)
アドバタイズメントで取れる情報 (1/1)
• 名前、送信電力、その他• CBAdvertisementDataLocalNameKey
• CBAdvertisementDataTxPowerLevelKey
• CBAdvertisementDataManufacturerDataKey
• この3つの情報はキャッシュされない
• LocalName以外はペリフェラルの設計次第
• 周囲へ一斉通知(ブロードキャスト)
• 送信電力とRSSIから距離概算
13年3月25日月曜日
![Page 47: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/47.jpg)
アドバタイズメントで取れる情報 (2/2)
• CBPeripheral インスタンス
• UUID : 128-bitの識別番号
• 一度でも接続したことがあれば≠nil
• RSSI : 受信信号強度
• 接続してreadRSSI:を呼び出せば≠nil
• 切断しても同じUUIDを見つけて再接続
• LocalNameに、識別番号や、状態を入れたり
13年3月25日月曜日
![Page 48: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/48.jpg)
接続と切断
13年3月25日月曜日
![Page 49: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/49.jpg)
-(void)connect:(PeripheralContainer *)c { //ターゲットを発見、接続します //この時点でperipheralはcentral managerに保持されていません。 //少なくとも接続が完了するまでの間、peripheralをアプリ側で保持します。 //接続処理はタイムアウトしません。接続に失敗すれば centralManager:didFailToConnectPeripheral:error: が呼ばれます。 //接続処理を中止するには、peripheralを開放するか、明示的に cancelPeripheralConnection を呼び出します。 self.peripheral = c.peripheral;
[_centralManager connectPeripheral:self.peripheral options:nil];}-(void)disconnect { // 切断処理をします。_peripheralをnilにするなどの、切断完了後処理は、 // centralManager:didDisconnectPeripheral:error: // で行います。 [_centralManager cancelPeripheralConnection:self.peripheral];}
13年3月25日月曜日
![Page 50: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/50.jpg)
-(void)connect:(PeripheralContainer *)c { //ターゲットを発見、接続します //この時点でperipheralはcentral managerに保持されていません。 //少なくとも接続が完了するまでの間、peripheralをアプリ側で保持します。 //接続処理はタイムアウトしません。接続に失敗すれば centralManager:didFailToConnectPeripheral:error: が呼ばれます。 //接続処理を中止するには、peripheralを開放するか、明示的に cancelPeripheralConnection を呼び出します。 self.peripheral = c.peripheral;
[_centralManager connectPeripheral:self.peripheral options:nil];}-(void)disconnect { // 切断処理をします。_peripheralをnilにするなどの、切断完了後処理は、 // centralManager:didDisconnectPeripheral:error: // で行います。 [_centralManager cancelPeripheralConnection:self.peripheral];}
13年3月25日月曜日
![Page 51: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/51.jpg)
-(void)connect:(PeripheralContainer *)c { //ターゲットを発見、接続します //この時点でperipheralはcentral managerに保持されていません。 //少なくとも接続が完了するまでの間、peripheralをアプリ側で保持します。 //接続処理はタイムアウトしません。接続に失敗すれば centralManager:didFailToConnectPeripheral:error: が呼ばれます。 //接続処理を中止するには、peripheralを開放するか、明示的に cancelPeripheralConnection を呼び出します。 self.peripheral = c.peripheral;
[_centralManager connectPeripheral:self.peripheral options:nil];}-(void)disconnect { // 切断処理をします。_peripheralをnilにするなどの、切断完了後処理は、 // centralManager:didDisconnectPeripheral:error: // で行います。 [_centralManager cancelPeripheralConnection:self.peripheral];}
必ずretain
13年3月25日月曜日
![Page 52: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/52.jpg)
-(void)connect:(PeripheralContainer *)c { //ターゲットを発見、接続します //この時点でperipheralはcentral managerに保持されていません。 //少なくとも接続が完了するまでの間、peripheralをアプリ側で保持します。 //接続処理はタイムアウトしません。接続に失敗すれば centralManager:didFailToConnectPeripheral:error: が呼ばれます。 //接続処理を中止するには、peripheralを開放するか、明示的に cancelPeripheralConnection を呼び出します。 self.peripheral = c.peripheral;
[_centralManager connectPeripheral:self.peripheral options:nil];}-(void)disconnect { // 切断処理をします。_peripheralをnilにするなどの、切断完了後処理は、 // centralManager:didDisconnectPeripheral:error: // で行います。 [_centralManager cancelPeripheralConnection:self.peripheral];}
必ずretain
13年3月25日月曜日
![Page 53: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/53.jpg)
-(void)connect:(PeripheralContainer *)c { //ターゲットを発見、接続します //この時点でperipheralはcentral managerに保持されていません。 //少なくとも接続が完了するまでの間、peripheralをアプリ側で保持します。 //接続処理はタイムアウトしません。接続に失敗すれば centralManager:didFailToConnectPeripheral:error: が呼ばれます。 //接続処理を中止するには、peripheralを開放するか、明示的に cancelPeripheralConnection を呼び出します。 self.peripheral = c.peripheral;
[_centralManager connectPeripheral:self.peripheral options:nil];}-(void)disconnect { // 切断処理をします。_peripheralをnilにするなどの、切断完了後処理は、 // centralManager:didDisconnectPeripheral:error: // で行います。 [_centralManager cancelPeripheralConnection:self.peripheral];}
必ずretain
13年3月25日月曜日
![Page 54: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/54.jpg)
-(void)connect:(PeripheralContainer *)c { //ターゲットを発見、接続します //この時点でperipheralはcentral managerに保持されていません。 //少なくとも接続が完了するまでの間、peripheralをアプリ側で保持します。 //接続処理はタイムアウトしません。接続に失敗すれば centralManager:didFailToConnectPeripheral:error: が呼ばれます。 //接続処理を中止するには、peripheralを開放するか、明示的に cancelPeripheralConnection を呼び出します。 self.peripheral = c.peripheral;
[_centralManager connectPeripheral:self.peripheral options:nil];}-(void)disconnect { // 切断処理をします。_peripheralをnilにするなどの、切断完了後処理は、 // centralManager:didDisconnectPeripheral:error: // で行います。 [_centralManager cancelPeripheralConnection:self.peripheral];}
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)p;
- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error;
必ずretain
13年3月25日月曜日
![Page 55: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/55.jpg)
-(void)connect:(PeripheralContainer *)c { //ターゲットを発見、接続します //この時点でperipheralはcentral managerに保持されていません。 //少なくとも接続が完了するまでの間、peripheralをアプリ側で保持します。 //接続処理はタイムアウトしません。接続に失敗すれば centralManager:didFailToConnectPeripheral:error: が呼ばれます。 //接続処理を中止するには、peripheralを開放するか、明示的に cancelPeripheralConnection を呼び出します。 self.peripheral = c.peripheral;
[_centralManager connectPeripheral:self.peripheral options:nil];}-(void)disconnect { // 切断処理をします。_peripheralをnilにするなどの、切断完了後処理は、 // centralManager:didDisconnectPeripheral:error: // で行います。 [_centralManager cancelPeripheralConnection:self.peripheral];}
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)p;
- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error;
必ずretain
delegateで通知13年3月25日月曜日
![Page 56: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/56.jpg)
サービスとキャラクタリスティクスそしてプロファイル
13年3月25日月曜日
![Page 57: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/57.jpg)
プロトコルスタックApplicationsCore Bluetooth
GATT
ATT
L2CAP
PHY
Link Layer
ApplicationsApplications
13年3月25日月曜日
![Page 58: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/58.jpg)
プロトコルスタックApplicationsCore Bluetooth
GATT
ATT
L2CAP
PHY
Link Layer
ApplicationsApplications
XPC
BTserver
13年3月25日月曜日
![Page 59: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/59.jpg)
プロトコルスタックApplicationsCore Bluetooth
GATT
ATT
L2CAP
PHY
Link Layer
ApplicationsApplications
XPC
BTserver
13年3月25日月曜日
![Page 60: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/60.jpg)
プロトコルスタックApplicationsCore Bluetooth
GATT
ATT
L2CAP
PHY
Link Layer
ApplicationsApplications
XPC
BTserver
13年3月25日月曜日
![Page 61: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/61.jpg)
プロトコルスタックApplicationsCore Bluetooth
GATT
ATT
L2CAP
PHY
Link Layer
ApplicationsApplications
XPC
BTserver
ペリフェラル=データベースハンドル,タイプ,値
13年3月25日月曜日
![Page 62: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/62.jpg)
プロトコルスタックApplicationsCore Bluetooth
GATT
ATT
L2CAP
PHY
Link Layer
ApplicationsApplications
XPC
BTserver
13年3月25日月曜日
![Page 63: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/63.jpg)
プロトコルスタックApplicationsCore Bluetooth
GATT
ATT
L2CAP
PHY
Link Layer
ApplicationsApplications
XPC
BTserver
サービス=クラスキャラクタリスティクス
=プロパティ13年3月25日月曜日
![Page 64: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/64.jpg)
プロトコルスタックApplicationsCore Bluetooth
GATT
ATT
L2CAP
PHY
Link Layer
ApplicationsApplications
XPC
BTserver
サービス=クラスキャラクタリスティクス
=プロパティ
13年3月25日月曜日
![Page 65: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/65.jpg)
プロトコルスタックApplicationsCore Bluetooth
GATT
ATT
L2CAP
PHY
Link Layer
ApplicationsApplications
XPC
BTserver
サービス=クラスキャラクタリスティクス
=プロパティ
• キャラクタリスティクス• 制御/動作指定
• センサー値/外界の状態
• (装置の)動作状態/内部状態
13年3月25日月曜日
![Page 66: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/66.jpg)
サービスとキャラクタリスティクスそしてプロファイル
13年3月25日月曜日
![Page 67: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/67.jpg)
サービスと振る舞いの分離
http://developer.bluetooth.org/gatt/profiles/Pages/ProfileViewer.aspx?u=org.bluetooth.profile.find_me.xml
Find MEProfile
ProximityProfile
デバイス発見
切断時の警告
13年3月25日月曜日
![Page 68: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/68.jpg)
サービスと振る舞いの分離
http://developer.bluetooth.org/gatt/profiles/Pages/ProfileViewer.aspx?u=org.bluetooth.profile.find_me.xml
ImmediateAlert Service
Tx Power Service
Link LossAlert Service
振動やブザーを出力
送信電力の値切断時の振る舞い指定
Find MEProfile
ProximityProfile
デバイス発見
切断時の警告
13年3月25日月曜日
![Page 69: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/69.jpg)
サービスと振る舞いの分離
http://developer.bluetooth.org/gatt/profiles/Pages/ProfileViewer.aspx?u=org.bluetooth.profile.find_me.xml
ImmediateAlert Service
Tx Power Service
Link LossAlert Service
振動やブザーを出力
送信電力の値切断時の振る舞い指定
Find MEProfile
ProximityProfile
デバイス発見
切断時の警告
13年3月25日月曜日
![Page 70: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/70.jpg)
サービスと振る舞いの分離
http://developer.bluetooth.org/gatt/profiles/Pages/ProfileViewer.aspx?u=org.bluetooth.profile.find_me.xml
ImmediateAlert Service
Tx Power Service
Link LossAlert Service
振動やブザーを出力
送信電力の値切断時の振る舞い指定
Find MEProfile
ProximityProfile
デバイス発見
切断時の警告
ユースケース ハード仕様
13年3月25日月曜日
![Page 71: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/71.jpg)
プロファイルの実装ApplicationsCore Bluetooth
GATT
ATT
L2CAP
PHY
Link Layer
ApplicationsApplications • プロファイルがアプリ側
• 任意の振る舞い、OK
• すべてがアプリの管理下• 発見
• 接続/切断
• 読み書き
• iOSはいっさい関係してこない13年3月25日月曜日
![Page 72: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/72.jpg)
プロファイルの実装ApplicationsCore Bluetooth
GATT
ATT
L2CAP
PHY
Link Layer
ApplicationsApplications
XPC
BTserver
• プロファイルがアプリ側
• 任意の振る舞い、OK
• すべてがアプリの管理下• 発見
• 接続/切断
• 読み書き
• iOSはいっさい関係してこない13年3月25日月曜日
![Page 73: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/73.jpg)
プロファイルの実装ApplicationsCore Bluetooth
GATT
ATT
L2CAP
PHY
Link Layer
ApplicationsApplications
XPC
BTserver
• プロファイルがアプリ側
• 任意の振る舞い、OK
• すべてがアプリの管理下• 発見
• 接続/切断
• 読み書き
• iOSはいっさい関係してこない13年3月25日月曜日
![Page 74: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/74.jpg)
サービスとキャラクタリスティクスを見てみる
13年3月25日月曜日
![Page 75: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/75.jpg)
13年3月25日月曜日
![Page 76: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/76.jpg)
13年3月25日月曜日
![Page 77: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/77.jpg)
• UUIDで識別
• 16-bit / 128-bit
• 読み(read)/書き(write)/通知(notify)13年3月25日月曜日
![Page 78: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/78.jpg)
• UUIDで識別
• 16-bit / 128-bit
• 読み(read)/書き(write)/通知(notify)13年3月25日月曜日
![Page 79: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/79.jpg)
• UUIDで識別
• 16-bit / 128-bit
• 読み(read)/書き(write)/通知(notify)13年3月25日月曜日
![Page 80: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/80.jpg)
• UUIDで識別
• 16-bit / 128-bit
• 読み(read)/書き(write)/通知(notify)13年3月25日月曜日
![Page 81: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/81.jpg)
• UUIDで識別
• 16-bit / 128-bit
• 読み(read)/書き(write)/通知(notify)13年3月25日月曜日
![Page 82: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/82.jpg)
• UUIDで識別
• 16-bit / 128-bit
• 読み(read)/書き(write)/通知(notify)13年3月25日月曜日
![Page 83: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/83.jpg)
• UUIDで識別
• 16-bit / 128-bit
• 読み(read)/書き(write)/通知(notify)13年3月25日月曜日
![Page 84: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/84.jpg)
サービスとキャラクタリスティクスのリスティング
13年3月25日月曜日
![Page 85: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/85.jpg)
// 接続- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)p { // サービスを探します self.peripheral.delegate = self; [p discoverServices:[NSArray arrayWithObjects: _linkLossServiceUUID, _immediateAlertServiceUUID, _txPowerServiceUUID, _batteryLevelServiceUUID, nil]];}
#define kImmediateAlertServiceUUID @"1802"#define kLinkLossServiceUUID @"1803"#define kAlertLevelUUID @"2A06"... _linkLossServiceUUID = [CBUUID UUIDWithString:kLinkLossServiceUUID]; _immediateAlertServiceUUID = [CBUUID UUIDWithString:kImmediateAlertServiceUUID]; _alertLevelUUID = [CBUUID UUIDWithString:kAlertLevelUUID];
13年3月25日月曜日
![Page 86: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/86.jpg)
// 接続- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)p { // サービスを探します self.peripheral.delegate = self; [p discoverServices:[NSArray arrayWithObjects: _linkLossServiceUUID, _immediateAlertServiceUUID, _txPowerServiceUUID, _batteryLevelServiceUUID, nil]];}
#define kImmediateAlertServiceUUID @"1802"#define kLinkLossServiceUUID @"1803"#define kAlertLevelUUID @"2A06"... _linkLossServiceUUID = [CBUUID UUIDWithString:kLinkLossServiceUUID]; _immediateAlertServiceUUID = [CBUUID UUIDWithString:kImmediateAlertServiceUUID]; _alertLevelUUID = [CBUUID UUIDWithString:kAlertLevelUUID];
13年3月25日月曜日
![Page 87: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/87.jpg)
//発見したサービスに対して、Characteristicsを探します- (void)peripheral:(CBPeripheral *)p didDiscoverServices:(NSError *)error { for (CBService *service in p.services) { if ([service.UUID.data isEqualToData:_linkLossServiceUUID.data]) { [p discoverCharacteristics:[NSArray arrayWithObjects: _alertLevelUUID, nil] forService:service]; } else if ([service.UUID.data isEqualToData:_immediateAlertServiceUUID.data]) { [p discoverCharacteristics:[NSArray arrayWithObjects: _alertLevelUUID, nil] forService:service]; } }}
- (void)peripheral:(CBPeripheral *)p didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error{ if ([service.UUID.data isEqualToData:_batteryLevelServiceUUID.data]) { _batteryLevelCharacteristics = [self findCharacteristics:service.characteristics uuid:_batteryLevelUUID]; _batteryLevelSwitchCharacteristics = [self findCharacteristics:service.characteristics uuid:_batteryLevelSwitchUUID]; [p setNotifyValue:YES forCharacteristic:_batteryLevelSwitchCharacteristics]; [p readValueForCharacteristic:_batteryLevelCharacteristics]; [p readValueForCharacteristic:_batteryLevelSwitchCharacteristics]; }}
13年3月25日月曜日
![Page 88: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/88.jpg)
//発見したサービスに対して、Characteristicsを探します- (void)peripheral:(CBPeripheral *)p didDiscoverServices:(NSError *)error { for (CBService *service in p.services) { if ([service.UUID.data isEqualToData:_linkLossServiceUUID.data]) { [p discoverCharacteristics:[NSArray arrayWithObjects: _alertLevelUUID, nil] forService:service]; } else if ([service.UUID.data isEqualToData:_immediateAlertServiceUUID.data]) { [p discoverCharacteristics:[NSArray arrayWithObjects: _alertLevelUUID, nil] forService:service]; } }}
- (void)peripheral:(CBPeripheral *)p didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error{ if ([service.UUID.data isEqualToData:_batteryLevelServiceUUID.data]) { _batteryLevelCharacteristics = [self findCharacteristics:service.characteristics uuid:_batteryLevelUUID]; _batteryLevelSwitchCharacteristics = [self findCharacteristics:service.characteristics uuid:_batteryLevelSwitchUUID]; [p setNotifyValue:YES forCharacteristic:_batteryLevelSwitchCharacteristics]; [p readValueForCharacteristic:_batteryLevelCharacteristics]; [p readValueForCharacteristic:_batteryLevelSwitchCharacteristics]; }}
13年3月25日月曜日
![Page 89: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/89.jpg)
//発見したサービスに対して、Characteristicsを探します- (void)peripheral:(CBPeripheral *)p didDiscoverServices:(NSError *)error { for (CBService *service in p.services) { if ([service.UUID.data isEqualToData:_linkLossServiceUUID.data]) { [p discoverCharacteristics:[NSArray arrayWithObjects: _alertLevelUUID, nil] forService:service]; } else if ([service.UUID.data isEqualToData:_immediateAlertServiceUUID.data]) { [p discoverCharacteristics:[NSArray arrayWithObjects: _alertLevelUUID, nil] forService:service]; } }}
- (void)peripheral:(CBPeripheral *)p didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error{ if ([service.UUID.data isEqualToData:_batteryLevelServiceUUID.data]) { _batteryLevelCharacteristics = [self findCharacteristics:service.characteristics uuid:_batteryLevelUUID]; _batteryLevelSwitchCharacteristics = [self findCharacteristics:service.characteristics uuid:_batteryLevelSwitchUUID]; [p setNotifyValue:YES forCharacteristic:_batteryLevelSwitchCharacteristics]; [p readValueForCharacteristic:_batteryLevelCharacteristics]; [p readValueForCharacteristic:_batteryLevelSwitchCharacteristics]; }}
13年3月25日月曜日
![Page 90: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/90.jpg)
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{ uint8_t b; if(characteristic == _txPowerCharacteristic) { [characteristic.value getBytes:&b length:1]; self.txPower = b;
LSB-first (Least Significant Byte First)
13年3月25日月曜日
![Page 91: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/91.jpg)
まとめ• BTLE
• 超低消費電力、なんとなくつながる
• iOSとBTLE
• MFi不要、一般開発者権限
• 振るまいと機能の分離
• プロファイルはアプリ次第13年3月25日月曜日
![Page 92: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/92.jpg)
開発体制
• ハード+プロファイル部分を担当すべし• 無線接続と振る舞い• デモアプリで動作確認、責任分界• ドキュメント、読める?• 納品後のトラブル (iOSの振舞い変化)
13年3月25日月曜日
![Page 93: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/93.jpg)
参考
13年3月25日月曜日
![Page 94: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/94.jpg)
情報源(1/3)
• https://developer.apple.com/videos/wwdc/2012/
• Session 703: Core Bluetooth 101
• Session 705 Advanced Core Bluetooth
• Apple Bluetooth Mailing list
• https://lists.apple.com/mailman/listinfo/bluetooth-dev
13年3月25日月曜日
![Page 95: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/95.jpg)
情報源(2/3)• Texus Instruments社
• http://www.tij.co.jp/product/jp/cc2540
• http://processors.wiki.ti.com/index.php/Category:BluetoothLE?DCMP=blestack&HQS=ble-wiki
• Nordic Semiconductor社
• https://www.nordicsemi.com
• BlueGiga
• http://www.bluegiga.com
13年3月25日月曜日
![Page 96: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/96.jpg)
情報源(3/3)
• AppleのBluetoothアクセサリ設計指針 (おすすめ)
• Bluetooth Low Energy Handbook (おすすめ)
• Bluetooth4 Core Specificaiton (辞書的に)
13年3月25日月曜日
![Page 97: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/97.jpg)
ぶっちゃけ
13年3月25日月曜日
![Page 98: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/98.jpg)
• CoreBluetoothを使う必要あるの?• API公開(Wahoo、NODE、konashi...)
• ハード販売、サービス展開• 自分で開発展開• ハードウェア開発?• 半導体/モジュール (組込開発)
• iOSでペリフェラル開発 (iOS6から)
13年3月25日月曜日
![Page 99: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/99.jpg)
オリジナル開発
• 半導体買ってきて回路を作ると• BT認証 1万ドル、電波法 40万円(実費)。
• 1万台以上作るなら
• モジュール• 日本の認証OKなのが4月から続々。
• BlueGiga、おすすめ
• BASICみたいなスクリプト開発。
• 冗談みたいなお手軽さ。
13年3月25日月曜日
![Page 100: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/100.jpg)
• ペリフェラルの設計• http://e2e.ti.com/support/low_power_rf/f/538/t/225290.aspx
• リファレンス設計/コード
• 必要な場面• 制御が入るとか、応答速度が必要とか• RFチップ+外付けマイコン
• RFチップ+RFチップ内蔵マイコン• 開発環境 TI社 8051互換 ~40万円、Nordic Semi.社 Cortex-M0 ~20万円 (32kBまで無償
版あり)
13年3月25日月曜日
![Page 101: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/101.jpg)
こまったときの
• 何が何やら、わけがわかんないよ• 通信の把握が勝利の鍵• TI社のCC2540開発キットでパケットスニファ
• iOS6.0で、切断時にiOSが1分ほど接続保持ー>アドバタイズ出ない
13年3月25日月曜日
![Page 102: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/102.jpg)
今時のBTLE
• BTLEデバイス、Over-the-airでファーム更新
• Kickstarterむっちゃでてる
• 10万台の呪い。日本、しんでる
• 覚えていますか♫
• 記憶にすら残らない、NECカシオのMEDIASとリモッピとG-SHOCK。まじ、ショック
13年3月25日月曜日
![Page 103: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/103.jpg)
他のプラットホーム
• Android
• チップメーカ独自スタック、公式BTLE対応なし
• できることはiOSと同じ。GATTを叩ける。
• BT3/4混在、Android4。
• ねらってもできない統一性のなさ• 5月のGoogle IO、ドコモの健康なんとか?
• Windows Phone8
• 公式スタックなし、BT3が最新機種
• Windows8
• ハード認定要件にBT4、スタック公式サポート
• C+でドライバ13年3月25日月曜日
![Page 104: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/104.jpg)
あどばんす
13年3月25日月曜日
![Page 105: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/105.jpg)
• アプリ
• バックグラウンド
• CBPeripheralManager
• ハード開発
• 情報源
13年3月25日月曜日
![Page 106: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/106.jpg)
13年3月25日月曜日
![Page 107: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/107.jpg)
13年3月25日月曜日
![Page 108: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/108.jpg)
13年3月25日月曜日
![Page 109: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/109.jpg)
13年3月25日月曜日
![Page 110: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/110.jpg)
Central
Peripheral
Centralclient
Peripheralserver
13年3月25日月曜日
![Page 111: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/111.jpg)
• サンプルファイル
• ペリフェラル(デバイス)
•
13年3月25日月曜日
![Page 112: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/112.jpg)
CentralPeripheral
CBCentralManager
CBPeripheral
CBPeripheralManager
CBCentral
13年3月25日月曜日
![Page 113: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/113.jpg)
CBCentralManager
CBPeripheral
CBPeripheralManager
CBCentral
iOS5 iOS6
CBService
CBCharacteristics
CBMutableService
CBMutableCharacteri
Data Objects
Main Objects
13年3月25日月曜日
![Page 114: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/114.jpg)
CentralPeripheral
Ad
Ad
Ad
13年3月25日月曜日
![Page 115: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/115.jpg)
discover and connect
CBCentralManager Periphearl device
scanForPeripheralsWithServices
didDiscoverPeripheral
stopScan
connectPeripheral
didConnectPeripheral
13年3月25日月曜日
![Page 116: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/116.jpg)
Central Peripheral
Service
Characteristic
Characteristic
CBService
CBCharacteristic
13年3月25日月曜日
![Page 117: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/117.jpg)
CBPeripheral CBPeripheralDelegate
discoverServicesdidDiscoverServices
discoverCharacteristics:forServicedidDiscoverCharacteristicsForSer
vice
13年3月25日月曜日
![Page 118: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/118.jpg)
CBPeripheral CBPeripheralDelegate
readValueForCharacteristicdidUpdateValueForCharacteristics
setNotifyValue:forCharacteristic
didUpdateValueForCharacteristics
13年3月25日月曜日
![Page 119: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/119.jpg)
• Scan and Connect
• Reconnecting using UUID
• Already connected?
•
13年3月25日月曜日
![Page 120: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/120.jpg)
discover and connect
CBCentralManager Periphearl device
retrievePeripherals
didRetrievePeripherals
connectPeripheral
didConnectPeripheral
13年3月25日月曜日
![Page 121: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/121.jpg)
iOS background
• Background app
• Scan and advertise
• Connect
• Interact
• “Start and stop”concept
• Info.plist
13年3月25日月曜日
![Page 122: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/122.jpg)
• Foreground app
• High-intensity scanning
• Bacground app/screen off
• Low-intensity scanning
•
13年3月25日月曜日
![Page 123: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/123.jpg)
• Ad
• Name
• Tx Power
• Service UUID
• Service Data
• Max: 31 bytes
13年3月25日月曜日
![Page 124: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/124.jpg)
• 16-bit UUID
• assigned by Bluetooth SIG
• http://develper/bluetooth.org
• 128-bit UUID
• assigned by you!
• コマンドライン uuidgen
13年3月25日月曜日
![Page 125: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/125.jpg)
• Centralがキャッシュするもの
• 発見したServiceとCharacteristics
• Valueはキャッシュされない
•
13年3月25日月曜日
![Page 126: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/126.jpg)
ペアリング• ペアリング
• 接続する、認証されてないー>拒否
• Insufficient Authentication
• ペアリング、暗号化
• AES-128、公開鍵
13年3月25日月曜日
![Page 127: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/127.jpg)
BTLE available?
• CBCentralManager
• delegate CBCentralManagerDelegate
• state CBCentralManagerState
• unknown -> 遷移
• 使う前にチェック
13年3月25日月曜日
![Page 128: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/128.jpg)
13年3月25日月曜日
![Page 129: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/129.jpg)
Bluetooth Smart ready と ready
• 2種類の周辺機器• デュアル・モード = クラシックBT + LE 両方搭載
• シングル・モード = LEのみ
• 接続問題• シングル・モード デバイスはクラシックBTとつながらない
• SMART READY (デュアル) / SMART (シングル) ロゴ
http://www.bluetooth.com/Pages/Smart-Logos.aspx13年3月25日月曜日
![Page 130: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/130.jpg)
ちょっと規格の話
13年3月25日月曜日
![Page 131: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/131.jpg)
13年3月25日月曜日
![Page 132: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/132.jpg)
13年3月25日月曜日
![Page 133: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/133.jpg)
13年3月25日月曜日
![Page 134: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/134.jpg)
13年3月25日月曜日
![Page 135: iOSアプリ開発者のための Bluetooth Low Energy体験講座](https://reader033.vdocuments.net/reader033/viewer/2022061613/555ee905d8b42ab6408b5694/html5/thumbnails/135.jpg)
13年3月25日月曜日