iOS藍芽4 0 BLE 開發

2021-07-11 06:12:28 字數 4635 閱讀 4287

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

macbook pro mac os x 10.10

xcode 6.3.2

iphone 5s v8.1

小公尺手環

從上面這幅圖可以看到,我們的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。

- (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:yes forcharacteristic:self

.characteristic];

這裡我們可以使用readvalueforcharacteristic:來讀取資料。

如果資料是不斷更新的,則可以使用setnotifyvalue:forcharacteristic:來實現只要有新資料,就獲取。

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

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

這個很簡單,只要使用:

[

self

.peripheral writevalue:data forcharacteristic:self

.characteristic type:cbcharacteristicwritewithresponse]

;

data是nsdata型別。

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

2015-06-10

16:52:31.607 ketherdemo[13786:1792995] scaning device

2015-06-10

16:52:33.474 ketherdemo[13786:1793032] name:0x1700e4380, identifier = 6ff833e3-93c1-28c6-cbc0-74a706aaae31, name = ls_sca16, state = disconnected>

2015-06-10

16:52:33.475 ketherdemo[13786:1793032] connect peripheral

2015-06-10

16:52:37.538 ketherdemo[13786:1793031] peripheral did connect

2015-06-10

16:52:37.984 ketherdemo[13786:1793031] service:fee7

2015-06-10

16:52:37.985 ketherdemo[13786:1793031] service:device information

2015-06-10

16:52:38.099 ketherdemo[13786:1793032] characteristics:(

"",""

)2015-06-10

16:52:38.100 ketherdemo[13786:1793032] kether did connect

2015-06-10

16:52:38.101 ketherdemo[13786:1793032] kether did connect

2015-06-10

16:52:38.280 ketherdemo[13786:1793031] characteristics:(

"","",

"","",

"",

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

iOS藍芽4 0開發 BLE

模型與corebluetooth的對應關係 這裡主要討論模型一,這也是當前大多數手環裝置和ios 互動的方式 開發流程 1.建立工程,匯入corebluetooth.framework 2.初始化 cbcentralmanager 並準備掃瞄周圍藍芽裝置 初始化 themanager cbcentr...

藍芽4 0(BLE)電子煙方案

電子香菸得到了越來越多的消費者的青睞。它不僅僅能讓吸菸者的健康不受到危害,而且還能讓他們的吸菸行為不受地點的限制。但是似乎有人認為 這些電子香菸的功能還 應該更強大點 近日,一種更加智慧型化的電子香菸問世,它就是smokio 據悉,這種電子香菸使用藍芽 針對這一產品市場,信馳達開發出藍芽4.0 電子...

藍芽4 0BLE協議棧介紹

問題 什麼是ble協議棧?ble協議棧與ble協議的關係?顧名思義,人類之間需要一種語言才能溝通,機器之間,電子之間也有乙個標準,也就是作為通訊標準,也就是通訊協議。協議 定義了一系列通訊標準,通訊雙方都按照這一標準,進行資料通訊。協議棧 協議的具體實現形式,實現的函式庫,韌體庫。把個個協議集合到了...