demo下載 http://download.csdn.net/detail/swibyn/9717588 直接看代碼 http://blog.csdn.net/swibyn/article/details/53785249 首先推薦去看官方文檔哦
現(xiàn)將創(chuàng)建藍(lán)牙工程的要點(diǎn)總結(jié)一下,,由于工程主要涉及中心模式,所以只總結(jié)中心模式的用法 1,引入CoreBluetooth.framework 2,實(shí)現(xiàn)藍(lán)牙協(xié)議,,如: .h文件如下 @protocolCBCentralManagerDelegate; @protocolCBPeripheralDelegate;
@interface ViewController :UIViewController <CBCentralManagerDelegate,CBPeripheralDelegate>
.m文件如下 #import "CoreBluetooth/CoreBluetooth.h" 另外還有代理部分請(qǐng)自行添加
3,,下面是使藍(lán)牙動(dòng)起來(lái)的過(guò)程 3.1創(chuàng)建CBCentralManager實(shí)例 self.cbCentralMgr = [[CBCentralManager alloc] initWithDelegate:self queue:nil]; 設(shè)置代理,,比如: self.cbCentralMgr.delegate =self;
創(chuàng)建數(shù)組管理外設(shè) self.peripheralArray = [NSMutableArrayarray];
3.2掃描周圍的藍(lán)牙 實(shí)際上周圍的藍(lán)牙如果可被發(fā)現(xiàn),則會(huì)一直往外發(fā)送廣告消息,,中心設(shè)備就是通過(guò)接收這些消息來(lái)發(fā)現(xiàn)周圍的藍(lán)牙的
[self.cbCentralMgr scanForPeripheralsWithServices:nil options:nil];
3.3發(fā)現(xiàn)一個(gè)藍(lán)牙設(shè)備 也就是收到了一個(gè)周圍的藍(lán)牙發(fā)來(lái)的廣告信息,,這是CBCentralManager會(huì)通知代理來(lái)處理 - (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {
} 如果周圍的藍(lán)牙有多個(gè),則這個(gè)方法會(huì)被調(diào)用多次,,你可以通過(guò)tableView或其他的控件把這些周圍的藍(lán)牙的信息打印出來(lái) 3.4連接一個(gè)藍(lán)牙 [self.cbCentralMgrconnectPeripheral:peripheral options:nil]; 一個(gè)中心設(shè)備可以同時(shí)連接多個(gè)周圍的藍(lán)牙設(shè)備 當(dāng)連接上某個(gè)藍(lán)牙之后,,CBCentralManager會(huì)通知代理處理
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral { }
因?yàn)樵诤竺嫖覀円獜耐庠O(shè)藍(lán)牙那邊再獲取一些信息,并與之通訊,,這些過(guò)程會(huì)有一些事件可能要處理,,所以要給這個(gè)外設(shè)設(shè)置代理,比如: peripheral.delegate =self; 3.5查詢藍(lán)牙服務(wù) [peripheral discoverServices:nil]; 返回的藍(lán)牙服務(wù)通知通過(guò)代理實(shí)現(xiàn) - (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error {
for (CBService* service in peripheral.services){
} } 3.6查詢服務(wù)所帶的特征值 [peripheral discoverCharacteristics:nil forService:service]; 返回的藍(lán)牙特征值通知通過(guò)代理實(shí)現(xiàn) - (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error {
for (CBCharacteristic * characteristic in service.characteristics) { } } 3.7給藍(lán)牙發(fā)數(shù)據(jù) [peripheral writeValue:data forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse]; 這時(shí)還會(huì)觸發(fā)一個(gè)代理事件 - (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error { } 3.8處理藍(lán)牙發(fā)過(guò)來(lái)的數(shù)據(jù) 一種方法是主動(dòng)讀取數(shù)據(jù),,不過(guò)更好的辦法是設(shè)置事件通知。 [peripheral setNotifyValue:YES forCharacteristic:characteristic]; 這樣當(dāng)有數(shù)據(jù)時(shí)會(huì)自動(dòng)觸發(fā)代理事件 - (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error { }
3.9 retrievePeripheralsWithIdentifiers使用例子 -(IBAction) Retrieve:(id)Sender { [self.tvLogsetText:@""]; NSMutableArray * Identifiers = [NSMutableArrayarray]; for (CBPeripheral * peripheralinself.peripheralArray) { [IdentifiersaddObject:peripheral.identifier]; }
[selfaddLog:@"[self.cbCentralMgr retrievePeripheralsWithIdentifiers:self.PeripheralIdentifiers]"]; self.retrievePeripherals = [self.cbCentralMgrretrievePeripheralsWithIdentifiers:Identifiers]; for (CBPeripheral* peripheralinself.retrievePeripherals) { [selfaddLog:[NSStringstringWithFormat:@"%@ name:%@",peripheral,peripheral.name]]; } [self.tableViewPeripheralreloadData]; }
3.10 retrieveConnectedPeripheralsWithServices使用例子
-(IBAction) Retrieve:(id)Sender { [self.tvLogsetText:@""]; NSMutableArray * services = [NSMutableArrayarray]; for (CBPeripheral * peripheralinself.peripheralArray) { if (peripheral.isConnected) { for (CBService *servicein peripheral.services) { [servicesaddObject:service.UUID]; } } }
[selfaddLog:@"[self.cbCentralMgr retrieveConnectedPeripheralsWithServices:peripheral.services]"]; self.retrievePeripherals = [self.cbCentralMgrretrieveConnectedPeripheralsWithServices:services]; for (CBPeripheral* peripheralinself.retrievePeripherals) { [selfaddLog:[NSStringstringWithFormat:@"%@ name:%@",peripheral,peripheral.name]]; } [self.tableViewPeripheralreloadData]; }
大概就這個(gè)個(gè)流程,例子中的參數(shù)設(shè)置,,及其其他的一些代理請(qǐng)自己研究,。
|
|
來(lái)自: 睜開(kāi)眼就變帥 > 《待分類》