概覽 略
Central and Peripheral Devices and Their Roles in Bluetooth Communication
central和peripheral設(shè)備和他們?cè)谒{(lán)牙通訊中的角色
藍(lán)牙通訊中的兩個(gè)重要的角色涤躲,central和peripheral。相對(duì)于傳統(tǒng)的client-server架構(gòu)崖叫,peripheral代表著擁有數(shù)據(jù)的一方并且外界是需要這數(shù)據(jù)的授艰。central代表著使用數(shù)據(jù)方婴洼,從peripheral那里獲得數(shù)據(jù)并完成特定的事情蒂阱。如圖1-1所示洪乍。心率監(jiān)測(cè)器有數(shù)據(jù),你的mac和ios app 需要這些數(shù)據(jù)开瞭。
Figure 1-1 Central and peripheral devices?
Centrals Discover and Connect to Peripherals That Are Advertising
centrals發(fā)現(xiàn)并連接到正在廣播的peripherals
peripheral在廣播包中廣播他們的數(shù)據(jù)懒震。廣告包中應(yīng)該包含一些有用的信息,比如peripheral的名稱和主要功能等嗤详。比如个扰,數(shù)字恒溫器可能就廣播當(dāng)前的溫度。在低功耗藍(lán)牙中廣播是periphera被發(fā)現(xiàn)的主要方式葱色。
central递宅,可以掃描和監(jiān)聽(tīng)任何他感興趣的peripheral設(shè)備。如圖1-2苍狰,central可以要求連接到任何他發(fā)現(xiàn)廣播包的peripheral.
Figure 1-2 Advertising and discovery?
How the Data of a Peripheral Is Structured
peripheral如何組織數(shù)據(jù)
peripheral可以包含一個(gè)或多個(gè)services或提供它們連接信號(hào)強(qiáng)度的信息办龄。service是數(shù)據(jù)和輔助行為的集合。比如舞痰,一個(gè)心率監(jiān)控器的服務(wù)用來(lái)輸出心率數(shù)據(jù)。?
Peripherals may contain one or more services or provide useful information about their connected signal strength. A service is a collection of data and associated behaviors for accomplishing a function or feature of a device (or portions of that device). For example, one service of a heart rate monitor may be to expose heart rate data from the monitor’s heart rate sensor.
service本身由characteristics和引用的services組成诀姚。characteristic提供服務(wù)的詳細(xì)的信息响牛。比如,心率服務(wù)可能包含一個(gè)特征值用來(lái)描述目標(biāo)的位置,同時(shí)包含另一個(gè)特征值用來(lái)傳輸心率的數(shù)據(jù)呀打。圖1-3描述這種組織結(jié)構(gòu)
Figure 1-3 A peripheral’s service and characteristics?
Centrals Explore and Interact with the Data on a Peripheral
central檢索并與peripheral交互數(shù)據(jù)
建立連接之后矢赁,就可以discover所有的services和characteristics(廣播中可能只包含部分services)
central可以通過(guò)services的characteristic來(lái)讀或?qū)憯?shù)據(jù)。你的應(yīng)用可以從恒溫器那邊讀取到當(dāng)前的溫度贬丛,也可以設(shè)置你想要的目標(biāo)溫度撩银。
How Centrals, Peripherals, and Peripheral Data Are Represented
centrals,peripherals 和 peripheral的數(shù)據(jù)如何表示
角色和數(shù)據(jù)在藍(lán)牙框架中以一種簡(jiǎn)單直接的方式表示。
Objects on the Central Side
central端的對(duì)象
當(dāng)你使用central來(lái)與peripheral通訊豺憔,你執(zhí)行的是central端的操作额获。
Local Centrals and Remote Peripherals
在central端,設(shè)備用CBCentralManager對(duì)象代表恭应。這個(gè)對(duì)象用來(lái)管理被發(fā)現(xiàn)的已連接的peripherals抄邀。圖1-4顯示藍(lán)牙框架中如何表示central和peripheral.
Figure 1-4 Core Bluetooth objects on the central side?
?
A Remote Peripheral’s Data Are Represented by CBService and CBCharacteristic Objects
peripheral的數(shù)據(jù)用CBService和CBCharacteristic來(lái)表示。
當(dāng)你與peripheral交互時(shí)昼榛,你需要使用他的sevices和characteristics境肾。在藍(lán)牙sdk中service用CBService對(duì)象表示,characteristic用CBCharacteristic對(duì)象表示胆屿。圖1-5描述這種結(jié)構(gòu)
Figure 1-5 A remote peripheral’s tree of services and characteristics?
Objects on the Peripheral Side
peripheral 端的對(duì)象
如果是安裝OS X v10.9 或 iOS6奥喻,那你的mac和ios設(shè)備就可以作為peripheral端設(shè)備,為其他設(shè)別提供數(shù)據(jù)非迹。讓你的設(shè)備實(shí)現(xiàn)peripheral角色环鲤,執(zhí)行peripheral端的功能。
Local Peripherals and Remote Centrals
本地peripherals和遠(yuǎn)端centrals
在peripheral端彻秆,peripheral設(shè)備用CBPeripheralManager對(duì)象表示楔绞。這個(gè)對(duì)象用來(lái)管理發(fā)布services和characteristics,并發(fā)出廣播。同時(shí)peripheral manager可用來(lái)響應(yīng)讀和寫(xiě)請(qǐng)求唇兑。圖1-6顯示這種關(guān)系
Figure 1-6 Core Bluetooth objects on the peripheral side?
A Local Peripheral’s Data Are Represented by CBMutableService and CBMutableCharacteristic Objects
本地peripheral的數(shù)據(jù)用CBMutableService和CBMutableCharacteristic對(duì)象表示
當(dāng)你創(chuàng)建并想設(shè)置peripheral的數(shù)據(jù)酒朵。你需要處理peripheral manager 對(duì)象的services (CBMutableService實(shí)例),同樣扎附,characteristic的實(shí)例也是CBMutableCharacteristic對(duì)象蔫耽。圖1-7說(shuō)明這種關(guān)系
Figure 1-7 A local peripheral’s tree of services and characteristics?