iOS 藍(lán)牙

iOS 藍(lán)牙算是比較獨(dú)立的一門技術(shù)了但是在ios上面藍(lán)牙有很多限制個(gè)人感覺這事影響ios藍(lán)牙發(fā)展壯大的原因了。今天我門一般是從藍(lán)牙4.0開始學(xué)習(xí)。

話不多說直接進(jìn)入正題

首先導(dǎo)入 <CoreBluetooth/CoreBluetooth.h>

藍(lán)牙有一個(gè)中心管理者 CBCentralManager 與外設(shè) CBPeripheral


/** 中心管理者 */

@property (nonatomic, strong) CBCentralManager *cMgr;

/** 連接到的外設(shè) */

@property (nonatomic, strong) CBPeripheral *peripheral;

懶加載


- (CBCentralManager *)cMgr

{

if (!_cMgr) {

_cMgr = [[CBCentralManager alloc] initWithDelegate:self

queue:dispatch_get_main_queue() options:nil];

}

return _cMgr;

}

只要中心管理者初始化,就會觸發(fā)此代理方法


- (void)centralManagerDidUpdateState:(CBCentralManager *)central

{

switch (central.state) {

case CBCentralManagerStateUnknown:



break;

case CBCentralManagerStateResetting:



break;

case CBCentralManagerStateUnsupported:



break;

case CBCentralManagerStateUnauthorized:



break;

case CBCentralManagerStatePoweredOff:

break;

case CBCentralManagerStatePoweredOn:

{



// 在中心管理者成功開啟后再進(jìn)行一些操作

// 搜索外設(shè)

[self.cMgr scanForPeripheralsWithServices:nil // 通過某些服務(wù)篩選外設(shè)

options:nil]; 

}

break;

default:

break;

}

}

// 發(fā)現(xiàn)外設(shè)后調(diào)用的方法

- (void)centralManager:(CBCentralManager *)central // 中心管理者 didDiscoverPeripheral:(CBPeripheral *)peripheral // 外設(shè)    advertisementData:(NSDictionary *)advertisementData // 外設(shè)攜帶的數(shù)據(jù)                  RSSI:(NSNumber *)RSSI // 外設(shè)發(fā)出的藍(lán)牙信號強(qiáng)度{    

*/



//過濾

if ([peripheral.name hasPrefix:@"slap"] && (ABS(RSSI.integerValue) > 35)) {



self.peripheral = peripheral;

// 發(fā)現(xiàn)完之后就是進(jìn)行連接

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



}

}

// 中心管理者連接外設(shè)成功

- (void)centralManager:(CBCentralManager *)central // 中心管理者

didConnectPeripheral:(CBPeripheral *)peripheral // 外設(shè)

{

[self.peripheral discoverServices:nil];

}

// 外設(shè)連接失敗

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

{

}

// 丟失連接

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

{

}

#pragma mark - 外設(shè)代理

// 發(fā)現(xiàn)外設(shè)的服務(wù)后調(diào)用的方法

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

{

for (CBService *service in peripheral.services) {

// 發(fā)現(xiàn)服務(wù)后,讓設(shè)備再發(fā)現(xiàn)服務(wù)內(nèi)部的特征們 didDiscoverCharacteristicsForService

[peripheral discoverCharacteristics:nil forService:service];

}

}

// 發(fā)現(xiàn)外設(shè)服務(wù)里的特征的時(shí)候調(diào)用的代理方法

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

{

for (CBCharacteristic *cha in service.characteristics) {

//NSLog(@"%s, line = %d, char = %@", __FUNCTION__, __LINE__, cha);

// 獲取特征對應(yīng)的描述 didUpdateValueForDescriptor

[peripheral discoverDescriptorsForCharacteristic:cha];

// 獲取特征的值 didUpdateValueForCharacteristic

[peripheral readValueForCharacteristic:cha];

}

}

// 更新特征的value的時(shí)候會調(diào)用

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

{

for (CBDescriptor *descriptor in characteristic.descriptors) {

// 它會觸發(fā)

[peripheral readValueForDescriptor:descriptor];

}

}

// 更新特征的描述的值的時(shí)候會調(diào)用

- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForDescriptor:(CBDescriptor *)descriptor error:(NSError *)error

{

// 這里當(dāng)描述的值更新的時(shí)候,直接調(diào)用此方法即可

[peripheral readValueForDescriptor:descriptor];

}

// 發(fā)現(xiàn)外設(shè)的特征的描述數(shù)組

- (void)peripheral:(CBPeripheral *)peripheral didDiscoverDescriptorsForCharacteristic:(nonnull CBCharacteristic *)characteristic error:(nullable NSError *)error

{

// 在此處讀取描述即可

for (CBDescriptor *descriptor in characteristic.descriptors) {

// 它會觸發(fā)

[peripheral readValueForDescriptor:descriptor];

}

}

斷開連接久調(diào)用

// 停止掃描

[self.cMgr stopScan];

// 斷開連接

[self.cMgr cancelPeripheralConnection:peripheral];

基本連接就這樣啦這樣也只能裝逼實(shí)際應(yīng)用還有很多場景需要考慮!后續(xù)我會更新

第一次寫簡書寫的不好哦還請?jiān)?/p>

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容