iOS藍牙開發

相比于網絡請求,藍牙是一種低功耗(low-energy)的通信手段。目前iOS的開發都是針對CoreBluetooth 4.0版本。

iOS的藍牙開發,是基于Core Bluetooth framework。

Do not subclass any of the classes of the Core Bluetooth framework. Overriding these classes is unsupported and results in undefined behavior.

大致的意思不要嘗試繼承Core Bluetooth framework中任何相關的類,因為重載將會導致未知的結果。

Core Bluetooth framework中幾個主要的類的相關概念如下:

1、中心設備:CBCentral? ?外圍設備管理類:?CBPeripheralManager?外圍設備的服務?CBMutableService?外圍設備的特征?CBMutableCharacteristic 作為外圍設備服務特征的唯一標志:CBUUID

2、外圍設備:CBPeripheral ?中心設備管理類:?CBCentralManager ?外圍設備的服務 :CBService ?外圍設備的特征:?CBCharacteristic ??讀寫中心設備數據的請求:CBATTRequest

上面兩種區別,是以不同的角色開發,1是以外圍角色當作開發,遠端設備是中心角色;2是以中心角色作為開發,遠端為外圍角色。如下圖:

開發模式

注意:iOS10以后,所有的藍牙開發都需要向用戶申請藍牙權限,所以需要在項目的info.plist設置NSBluetoothPeripheralUsageDescription.

一、以中心設備CBCentral作為本地,外圍設備CBPeripheral作為遠端的開發模式方案:

1、首先我們需要初始化一個CBCentralManager的實例對象,是用來掃描發現外圍設備的服務跟特征:

NSDictionary *options = @{CBCentralManagerOptionShowPowerAlertKey: @YES};

CBCentralManager* tM = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:options];

self.myCentralManager = tM;

2、其次開始掃描外圍設備:

[self.myCentralManager scanForPeripheralsWithServices:nil options:nil];

如果沒有指定掃描哪些外圍設備,services可以傳nil,option也可以為nil。

3、掃面的結果會通過CBCentralManagerDelegate回調:

///@required 藍牙狀態變化監聽

- (void)centralManagerDidUpdateState:(CBCentralManager*)central;

///@optional發現外圍設備,雙方可以約定外圍設備唯一參數,一般是外圍設備的MAC地址

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

4、發現設備后,開始連接設備:

[self.myCentralManager connectPeripheral:self.peripheral options:nil];

5、連接結果也是通過CBCentralManagerDelegate回調:

///連接成功

- (void)centralManager:(CBCentralManager*)central didConnectPeripheral:(CBPeripheral*)peripheral;

///連接失敗

- (void)centralManager:(CBCentralManager*)central didFailToConnectPeripheral:(CBPeripheral*)peripheral error:(nullableNSError*)error;

///斷開連接結果

- (void)centralManager:(CBCentralManager*)central didDisconnectPeripheral:(CBPeripheral*)peripheral error:(nullableNSError*)error;

6、連接成功后,掃描外圍設備的服務CBService:

///UUID雙方約定好的

[self.myPeripheral discoverServices:@[[CBUUID UUIDWithString:KServiceUUID]]];

7、掃描服務結果會通過CBPeripheralDelegate回調:

///如果失敗,error不為空,否則為nil,比較雙方約定的UUID

- (void)peripheral:(CBPeripheral*)peripheral didDiscoverServices:(nullableNSError*)error;

8、發現CBService后,查詢服務的特征CBCharacteristic:

[self.myPeripheral discoverCharacteristics:nil forService:service];

9、查詢特征結果會通過CBPeripheralDelegate回調:

///如果失敗,error不為空,否則為nil,比較雙方約定的特征UUID

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

10、發現服務特征后,注冊特征通知,這樣中心設備就可以讀取外圍發過來的數據了:

[self.myPeripheral setNotifyValue:YES forCharacteristic:characteristic];

11、注冊通知的結果通過CBPeripheralDelegate回調:

///如果失敗,error不為空,否則為nil

-(void)peripheral:(CBPeripheral*)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic*)characteristic error:(NSError*)error;

12、特征的數據也是通過CBPeripheralDelegate回調:

///如果失敗,error不為空,否則為nil。數據在characteristic的value中

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

13、如果給對應的特征發送數據,則調用:

///type有兩個值,一個是不用返回發送的結果,一個需要返回發送數據的結果,默認是返回CBCharacteristicWriteWithResponse =0,CBCharacteristicWriteWithoutResponse,

[self.myPeripheral writeValue:sendData forCharacteristic:_mWriteCharacteristic type:CBCharacteristicWriteWithResponse];

14、如果需要返回結果,發送數據結果也是通過CBPeripheralDelegate回調:

///失敗error不為nil

- (void)peripheral:(CBPeripheral*)peripheral didWriteValueForCharacteristic:(CBCharacteristic*)characteristic error:(nullableNSError*)error;

第二種方案(以外圍設備當作本地,中心當作遠端)未完待續,有時間繼續更新。。。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。