iOS 藍牙使用小結 bluetooth – iPhone手機開發技術文章 iPhone軟體開發教學課程

首先推薦去看官方文檔哦

最近做一項目,本來藍牙通訊這塊不是我負責的,但是負責這塊的同事要走,隻好咬咬牙學習瞭。嗚嗚嗚。。。。。

現將創建藍牙工程的要點總結一下,由於工程主要涉及中心模式,所以隻總結中心模式的用法

1,引入CoreBluetooth.framework

2,實現藍牙協議,如:

.h文件如下

@protocol CBCentralManagerDelegate;

@protocol CBPeripheralDelegate;

@interface ViewController :
UIViewController

.m文件如下

#import “CoreBluetooth/CoreBluetooth.h”

另外還有代理部分請自行添加

3,下面是使藍牙動起來的過程

3.1創建CBCentralManager實例

self.cbCentralMgr = [[CBCentralManager
alloc] initWithDelegate:self
queue:nil];

設置代理,比如:

self.cbCentralMgr.delegate =
self;

創建數組管理外設

self.peripheralArray = [NSMutableArray
array];

3.2掃描周圍的藍牙

實際上周圍的藍牙如果可被發現,則會一直往外發送廣告消息,中心設備就是通過接收這些消息來發現周圍的藍牙的

NSDictionary * dic = [NSDictionary
dictionaryWithObjectsAndKeys:[NSNumber
numberWithBool:false],CBCentralManagerScanOptionAllowDuplicatesKey,
nil];

[self.cbCentralMgr
scanForPeripheralsWithServices:nil
options:dic];

3.3發現一個藍牙設備

也就是收到瞭一個周圍的藍牙發來的廣告信息,這是CBCentralManager會通知代理來處理

– (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral
*)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI

{

}

如果周圍的藍牙有多個,則這個方法會被調用多次,你可以通過tableView或其他的控件把這些周圍的藍牙的信息打印出來

3.4連接一個藍牙

[self.cbCentralMgr
connectPeripheral:peripheral
options:[NSDictionary
dictionaryWithObject:[NSNumber
numberWithBool:YES]
forKey:CBConnectPeripheralOptionNotifyOnDisconnectionKey]];

一個中心設備可以同時連接多個周圍的藍牙設備

當連接上某個藍牙之後,CBCentralManager會通知代理處理

– (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral
*)peripheral

{

}

因為在後面我們要從外設藍牙那邊再獲取一些信息,並與之通訊,這些過程會有一些事件可能要處理,所以要給這個外設設置代理,比如:

peripheral.delegate =
self;

3.5查詢藍牙服務

[peripheral discoverServices:nil];

返回的藍牙服務通知通過代理實現

– (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError
*)error

{

for (CBService* service
in peripheral.services){

}

}

3.6查詢服務所帶的特征值

[peripheral discoverCharacteristics:nil
forService:service];

返回的藍牙特征值通知通過代理實現

– (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService
*)service error:(NSError *)error

{

for (CBCharacteristic * characteristic
in service.characteristics) {

}

}

3.7給藍牙發數據

[peripheral writeValue:data
forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse];

這時還會觸發一個代理事件

– (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic
*)characteristic error:(NSError *)error

{

}

3.8處理藍牙發過來的數據

– (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic
*)characteristic error:(NSError *)error

{

}

3.9 retrievePeripheralsWithIdentifiers
使用例子

-(IBAction) Retrieve:(id)Sender

{

[self.tvLog
setText:@””];

NSMutableArray * Identifiers = [NSMutableArray
array];

for (CBPeripheral * peripheral
in self.peripheralArray) {

[Identifiers
addObject:peripheral.identifier];

}

[self
addLog:@”[self.cbCentralMgr retrievePeripheralsWithIdentifiers:self.PeripheralIdentifiers]”];

self.retrievePeripherals = [self.cbCentralMgr
retrievePeripheralsWithIdentifiers:Identifiers];

for (CBPeripheral* peripheral
in self.retrievePeripherals) {

[self
addLog:[NSString
stringWithFormat:
@”%@ name:%@”,peripheral,peripheral.name]];

}

[self.tableViewPeripheral
reloadData];

}

3.10 retrieveConnectedPeripheralsWithServices
使用例子

-(IBAction) Retrieve:(id)Sender

{

[self.tvLog
setText:@””];

NSMutableArray * services = [NSMutableArray
array];

for (CBPeripheral * peripheral
in self.peripheralArray) {

if (peripheral.isConnected) {

for (CBService *service
in peripheral.services) {

[services
addObject:service.UUID];

}

}

}

[self
addLog:@”[self.cbCentralMgr retrieveConnectedPeripheralsWithServices:peripheral.services]”];

self.retrievePeripherals = [self.cbCentralMgr
retrieveConnectedPeripheralsWithServices:services];

for (CBPeripheral* peripheral
in self.retrievePeripherals) {

[self
addLog:[NSString
stringWithFormat:
@”%@ name:%@”,peripheral,peripheral.name]];

}

[self.tableViewPeripheral
reloadData];

}

大概就這個個流程,例子中的參數設置,及其其他的一些代理請自己研究,因為我也是剛入門

例子在此,需要的請下載參考。

發佈留言