iOS藍芽4 0開發 BLE

2022-09-17 07:51:11 字數 3032 閱讀 2798

模型與corebluetooth的對應關係;

這裡主要討論模型一,這也是當前大多數手環裝置和ios 互動的方式;開發流程

1. 建立工程,匯入corebluetooth.framework

2. 初始化 cbcentralmanager 並準備掃瞄周圍藍芽裝置

//初始化    

themanager = [[cbcentralmanager alloc]initwithdelegate:self queue:nil];

//初始化完成會呼叫如下**,返回當前手機的藍芽狀態

//當前藍芽主裝置狀態

-(void)centralmanagerdidupdatestate:(cbcentralmanager*)centralelse}}

3. 當藍芽狀態一切正常時,這時可以開始搜尋周邊裝置了,比如手環

//開始連線action

- (ibaction)startconnectaction:(id)sender

}

4. 在掃瞄到裝置的**裡面,連線我們找到的智慧型裝置

//掃瞄到裝置會進入方法

-(void)centralmanager:(cbcentralmanager *)central diddiscoverperipheral:(cbperipheral *)peripheral advertisementdata:(nsdictionary *)advertisementdata rssi:(nsnumber *)rssi

}

5. 連線成功之後,去掃瞄裝置的服務;掃瞄到服務之後,緊接著去掃瞄裝置的特徵;好比乙個類提供的介面,只有知道這些,我們才能操作智慧型裝置

//

掃瞄到特徵

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

nslog(

@"掃瞄到外設服務特徵有:%@->%@->%@

",peripheral.name,service.uuid,service.characteristics);

//獲取characteristic的值

for (cbcharacteristic *characteristic in

service.characteristics)

//電池電量

else

if([characteristic.uuid.uuidstring isequaltostring:kmi_butery])

else

if([characteristic.uuid.uuidstring isequaltostring:kmi_shake])

//裝置資訊

else

if([characteristic.uuid.uuidstring isequaltostring:kmi_device])}}

}//掃瞄到服務

-(void)peripheral:(cbperipheral *)peripheral diddiscoverservices:(nserror *)error

nslog(

@"掃瞄到外設服務:%@ -> %@

",peripheral.name,peripheral.services);

for (cbservice *service in

peripheral.services)

nslog(

@"開始掃瞄外設服務的特徵 %@...

",peripheral.name);

}#pragma mark 裝置掃瞄與連線的**

//連線到peripherals-成功

- (void)centralmanager:(cbcentralmanager *)central didconnectperipheral:(cbperipheral *)peripheral

//連線外設失敗

-(void)centralmanager:(cbcentralmanager *)central didfailtoconnectperipheral:(cbperipheral *)peripheral error:(nserror *)error

//與外設斷開連線

- (void)centralmanager:(cbcentralmanager *)central diddisconnectperipheral:(cbperipheral *)peripheral error:(nserror *)error

view code

6. 當我們對裝置傳送一些指令時,會在如下**裡面響應到裝置返回的資料

#pragma mark 裝置資訊處理

//掃瞄到具體的值

- (void)peripheral:(cbperipheral *)peripheral didupdatevalueforcharacteristic:(cbcharacteristic *)characteristic error:(nullable nserror *)error

nslog(

@"%@ %@

",characteristic.uuid.uuidstring,characteristic.value);

if([characteristic.uuid.uuidstring isequaltostring:kmi_step])

else

if([characteristic.uuid.uuidstring isequaltostring:kmi_butery])

else

if([characteristic.uuid.uuidstring isequaltostring:kmi_device])

[self.activeid stopanimating];

self.connectbtn.enabled =yes;

self.title = @"

資訊掃瞄完成";

}

view code

7. 完整的**工程;

iOS藍芽4 0 BLE 開發

本文將一步一步講解如何使用corebluetooth框架來與各種可穿戴裝置進行通訊,使用 小公尺手環 來進行基本的測試。macbook pro mac os x 10.10 xcode 6.3.2 iphone 5s v8.1 小公尺手環 從上面這幅圖可以看到,我們的ios裝置是central,用來...

iOS藍芽BLE開發

藍芽是乙個標準的無線通訊協議,具有裝置成本低 傳輸距離近和功耗低等特點,被廣泛的應用在多種場合。藍芽一般分為傳統藍芽和ble兩種模式 傳統藍芽可以傳輸音訊等較大資料量,距離近 功耗相對大 而ble則用來傳輸節點資料,傳輸資料量十分小,多數情況處於休眠狀態,因而功耗十分低,被廣泛的應用於智慧型穿戴裝置...

ios開發藍芽 BLE

import 中心管理者 property nonatomic,strong cbcentralmanager themanager property nonatomic,strong cbperipheral theperpher property nonatomic,strong cbchara...