IOS 有關藍芽的開發

2021-06-20 19:19:14 字數 3974 閱讀 9073

首先推薦去看官方文件哦

最近做一專案,本來藍芽通訊這塊不是我負責的,但是負責這塊的同事要走,只好咬咬牙學習了。嗚嗚嗚。。。。。

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

1,引入corebluetooth.framework

2,實現藍芽協議,如:

.h檔案如下

@protocol

cbcentralmanagerdelegate;

@protocol

cbperipheraldelegate;

@inte***ce

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:

niloptions: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

}3.6查詢服務所帶的特徵值

[peripheral discovercharacteristics:

nilforservice:service];

返回的藍芽特徵值通知通過**實現

- (void)peripheral:(cbperipheral *)peripheral diddiscovercharacteristicsforservice:(cbservice *)service error:(nserror *)error

}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

addlog

:@"[self.cbcentralmgr retrieveperipheralswithidentifiers:self.peripheralidentifiers]"];

self

.retrieveperipherals

= [self

.cbcentralmgr

retrieveperipheralswithidentifiers:identifiers];

for (cbperipheral* peripheral in

self.retrieveperipherals)

[self

.tableviewperipheral

reloaddata];

}3.10 retrieveconnectedperipheralswithservices 使用例子

-(ibaction) retrieve:(id)sender}}

[self

addlog

:@"[self.cbcentralmgr retrieveconnectedperipheralswithservices:peripheral.services]"];

self

.retrieveperipherals

= [self

.cbcentralmgr

retrieveconnectedperipheralswithservices:services];

for (cbperipheral* peripheral in

self.retrieveperipherals)

[self

.tableviewperipheral

reloaddata];

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

iOS藍芽開發

直接看 首先推薦去看官方文件哦 現將建立藍芽工程的要點總結一下,由於工程主要涉及中心模式,所以只總結中心模式的用法 1,引入corebluetooth.framework 2,實現藍芽協議,如 h檔案如下 protocol cbcentralmanagerdelegate protocol cbpe...

iOS藍芽開發

藍芽開發的流程 建立中心管理 cbcentralmanager 掃瞄外部裝置 連線外部裝置 掃瞄服務和特徵 資料互動 斷開連線 1 倒入 接下來的 如下 property nonatomic,strong cbperipheral peripheral 中心管家 property nonatomic...

iOS開發 GameKit藍芽開發

藍芽4.0是2012年最新藍芽版本,是3.0的公升級版本 較3.0版本更省電 成本低 3毫秒低延遲 超長有效連線距離 aes 128加密等 通常用在藍芽耳機 藍芽音箱等裝置上。藍芽技術聯盟 bluetooth sig 2010年7月7日宣布,正式採納藍芽4.0核心規範 bluetooth core ...