iOS 藍芽連線小公尺手環

2021-07-27 08:57:54 字數 3809 閱讀 1009

**:

本文將一步一步講解如何使用corebluetooth框架來與各種可穿戴裝置進行通訊,使用 小公尺手環 來進行基本的測試。 

4 小公尺手環

從上面這幅圖可以看到,我們的ios裝置是central,用來接收資料和傳送命令,而外設比如小公尺手環是peripheral,向外傳輸資料和接收命令。我們要做的就是通過central來連線peripheral,然後實現資料的接收和控制指令的傳送。在做到這一步之後,再根據具體的硬體,對接收到的資料進行parse解析。

這裡用的是心率裝置來做說明,每個外設peripheral都有對應的服務service,比如這裡是心率service。乙個外設可以有不止乙個s、service。每個service裡面可以有多個屬性characteristic,比如這裡有兩個characteristic,乙個是用來測量心率,乙個是用來定位位置。

那麼很關鍵的一點是每個service,每個characteristic都是用uuid來確定的。uuid就是每個service或characteristic的identifier。

在實際使用中,我們都是要通過uuid來獲取資料。這點非常重要。 

在corebluetooth中,其具體的資料結構圖如下: 

從名字上大家可以很清楚的知道,這個類是用來管理ble的。我們也就是通過這個類來實現連線。

先建立乙個:

@property

(nonatomic

,strong

) cbcentralmanager *centralmanager;

dispatch_queue_t

centralqueue = dispatch_queue_create(

"com.manmanlai"

, dispatch_queue_serial);  

self

.centralmanager

= [[cbcentralmanager alloc] initwithdelegate:

self

queue:centralqueue];

然後關鍵在於cbcentralmanagerdelegate的使用。這個之後再講。

有了cbcentralmanager,接下來就是尋找cbperipheral外設,方法很簡單:

[self

.centralmanager

scanforperipheralswithservices:

@options:

nil];

這裡的service就是對應的uuid,如果為空,這scan所有service。

在上一步中,如果找到了裝置,則cbcentralmanager的delegate會呼叫下面的方法:

- (void

)centralmanager:(cbcentralmanager *)central diddiscoverperipheral:(cbperipheral *)peripheral advertisementdata:(

nsdictionary

*)advertisementdata rssi:(

nsnumber

*)rssi   if

(!self

.peripheral

|| (

self

.peripheral

.state

== cbperipheralstatedisconnected)) }

我們在這裡建立了乙個cbperipheral的物件,然後直接連線 

cbperipheral的物件也需要設定delegate.

如果peripheral連線成功的話,就會呼叫delegate的方法:

- (void

)centralmanager:(cbcentralmanager *)central didconnectperipheral:(cbperipheral *)peripheral   [

self

.centralmanager

stopscan];  

nslog

(@"peripheral did connect"

);   [

self

.peripheral

discoverservices:

nil]; }

我們這裡先停止scan,然後讓peripheral外設尋找其service。

找到service後會呼叫下面的方法:

- (void

)peripheral:(cbperipheral *)peripheral diddiscoverservices:(

nserror

*)error   if

(error !=

nil)  

services = [peripheral services];   if

(!services || ![services count])  

for(cbservice *service in services) }

我們根據找到的service尋找其對應的characteristic。

找到characteristic後會呼叫下面的delegate方法:

- (void

)peripheral:(cbperipheral *)peripheral diddiscovercharacteristicsforservice:(cbservice *)service error:(

nserror

*)error   if

(error !=

nil)  

self

.characteristic

= [characteristics firstobject];

//[self.peripheral readvalueforcharacteristic:self.characteristic]; [

self

.peripheral

setnotifyvalue:

yesforcharacteristic:

self

.characteristic

];這裡我們可以使用readvalueforcharacteristic:來讀取資料。如果資料是不斷更新的,則可以使用setnotifyvalue:forcharacteristic:來實現只要有新資料,就獲取。

讀到資料後會呼叫delegate方法:

這個很簡單,只要使用

[self

.peripheral writevalue:

data

forcharacteristic:

self

.characteristic

type

:cbcharacteristicwritewithresponse];

data是nsdata型別。

使用小公尺手環實驗,得到如下結果:

通過上面的方法,我們就可以輕鬆的對ble進行開發。實際上比想象的要簡單。

iOS 藍芽連線小公尺手環

本文將一步一步講解如何使用corebluetooth框架來與各種可穿戴裝置進行通訊,使用 小公尺手環 來進行基本的測試。4 小公尺手環 從上面這幅圖可以看到,我們的ios裝置是central,用來接收資料和傳送命令,而外設比如小公尺手環是peripheral,向外傳輸資料和接收命令。我們要做的就是通...

小公尺手環5續航時間 小公尺手環5配置功能介紹

文章目錄 小公尺於 6 月 11 日正式發布全新的 小公尺手環5 其主要功能包程式設計客棧括全天睡眠監測,並新增了多項運動模式。小公尺手環 5 採用 1.1 英吋動態彩顯螢幕,螢幕表面由 2.5d 耐刮花玻璃lddbyntln面板保護,螢幕背光亮www.cppcns.com度最高可達 450nit。...

為小公尺手環的定價叫好

小公尺手環一經發布,就迎來了正反兩面兩種不同的聲音。譴責者有之,叫好者有之。譴責者,不外乎是即得利益者,如現有的智慧型手環生產廠商 投資人 渠道商 一些一經進入或早已進入該市場的手機廠商。叫好者,當然是眾多的使用者。作為使用者,我為小公尺智慧型手環的定價叫好。作為智慧型穿戴的創業者,我還得為小公尺智...