Enviar pesquisa
Carregar
Ios corebluetooth beginner
•
1 gostou
•
2,870 visualizações
S
shinsuke79
Seguir
Software
Tecnologia
Negócios
Denunciar
Compartilhar
Denunciar
Compartilhar
1 de 33
Baixar agora
Baixar para ler offline
Recomendados
リソースフレームワークBEARのススメ(PHP勉強会#51)
リソースフレームワークBEARのススメ(PHP勉強会#51)
stellaqua
ソーシャルアプリ勉強会(第一回資料)配布用
ソーシャルアプリ勉強会(第一回資料)配布用
Yatabe Terumasa
Zend Frameworkで始める携帯サイト
Zend Frameworkで始める携帯サイト
清水樹
[Alfresco]ドキュメントライブラリのUIカスタマイズ
[Alfresco]ドキュメントライブラリのUIカスタマイズ
Jun Terashita
自作node.jsフレームワークとnginxを使ってラジオサイトを作ってみた
自作node.jsフレームワークとnginxを使ってラジオサイトを作ってみた
Yuki Takei
La ilusión de la felicidad
La ilusión de la felicidad
zanzado
Wolcome to swift
Wolcome to swift
Kyohei Ito
20160521 大規模映像配信サービスの Java8による全面リニューアルの裏側
20160521 大規模映像配信サービスの Java8による全面リニューアルの裏側
賢 秋穂
Recomendados
リソースフレームワークBEARのススメ(PHP勉強会#51)
リソースフレームワークBEARのススメ(PHP勉強会#51)
stellaqua
ソーシャルアプリ勉強会(第一回資料)配布用
ソーシャルアプリ勉強会(第一回資料)配布用
Yatabe Terumasa
Zend Frameworkで始める携帯サイト
Zend Frameworkで始める携帯サイト
清水樹
[Alfresco]ドキュメントライブラリのUIカスタマイズ
[Alfresco]ドキュメントライブラリのUIカスタマイズ
Jun Terashita
自作node.jsフレームワークとnginxを使ってラジオサイトを作ってみた
自作node.jsフレームワークとnginxを使ってラジオサイトを作ってみた
Yuki Takei
La ilusión de la felicidad
La ilusión de la felicidad
zanzado
Wolcome to swift
Wolcome to swift
Kyohei Ito
20160521 大規模映像配信サービスの Java8による全面リニューアルの裏側
20160521 大規模映像配信サービスの Java8による全面リニューアルの裏側
賢 秋穂
できる!サーバレスアーキテクチャ
できる!サーバレスアーキテクチャ
azuma satoshi
[db tech showcase Tokyo 2015] E26 Couchbaseの最新情報/JBoss Data Virtualizationで仮想...
[db tech showcase Tokyo 2015] E26 Couchbaseの最新情報/JBoss Data Virtualizationで仮想...
Masahiro Tomisugi
Automation with SoftLayer and Zabbix
Automation with SoftLayer and Zabbix
softlayerjp
【B-1】kintoneでお手軽コールセンター!
【B-1】kintoneでお手軽コールセンター!
Sakae Saito
iAP&iAC 2014 Summer
iAP&iAC 2014 Summer
NTTDATA INTRAMART
Deep-Dive into Scriptable Build Pipeline
Deep-Dive into Scriptable Build Pipeline
Haruto Otake
Building React, Flutter and Blazor development and debugging environment with...
Building React, Flutter and Blazor development and debugging environment with...
Shotaro Suzuki
zozotown real time linkage infrastructure
zozotown real time linkage infrastructure
KeisukeTaniguchi2
5分でわかったつもりになるParse.com
5分でわかったつもりになるParse.com
Kenta Tsuji
脆弱性事例に学ぶセキュアコーディング「SSL/TLS証明書検証」編 (KOF2014)
脆弱性事例に学ぶセキュアコーディング「SSL/TLS証明書検証」編 (KOF2014)
JPCERT Coordination Center
Ansible とネットワーク自動化の概要(SmartCS と Ansible の連携による自動化の可能性を体験!)
Ansible とネットワーク自動化の概要(SmartCS と Ansible の連携による自動化の可能性を体験!)
akira6592
HTML5最新動向
HTML5最新動向
Shumpei Shiraishi
エンジニアのための Azure 基礎知識
エンジニアのための Azure 基礎知識
Daiyu Hatakeyama
OpenStack環境構築入門 Havana対応版 - OpenStack最新情報セミナー2014年4月
OpenStack環境構築入門 Havana対応版 - OpenStack最新情報セミナー2014年4月
VirtualTech Japan Inc.
モバイル開発@symfony
モバイル開発@symfony
Daichi Kamemoto
EMS 勉強会 第1回 Autopilot 祭り - Autopilot 最新情報
EMS 勉強会 第1回 Autopilot 祭り - Autopilot 最新情報
Dai Matsui
Data APIの基本
Data APIの基本
Hajime Fujimoto
DevOps on azure 高品質クラウドデザインを求めて
DevOps on azure 高品質クラウドデザインを求めて
Atsushi Kojima
富士市役所のデスクトップ仮想化 2015年4月版 ~ゼロクライアントとICカード認証の導入~
富士市役所のデスクトップ仮想化 2015年4月版 ~ゼロクライアントとICカード認証の導入~
Yasunobu Fukasawa
初めての Data API CMS どうでしょう - 仙台編 -
初めての Data API CMS どうでしょう - 仙台編 -
Yuji Takayama
Mais conteúdo relacionado
Semelhante a Ios corebluetooth beginner
できる!サーバレスアーキテクチャ
できる!サーバレスアーキテクチャ
azuma satoshi
[db tech showcase Tokyo 2015] E26 Couchbaseの最新情報/JBoss Data Virtualizationで仮想...
[db tech showcase Tokyo 2015] E26 Couchbaseの最新情報/JBoss Data Virtualizationで仮想...
Masahiro Tomisugi
Automation with SoftLayer and Zabbix
Automation with SoftLayer and Zabbix
softlayerjp
【B-1】kintoneでお手軽コールセンター!
【B-1】kintoneでお手軽コールセンター!
Sakae Saito
iAP&iAC 2014 Summer
iAP&iAC 2014 Summer
NTTDATA INTRAMART
Deep-Dive into Scriptable Build Pipeline
Deep-Dive into Scriptable Build Pipeline
Haruto Otake
Building React, Flutter and Blazor development and debugging environment with...
Building React, Flutter and Blazor development and debugging environment with...
Shotaro Suzuki
zozotown real time linkage infrastructure
zozotown real time linkage infrastructure
KeisukeTaniguchi2
5分でわかったつもりになるParse.com
5分でわかったつもりになるParse.com
Kenta Tsuji
脆弱性事例に学ぶセキュアコーディング「SSL/TLS証明書検証」編 (KOF2014)
脆弱性事例に学ぶセキュアコーディング「SSL/TLS証明書検証」編 (KOF2014)
JPCERT Coordination Center
Ansible とネットワーク自動化の概要(SmartCS と Ansible の連携による自動化の可能性を体験!)
Ansible とネットワーク自動化の概要(SmartCS と Ansible の連携による自動化の可能性を体験!)
akira6592
HTML5最新動向
HTML5最新動向
Shumpei Shiraishi
エンジニアのための Azure 基礎知識
エンジニアのための Azure 基礎知識
Daiyu Hatakeyama
OpenStack環境構築入門 Havana対応版 - OpenStack最新情報セミナー2014年4月
OpenStack環境構築入門 Havana対応版 - OpenStack最新情報セミナー2014年4月
VirtualTech Japan Inc.
モバイル開発@symfony
モバイル開発@symfony
Daichi Kamemoto
EMS 勉強会 第1回 Autopilot 祭り - Autopilot 最新情報
EMS 勉強会 第1回 Autopilot 祭り - Autopilot 最新情報
Dai Matsui
Data APIの基本
Data APIの基本
Hajime Fujimoto
DevOps on azure 高品質クラウドデザインを求めて
DevOps on azure 高品質クラウドデザインを求めて
Atsushi Kojima
富士市役所のデスクトップ仮想化 2015年4月版 ~ゼロクライアントとICカード認証の導入~
富士市役所のデスクトップ仮想化 2015年4月版 ~ゼロクライアントとICカード認証の導入~
Yasunobu Fukasawa
初めての Data API CMS どうでしょう - 仙台編 -
初めての Data API CMS どうでしょう - 仙台編 -
Yuji Takayama
Semelhante a Ios corebluetooth beginner
(20)
できる!サーバレスアーキテクチャ
できる!サーバレスアーキテクチャ
[db tech showcase Tokyo 2015] E26 Couchbaseの最新情報/JBoss Data Virtualizationで仮想...
[db tech showcase Tokyo 2015] E26 Couchbaseの最新情報/JBoss Data Virtualizationで仮想...
Automation with SoftLayer and Zabbix
Automation with SoftLayer and Zabbix
【B-1】kintoneでお手軽コールセンター!
【B-1】kintoneでお手軽コールセンター!
iAP&iAC 2014 Summer
iAP&iAC 2014 Summer
Deep-Dive into Scriptable Build Pipeline
Deep-Dive into Scriptable Build Pipeline
Building React, Flutter and Blazor development and debugging environment with...
Building React, Flutter and Blazor development and debugging environment with...
zozotown real time linkage infrastructure
zozotown real time linkage infrastructure
5分でわかったつもりになるParse.com
5分でわかったつもりになるParse.com
脆弱性事例に学ぶセキュアコーディング「SSL/TLS証明書検証」編 (KOF2014)
脆弱性事例に学ぶセキュアコーディング「SSL/TLS証明書検証」編 (KOF2014)
Ansible とネットワーク自動化の概要(SmartCS と Ansible の連携による自動化の可能性を体験!)
Ansible とネットワーク自動化の概要(SmartCS と Ansible の連携による自動化の可能性を体験!)
HTML5最新動向
HTML5最新動向
エンジニアのための Azure 基礎知識
エンジニアのための Azure 基礎知識
OpenStack環境構築入門 Havana対応版 - OpenStack最新情報セミナー2014年4月
OpenStack環境構築入門 Havana対応版 - OpenStack最新情報セミナー2014年4月
モバイル開発@symfony
モバイル開発@symfony
EMS 勉強会 第1回 Autopilot 祭り - Autopilot 最新情報
EMS 勉強会 第1回 Autopilot 祭り - Autopilot 最新情報
Data APIの基本
Data APIの基本
DevOps on azure 高品質クラウドデザインを求めて
DevOps on azure 高品質クラウドデザインを求めて
富士市役所のデスクトップ仮想化 2015年4月版 ~ゼロクライアントとICカード認証の導入~
富士市役所のデスクトップ仮想化 2015年4月版 ~ゼロクライアントとICカード認証の導入~
初めての Data API CMS どうでしょう - 仙台編 -
初めての Data API CMS どうでしょう - 仙台編 -
Ios corebluetooth beginner
1.
iOS CoreBluetooth 入門 ! @79SHINSUKE 西川慎介 ヱビス株式会社 webisu-inc.co.jp 2014-03-20
2.
・BLEの通信規格 デバイスの種類ごとにある ! ・手順書 いつ、どんなデータ ! ・たくさんある プロファイル
3.
・Core Bluetoothが対応しているプロファイル ! ・サービス(BLEデバイスの機能) ! ・キャラクテリスティック(特性) ! ・GATTベースのプロファイルもたくさんある ! GATT(Generic Attribute
Profile)
4.
・BLEデバイスを見つける ・BLEデバイスに接続する ・BLEデバイスに対するRead/Write ・通信するデータはバイトデータなので何でもOK.動的デー タも。 ・ペアリングすると暗号化される ・バックグラウンドでも動く Core Bluetoothフレームワーク
5.
バックグラウンド ・Info.listで宣言 UIBackgroundModes:bluetooth-central UIBackgroundModes:bluetooth-peripheral ! ・デバイスの検知 (セントラル) ・アドバタイズ発信 (ペリフェラル) UUIDが送信されないので、セントラルがUUID で検索している場合は見つけられない ! ! !
6.
・1つ以上のサービスを提供する ! ・アドバタイズ ! ・アドバタイジング・データ ! ・たとえば、iBeaconで利用されるBeacon ペリフェラル(Peripheral)
7.
ペリフェラルのデータ構造 ! ! CBMutableService CBPeripheralManager CBMutableService CBMutableCharacteristic CBMutableCharacteristic CBMutableCharacteristic CBMutableCharacteristic CBPeripheralManager *_peripheralManager; CBMutableService *_service; CBMutableCharacteristic
*_characteristic;
8.
・アドバタイズしているペリフェラルを見つける ! ・アドバタイズしているペリフェラルに接続する ! ! ・データ通信(Read/Write) セントラル(Central)
9.
iOSデバイス間で セントラル-ペリフェラル に通信してみました
10.
ペリフェラルのコード
11.
#import <CoreBluetooth/CoreBluetooth.h> ! @interface ViewController
: UIViewController<CBPeripheralManagerDelegate> @end ! @implementation ViewController { CBPeripheralManager *_peripheralManager; CBMutableService *_service; CBMutableCharacteristic *_characteristic; NSData *_value; } ! ! ! - (void)viewDidLoad { [super viewDidLoad]; ! // データ _value = [@"Hello World" dataUsingEncoding:NSUTF8StringEncoding]; // ペリフェラルマネージャをつくる _peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self queue:nil]; } ! ! ! - (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral { if (peripheral.state == CBPeripheralManagerStatePoweredOn) { [self startService]; } }
12.
#import <CoreBluetooth/CoreBluetooth.h> ! @interface ViewController
: UIViewController<CBPeripheralManagerDelegate> @end ! @implementation ViewController { CBPeripheralManager *_peripheralManager; CBMutableService *_service; CBMutableCharacteristic *_characteristic; NSData *_value; } ! ! ! - (void)viewDidLoad { [super viewDidLoad]; ! // データ _value = [@"Hello World" dataUsingEncoding:NSUTF8StringEncoding]; // ペリフェラルマネージャをつくる _peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self queue:nil]; } ! ! ! - (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral { if (peripheral.state == CBPeripheralManagerStatePoweredOn) { [self startService]; } }
13.
#import <CoreBluetooth/CoreBluetooth.h> ! @interface ViewController
: UIViewController<CBPeripheralManagerDelegate> @end ! @implementation ViewController { CBPeripheralManager *_peripheralManager; CBMutableService *_service; CBMutableCharacteristic *_characteristic; NSData *_value; } ! ! ! - (void)viewDidLoad { [super viewDidLoad]; ! // データ _value = [@"Hello World" dataUsingEncoding:NSUTF8StringEncoding]; // ペリフェラルマネージャをつくる _peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self queue:nil]; } ! ! ! - (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral { if (peripheral.state == CBPeripheralManagerStatePoweredOn) { [self startService]; } }
14.
#import <CoreBluetooth/CoreBluetooth.h> ! @interface ViewController
: UIViewController<CBPeripheralManagerDelegate> @end ! @implementation ViewController { CBPeripheralManager *_peripheralManager; CBMutableService *_service; CBMutableCharacteristic *_characteristic; NSData *_value; } ! ! ! - (void)viewDidLoad { [super viewDidLoad]; ! // データ _value = [@"Hello World" dataUsingEncoding:NSUTF8StringEncoding]; // ペリフェラルマネージャをつくる _peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self queue:nil]; } ! ! ! - (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral { if (peripheral.state == CBPeripheralManagerStatePoweredOn) { [self startService]; } }
15.
- (void)startService { NSLog(@"start service"); //
サービスをつくる _service = [[CBMutableService alloc] initWithType:[CBUUID UUIDWithString:@"74E493FC-B56E-471E-91E5-C63DC9CD4716"] primary:YES]; ! //キャラクテリスティックをつくる _characteristic = [[CBMutableCharacteristic alloc] initWithType:[CBUUID UUIDWithString:@"97D158FF-A5F4-4D13-A97F-86D460CF1FD2"] properties:(CBCharacteristicPropertyRead|CBCharacteristicPropertyWrite| CBCharacteristicPropertyNotify) value:nil //動的な値をわたすときはnil permissions:(CBAttributePermissionsReadable|CBAttributePermissionsWriteable)]; ! // サービスにキャラクテリスティックをセット _service.characteristics = @[_characteristic]; // マネージャにサービスを登録 [_peripheralManager addService:_service]; // サービスをアドバタイズする [_peripheralManager startAdvertising:@{ CBAdvertisementDataLocalNameKey: @"iBLE", CBAdvertisementDataServiceUUIDsKey:@[_service.UUID] }]; }
16.
- (void)startService { NSLog(@"start service"); //
サービスをつくる _service = [[CBMutableService alloc] initWithType:[CBUUID UUIDWithString:@"74E493FC-B56E-471E-91E5-C63DC9CD4716"] primary:YES]; ! //キャラクテリスティックをつくる _characteristic = [[CBMutableCharacteristic alloc] initWithType:[CBUUID UUIDWithString:@"97D158FF-A5F4-4D13-A97F-86D460CF1FD2"] properties:(CBCharacteristicPropertyRead|CBCharacteristicPropertyWrite| CBCharacteristicPropertyNotify) value:nil //動的な値をわたすときはnil permissions:(CBAttributePermissionsReadable|CBAttributePermissionsWriteable)]; ! // サービスにキャラクテリスティックをセット _service.characteristics = @[_characteristic]; // マネージャにサービスを登録 [_peripheralManager addService:_service]; // サービスをアドバタイズする [_peripheralManager startAdvertising:@{ CBAdvertisementDataLocalNameKey: @"iBLE", CBAdvertisementDataServiceUUIDsKey:@[_service.UUID] }]; }
17.
- (void)startService { NSLog(@"start service"); //
サービスをつくる _service = [[CBMutableService alloc] initWithType:[CBUUID UUIDWithString:@"74E493FC-B56E-471E-91E5-C63DC9CD4716"] primary:YES]; ! //キャラクテリスティックをつくる _characteristic = [[CBMutableCharacteristic alloc] initWithType:[CBUUID UUIDWithString:@"97D158FF-A5F4-4D13-A97F-86D460CF1FD2"] properties:(CBCharacteristicPropertyRead|CBCharacteristicPropertyWrite| CBCharacteristicPropertyNotify) value:nil //動的な値をわたすときはnil permissions:(CBAttributePermissionsReadable|CBAttributePermissionsWriteable)]; ! // サービスにキャラクテリスティックをセット _service.characteristics = @[_characteristic]; // マネージャにサービスを登録 [_peripheralManager addService:_service]; // サービスをアドバタイズする [_peripheralManager startAdvertising:@{ CBAdvertisementDataLocalNameKey: @"iBLE", CBAdvertisementDataServiceUUIDsKey:@[_service.UUID] }]; }
18.
- (void)startService { NSLog(@"start service"); //
サービスをつくる _service = [[CBMutableService alloc] initWithType:[CBUUID UUIDWithString:@"74E493FC-B56E-471E-91E5-C63DC9CD4716"] primary:YES]; ! //キャラクテリスティックをつくる _characteristic = [[CBMutableCharacteristic alloc] initWithType:[CBUUID UUIDWithString:@"97D158FF-A5F4-4D13-A97F-86D460CF1FD2"] properties:(CBCharacteristicPropertyRead|CBCharacteristicPropertyWrite| CBCharacteristicPropertyNotify) value:nil //動的な値をわたすときはnil permissions:(CBAttributePermissionsReadable|CBAttributePermissionsWriteable)]; ! // サービスにキャラクテリスティックをセット _service.characteristics = @[_characteristic]; // マネージャにサービスを登録 [_peripheralManager addService:_service]; // サービスをアドバタイズする [_peripheralManager startAdvertising:@{ CBAdvertisementDataLocalNameKey: @"iBLE", CBAdvertisementDataServiceUUIDsKey:@[_service.UUID] }]; }
19.
// サービスを追加 - (void)peripheralManager:(CBPeripheralManager
*)peripheral didAddService:(CBService *)service error:(NSError *)error ! { if (error) { // エラー処理 } } ! ! ! ! // Centralからの接続待ち状態 - (void)peripheralManagerDidStartAdvertising:(CBPeripheralManager *)peripheral error:(NSError *)error { if (error) { // エラー処理 } }
20.
// セントラルからの読み取り要求 -(void)peripheralManager:(CBPeripheralManager *)peripheral didReceiveReadRequest:(CBATTRequest
*)request { NSLog(@"didReceiveReadRequest: %@",request); request.value = _value; [_peripheralManager respondToRequest:request withResult:CBATTErrorSuccess]; }
21.
セントラルのコード
22.
#import <CoreBluetooth/CoreBluetooth.h> ! @interface ViewController
: UIViewController<CBCentralManagerDelegate,CBPeripheralManagerDelegate> @end ! @implementation ViewController { CBCentralManager *_centralManager; CBPeripheral *_peripheral; } ! ! ! - (void)viewDidLoad { [super viewDidLoad]; ! //セントラルマネージャを起動 _centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil]; } ! ! ! - (void)centralManagerDidUpdateState:(CBCentralManager *)central { ! if (central.state == CBCentralManagerStatePoweredOn) { //アドバタイズしているペリフェラルを検出する(探索対象のデバイスが持つサービスを指定) NSArray *services = [NSArray arrayWithObjects:[CBUUID UUIDWithString:kServiceUUID], nil]; // 単一デバイスの発見イベントを重複して発行させない NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:CBCentralManagerScanOptionAllowDuplicatesKey]; // デバイスの探索を開始 [_centralManager scanForPeripheralsWithServices:services options:options]; } }
23.
#import <CoreBluetooth/CoreBluetooth.h> ! @interface ViewController
: UIViewController<CBCentralManagerDelegate,CBPeripheralManagerDelegate> @end ! @implementation ViewController { CBCentralManager *_centralManager; CBPeripheral *_peripheral; } ! ! ! - (void)viewDidLoad { [super viewDidLoad]; ! //セントラルマネージャを起動 _centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil]; } ! ! ! - (void)centralManagerDidUpdateState:(CBCentralManager *)central { ! if (central.state == CBCentralManagerStatePoweredOn) { //アドバタイズしているペリフェラルを検出する(探索対象のデバイスが持つサービスを指定) NSArray *services = [NSArray arrayWithObjects:[CBUUID UUIDWithString:kServiceUUID], nil]; // 単一デバイスの発見イベントを重複して発行させない NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:CBCentralManagerScanOptionAllowDuplicatesKey]; // デバイスの探索を開始 [_centralManager scanForPeripheralsWithServices:services options:options]; } }
24.
#import <CoreBluetooth/CoreBluetooth.h> ! @interface ViewController
: UIViewController<CBCentralManagerDelegate,CBPeripheralManagerDelegate> @end ! @implementation ViewController { CBCentralManager *_centralManager; CBPeripheral *_peripheral; } ! ! ! - (void)viewDidLoad { [super viewDidLoad]; ! //セントラルマネージャを起動 _centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil]; } ! ! ! - (void)centralManagerDidUpdateState:(CBCentralManager *)central { ! if (central.state == CBCentralManagerStatePoweredOn) { //アドバタイズしているペリフェラルを検出する(探索対象のデバイスが持つサービスを指定) NSArray *services = [NSArray arrayWithObjects:[CBUUID UUIDWithString:kServiceUUID], nil]; // 単一デバイスの発見イベントを重複して発行させない NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:CBCentralManagerScanOptionAllowDuplicatesKey]; // デバイスの探索を開始 [_centralManager scanForPeripheralsWithServices:services options:options]; } }
25.
#import <CoreBluetooth/CoreBluetooth.h> ! @interface ViewController
: UIViewController<CBCentralManagerDelegate,CBPeripheralManagerDelegate> @end ! @implementation ViewController { CBCentralManager *_centralManager; CBPeripheral *_peripheral; } ! ! ! - (void)viewDidLoad { [super viewDidLoad]; ! //セントラルマネージャを起動 _centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil]; } ! ! ! - (void)centralManagerDidUpdateState:(CBCentralManager *)central { ! if (central.state == CBCentralManagerStatePoweredOn) { //アドバタイズしているペリフェラルを検出する(探索対象のデバイスが持つサービスを指定) NSArray *services = [NSArray arrayWithObjects:[CBUUID UUIDWithString:kServiceUUID], nil]; // 単一デバイスの発見イベントを重複して発行させない NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:CBCentralManagerScanOptionAllowDuplicatesKey]; // デバイスの探索を開始 [_centralManager scanForPeripheralsWithServices:services options:options]; } }
26.
#import <CoreBluetooth/CoreBluetooth.h> ! @interface ViewController
: UIViewController<CBCentralManagerDelegate,CBPeripheralManagerDelegate> @end ! @implementation ViewController { CBCentralManager *_centralManager; CBPeripheral *_peripheral; } ! ! ! - (void)viewDidLoad { [super viewDidLoad]; ! //セントラルマネージャを起動 _centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil]; } ! ! ! - (void)centralManagerDidUpdateState:(CBCentralManager *)central { ! if (central.state == CBCentralManagerStatePoweredOn) { //アドバタイズしているペリフェラルを検出する(探索対象のデバイスが持つサービスを指定) NSArray *services = [NSArray arrayWithObjects:[CBUUID UUIDWithString:kServiceUUID], nil]; // 単一デバイスの発見イベントを重複して発行させない NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:CBCentralManagerScanOptionAllowDuplicatesKey]; // デバイスの探索を開始 [_centralManager scanForPeripheralsWithServices:services options:options]; } }
27.
// ペリフェラルが見つかったら通知 - (void)centralManager:(CBCentralManager
*)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI { NSLog(@"Discoverd peripheral: %@", peripheral); // ペリフェラルに接続 _peripheral = peripheral; _peripheral.delegate = self; [_centralManager connectPeripheral:_peripheral options:nil]; } ! ! ! ! //ペリフェラルへの接続に失敗 - (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral: (CBPeripheral *)peripheral error:(NSError *)error { NSLog(@"Fail To Connetct Peripheral"); [self cleanup]; }
28.
//ペリフェラルへの接続完了 - (void)centralManager:(CBCentralManager *)central
didConnectPeripheral:(CBPeripheral *)peripheral { NSLog(@"Connetcted Peripheral"); // 見つかったので他のペリフェラルのスキャンを停止 [_centralManager stopScan]; _peripheral = peripheral; _peripheral.delegate = self; // サービス接続 [_peripheral discoverServices:@[[CBUUID UUIDWithString:kServiceUUID]]]; } ! - (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error { if (error) { [self cleanup]; return; } for (CBService *service in peripheral.services) { [peripheral discoverCharacteristics:@[[CBUUID UUIDWithString:kCharacteristicUUID]] forService:service]; } }
29.
//ペリフェラルへの接続完了 - (void)centralManager:(CBCentralManager *)central
didConnectPeripheral:(CBPeripheral *)peripheral { NSLog(@"Connetcted Peripheral"); // 見つかったので他のペリフェラルのスキャンを停止 [_centralManager stopScan]; _peripheral = peripheral; _peripheral.delegate = self; // サービス接続 [_peripheral discoverServices:@[[CBUUID UUIDWithString:kServiceUUID]]]; } ! - (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error { if (error) { [self cleanup]; return; } for (CBService *service in peripheral.services) { [peripheral discoverCharacteristics:@[[CBUUID UUIDWithString:kCharacteristicUUID]] forService:service]; } }
30.
// 指定されたサービスのキャラクタリスティックを検出 - (void)peripheral:(CBPeripheral
*)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error { if (error) { [self cleanup]; return; } for (CBCharacteristic *characteristic in service.characteristics) { if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:kCharacteristicUUID]]) { // 読み取り要求 [peripheral readValueForCharacteristic:characteristic]; } } } ! // 読み取り要求 - (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error { if (error) { NSLog(@"Error"); return; } NSString *value= [[NSString alloc] initWithData:characteristic.value encoding:NSUTF8StringEncoding]; NSLog(@"characteristic value is %@", value); }
31.
// 指定されたサービスのキャラクタリスティックを検出 - (void)peripheral:(CBPeripheral
*)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error { if (error) { [self cleanup]; return; } for (CBCharacteristic *characteristic in service.characteristics) { if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:kCharacteristicUUID]]) { // 読み取り要求 [peripheral readValueForCharacteristic:characteristic]; } } } ! // 読み取り要求 - (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error { if (error) { NSLog(@"Error"); return; } NSString *value= [[NSString alloc] initWithData:characteristic.value encoding:NSUTF8StringEncoding]; NSLog(@"characteristic value is %@", value); }
32.
・writeValue [_peripheral writeValue:dataToWrite forCharacteristic:_characteristic type:CBCharacteristicWriteWithResponse]; ! ! [_peripheral
writeValue:dataToWrite forCharacteristic:_characteristic type:CBCharacteristicWriteWithoutResponse]; ! ・notify [_peripheral setNotifyValue:YES forCharacteristic:_characteristic]; !
33.
ありがとうございました!
Baixar agora