iOS開發技術之藍牙通信功能的實現


一、相關介紹

CoreBluetooth專門用於與BLE設備通訊。並且現在很多藍牙設備都支持4.0,4.0以其低功耗著稱,所以一般也叫BLE(Bluetooth low energy),所以也是在iOS比較推薦的一種開發方法。 

Central(中心設備); 

Peripheral(外圍設備); 

advertising(廣告); 

Services(服務); 

Characteristic(特征) 

  • MFI  (make for iPad ,iTouch,iPhone)為蘋果設備制定的藍牙,開發使用ExternalAccessory框架
  • BLE    (blueTooth  low energy)  藍牙4.0之后耗電低,又名BLE,開發時使用CoreBluetooth框架
  • peripheral and central  外設與中心  ,中心控制向外發起連接,被連接的設備即為外設(外部設備)
  • service and characteristic 服務和特征 ,每個設備會提供若干服務,每個服務中又有不同的特征,特征
  • 權限一般分為讀寫以及通知等
  • Description 每個特征都有許多用戶描述,介紹設備的屬性信息(生產商,版本號等)

CoreBluetooth介紹

在CoreBluetooth中有兩個主要的部分,Central和Peripheral,CBPeripheralManager 作為外圍設備。CBCentralManager作為中心設備。所有可用的iOS設備可以作為外圍(Peripheral)也可以作為中央(Central),但不可以同時既是周邊也是中央。

外圍設備(Peripheral)設備是廣播設備的數據,中央設備(Central)是管理並且使用這些數據的設備。 

也就是說外圍(Peripheral)向周圍發送廣播,告訴周圍的中央設備(Central)它(周邊(Peripheral)這里有數據,並且說明了能提供的服務和特征值(連接之后才能獲取), 

其實藍牙傳值相當於網絡接口,硬件的service的UUID加上characteristic的UUID, 

打一個比喻:service的UUID相當於主地址,characteristic的UUID相當於短鏈接,短鏈接必須是主地址的分支,拼在一起的是接口,你和硬件設定的藍牙傳輸格式類似於json,雙方可識別的數據,因為藍牙只能支持16進制,而且每次傳輸只能20個字節,所以要把信息流轉成雙方可識別的16進制。

 

二、中心設備CBCentralManager

        CBCentralManager是管理中心設備的管理類,其中重要方法如下:

//設置中心設備代理

@property(assign, nonatomic, nullable) id<CBCentralManagerDelegate> delegate;

//中心設備當前狀態

@property(readonly) CBCentralManagerState state;

//中心設備是否正在掃描

@property(readonly) BOOL isScanning NS_AVAILABLE(NA, 9_0);

其中state是一個枚舉,有關藍牙是否可用的狀態如下:

typedef NS_ENUM(NSInteger, CBCentralManagerState) {

        //狀態未知

    CBCentralManagerStateUnknown = 0,

    //連接斷開 即將重置

    CBCentralManagerStateResetting,

    //該平台不支持藍牙

    CBCentralManagerStateUnsupported,

    //未授權藍牙使用 hovertree.com

    CBCentralManagerStateUnauthorized,

    //藍牙關閉

    CBCentralManagerStatePoweredOff,

    //藍牙正常開啟

    CBCentralManagerStatePoweredOn,

};

下面這些方法用於初始化管理中心:

//初始化方法

//設置的代理需要遵守CBCentralManagerDelegate協議

//queue可以設置藍牙掃描的線程 傳入nil則為在主線程中進行

- (instancetype)initWithDelegate:(nullable id<CBCentralManagerDelegate>)delegate

                           queue:(nullable dispatch_queue_t)queue;

//此方法同上 在options字典中用於進行一些管理中心的初始化屬性設置

//字典中支持的鍵值如下 http://www.cnblogs.com/roucheng/

/*

NSString * const CBCentralManagerOptionShowPowerAlertKey 對應一個NSNumber類型的bool值,用於設置是否在關閉藍牙時彈出用戶提示

NSString * const CBCentralManagerOptionRestoreIdentifierKey 對應一個NSString對象,設置管理中心的標識符ID

*/

- (instancetype)initWithDelegate:(nullable id<CBCentralManagerDelegate>)delegate

                           queue:(nullable dispatch_queue_t)queue

                         options:(nullable NSDictionary<NSString *, id> *)options;

//根據獲取所有已知設備

- (NSArray<CBPeripheral *> *)retrievePeripheralsWithIdentifiers:(NSArray<NSUUID *> *)identifiers;

//根據服務id獲取所有連接的設備 hovertree.com

- (NSArray<CBPeripheral *> *)retrieveConnectedPeripheralsWithServices:(NSArray<CBUUID *> *)serviceUUIDs;

在初始化管理中心完成后,會回調代理中的如下方法,我們必須實現如下方法:

//這個方法中可以獲取到管理中心的狀態

- (void)centralManagerDidUpdateState:(CBCentralManager *)central;

如果上面方法中管理中心狀態為藍牙可用,可以通過下面方法開啟掃描外設:

//serviceUUIDs用於掃描一個特點ID的外設 options用於設置一些掃描屬性 鍵值如下

/*

//是否允許重復掃描 對應NSNumber的bool值,默認為NO,會自動去重

NSString *const CBCentralManagerScanOptionAllowDuplicatesKey;

//要掃描的設備UUID 數組 對應NSArray hovertree.com

NSString *const CBCentralManagerScanOptionSolicitedServiceUUIDsKey;

*/

- (void)scanForPeripheralsWithServices:(nullable NSArray<CBUUID *> *)serviceUUIDs options:(nullable NSDictionary<NSString *, id> *)options;

//停止掃描外設

- (void)stopScan;

掃描的結果會在如下代理方法中回掉:

//peripheral 掃描到的外設

//advertisementData是外設發送的廣播數據

//RSSI 是信號強度 http://www.cnblogs.com/roucheng/

- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *, id> *)advertisementData RSSI:(NSNumber *)RSSI;

掃描到外設后,通過下面方法可以連接一個外設:

/*

options中可以設置一些連接設備的初始屬性鍵值如下

//對應NSNumber的bool值,設置當外設連接后是否彈出一個警告

NSString *const CBConnectPeripheralOptionNotifyOnConnectionKey;

//對應NSNumber的bool值,設置當外設斷開連接后是否彈出一個警告

NSString *const CBConnectPeripheralOptionNotifyOnDisconnectionKey;

//對應NSNumber的bool值,設置當外設暫停連接后是否彈出一個警告 http://www.cnblogs.com/roucheng/

NSString *const CBConnectPeripheralOptionNotifyOnNotificationKey;

*/

- (void)connectPeripheral:(CBPeripheral *)peripheral options:(nullable NSDictionary<NSString *, id> *)options;

//取消一個外設的連接

- (void)cancelPeripheralConnection:(CBPeripheral *)peripheral;

調用過連接外設的方法后,會回掉如下代理方法:

//連接外設成功

- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral;

//連接外設失敗

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

//斷開外設連接

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

當管理中心恢復時會調用如下代理:

 1 //dict中會傳入如下鍵值對 hovertree.com

 2 /*

 3 //恢復連接的外設數組

 4 NSString *const CBCentralManagerRestoredStatePeripheralsKey;

 5 //恢復連接的服務UUID數組

 6 NSString *const CBCentralManagerRestoredStateScanServicesKey;

 7 //恢復連接的外設掃描屬性字典數組

 8 NSString *const CBCentralManagerRestoredStateScanOptionsKey;

 9 */

10 - (void)centralManager:(CBCentralManager *)central willRestoreState:(NSDictionary<NSString *, id> *)dict;

 

三、外設CBPeripheralManager

        從上面我們知道,中心設備是用來掃描周圍的外設,兩台設備的通訊中,必須有一個充當中心設備,一個充當外設,外設是由CBPeripheralManager進行管理,主要方法如下:

 1 //設置外設管理中心代理

 2 @property(assign, nonatomic, nullable) id<CBPeripheralManagerDelegate> delegate;

 3 //外設狀態 枚舉如中心設備

 4 @property(readonly) CBPeripheralManagerState state;

 5 //是否正在發送廣播

 6 @property(readonly) BOOL isAdvertising;

 7 //用戶的授權狀態

 8 + (CBPeripheralManagerAuthorizationStatus)authorizationStatus;

 9 //初始化並設置代理 參數的具體含義與中心設備管理中心

10 - (instancetype)initWithDelegate:(nullable id<CBPeripheralManagerDelegate>)delegate

11                            queue:(nullable dispatch_queue_t);

12 - (instancetype)initWithDelegate:(nullable id<CBPeripheralManagerDelegate>)delegate

13                            queue:(nullable dispatch_queue_t)queue

14                          options:(nullable NSDictionary<NSString *, id> *)options;

15 //開始發送廣播 hovertree.com  何問起

16 //advertisementData中可以發送的數據有約定 如下

17 /*

18 對應設置NSString類型的廣播名

19 NSString *const CBAdvertisementDataLocalNameKey;

20 外設制造商的NSData數據

21 NSString *const CBAdvertisementDataManufacturerDataKey;

22 外設制造商的CBUUID數據

23 NSString *const CBAdvertisementDataServiceDataKey;

24 服務的UUID與其對應的服務數據字典數組

25 NSString *const CBAdvertisementDataServiceUUIDsKey;

26 附加服務的UUID數組

27 NSString *const CBAdvertisementDataOverflowServiceUUIDsKey;

28 外設的發送功率 NSNumber類型

29 NSString *const CBAdvertisementDataTxPowerLevelKey;

30 外設是否可以連接

31 NSString *const CBAdvertisementDataIsConnectable;

32 服務的UUID數組

33 NSString *const CBAdvertisementDataSolicitedServiceUUIDsKey;

34 */

35 - (void)startAdvertising:(nullable NSDictionary<NSString *, id> *)advertisementData;

36 //停止發送廣播

37 - (void)stopAdvertising;

38 //設置一個連接的具體central設備的延時 枚舉如下

39 /*

40 typedef NS_ENUM(NSInteger, CBPeripheralManagerConnectionLatency) {

41     CBPeripheralManagerConnectionLatencyLow = 0,

42     CBPeripheralManagerConnectionLatencyMedium,

43     CBPeripheralManagerConnectionLatencyHigh

44 } NS_ENUM_AVAILABLE(NA, 6_0);

45 */

46 - (void)setDesiredConnectionLatency:(CBPeripheralManagerConnectionLatency)latency forCentral:(CBCentral *)central;

47 //添加一個服務 http://www.cnblogs.com/roucheng/

48 - (void)addService:(CBMutableService *)service;

49 //移除一個服務

50 - (void)removeService:(CBMutableService *)service;

51 //移除所有服務

52 - (void)removeAllServices;

53 //響應中心設備的讀寫請求

54 - (void)respondToRequest:(CBATTRequest *)request withResult:(CBATTError)result;

55 //更新一個連接中心設備的訂閱特征值

56 - (BOOL)updateValue:(NSData *)value forCharacteristic:(CBMutableCharacteristic *)characteristic onSubscribedCentrals:(nullable NSArray<CBCentral *> *)centrals;

外設代理的相關方法如下:

 1 //這個方法是必須實現的 狀態可用后可以發送廣播

 2 - (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral;

 3 //連接回復時調用的方法 和centralManager類似

 4 - (void)peripheralManager:(CBPeripheralManager *)peripheral willRestoreState:(NSDictionary<NSString *, id> *)dict;

 5 //開始發送廣播時調用的方法

 6 - (void)peripheralManagerDidStartAdvertising:(CBPeripheralManager *)peripheral error:(nullable NSError *)error;

 7 //添加服務調用的回調

 8 - (void)peripheralManager:(CBPeripheralManager *)peripheral didAddService:(CBService *)service error:(nullable NSError *)error;

 9 //當一個central設備訂閱一個特征值時調用的方法

10 - (void)peripheralManager:(CBPeripheralManager *)peripheral central:(CBCentral *)central didSubscribeToCharacteristic:(CBCharacteristic *)characteristic;

11 //取消訂閱一個特征值時調用的方法

12 - (void)peripheralManager:(CBPeripheralManager *)peripheral central:(CBCentral *)central didUnsubscribeFromCharacteristic:(CBCharacteristic *)characteristic;

13 //收到讀請求時觸發的方法 何問起 hovertree.com

14 - (void)peripheralManager:(CBPeripheralManager *)peripheral didReceiveReadRequest:(CBATTRequest *)request;

15 //收到寫請求時觸發的方法

16 - (void)peripheralManager:(CBPeripheralManager *)peripheral didReceiveWriteRequests:(NSArray<CBATTRequest *> *)requests;

17 //外設准備更新特征值時調用的方法

18 - (void)peripheralManagerIsReadyToUpdateSubscribers:(CBPeripheralManager *)peripheral;

 

四、中心設備與外設對象CBCentral與CBPeripheral

        上面介紹了中心設備管理類與外設管理類,這些類用於將設備連接建立起來,器具的數據交換的服務和一些信息則是在對應的設備對象中。

1、中心設備 CBCentral屬性與方法

//設備UUID

@property(readonly, nonatomic) NSUUID *identifier;

//中心設備最大接收的數據長度

@property(readonly, nonatomic) NSUInteger maximumUpdateValueLength;

2、外設CAPeripheral屬性與方法

        外設對象要比中心對象復雜的多,當centralManager連接到外設后,需要通過外設對象的代理方法進行數據交互,其中主要方法屬性如下:

68395175_1.gif

 1 //設置代理

 2 @property(assign, nonatomic, nullable) id<CBPeripheralDelegate> delegate;

 3 //外設name

 4 @property(retain, readonly, nullable) NSString *name;

 5 //信號強度 http://www.cnblogs.com/roucheng/

 6 @property(retain, readonly, nullable) NSNumber *RSSI NS_DEPRECATED(NA, NA, 5_0, 8_0);

 7 //外設狀態

 8 /*

 9 typedef NS_ENUM(NSInteger, CBPeripheralState) {

10     CBPeripheralStateDisconnected = 0,//未連接

11     CBPeripheralStateConnecting,//正在鏈接

12     CBPeripheralStateConnected,//已經連接

13     CBPeripheralStateDisconnecting NS_AVAILABLE(NA, 9_0),//正在斷開連接

14 } NS_AVAILABLE(NA, 7_0);

15 */

16 @property(readonly) CBPeripheralState state;

17 //所有的服務數組

18 @property(retain, readonly, nullable) NSArray<CBService *> *services;

19 //獲取當前信號強度

20 - (void)readRSSI;

21 //根據服務UUID尋找服務對象

22 - (void)discoverServices:(nullable NSArray<CBUUID *> *)serviceUUIDs;

23 //在服務對象UUID數組中尋找特定服務

24 - (void)discoverIncludedServices:(nullable NSArray<CBUUID *> *)includedServiceUUIDs forService:(CBService *)service;

25 //在一個服務中尋找特征值

26 - (void)discoverCharacteristics:(nullable NSArray<CBUUID *> *)characteristicUUIDs forService:(CBService *)service;

27 //從一個特征中讀取數據

28 - (void)readValueForCharacteristic:(CBCharacteristic *)characteristic;

29 //寫數據的最大長度 hovertree.com  何問起

30 //type枚舉如下

31 /*

32 typedef NS_ENUM(NSInteger, CBCharacteristicWriteType) {

33     CBCharacteristicWriteWithResponse = 0,//寫數據並且接收成功與否回執

34     CBCharacteristicWriteWithoutResponse,//寫數據不接收回執

35 };

36 */

37 - (NSUInteger)maximumWriteValueLengthForType:(CBCharacteristicWriteType)type NS_AVAILABLE(NA, 9_0);

38 //向某個特征中寫數據

39 - (void)writeValue:(NSData *)data forCharacteristic:(CBCharacteristic *)characteristic type:(CBCharacteristicWriteType)type;

40 //為制定的特征值設置監聽通知

41 - (void)setNotifyValue:(BOOL)enabled forCharacteristic:(CBCharacteristic *)characteristic;

42 //尋找特征值的描述

43 - (void)discoverDescriptorsForCharacteristic:(CBCharacteristic *)characteristic;

44 //讀取特征的描述值

45 - (void)readValueForDescriptor:(CBDescriptor *)descriptor;

46 //寫特征的描述值

47 - (void)writeValue:(NSData *)data forDescriptor:(CBDescriptor *)descriptor;

View Code

外設的代理方法如下:

68395175_1.gif

 1 //外設名稱更改時回調的方法

 2 - (void)peripheralDidUpdateName:(CBPeripheral *)peripheral NS_AVAILABLE(NA, 6_0);

 3 //外設服務變化時回調的方法

 4 - (void)peripheral:(CBPeripheral *)peripheral didModifyServices:(NSArray<CBService *> *)invalidatedServices NS_AVAILABLE(NA, 7_0);

 5 //信號強度改變時調用的方法

 6 - (void)peripheralDidUpdateRSSI:(CBPeripheral *)peripheral error:(nullable NSError *)error NS_DEPRECATED(NA, NA, 5_0, 8_0);

 7 //讀取信號強度回調的方法 柯樂義 keleyi.com

 8 - (void)peripheral:(CBPeripheral *)peripheral didReadRSSI:(NSNumber *)RSSI error:(nullable NSError *)error NS_AVAILABLE(NA, 8_0);

 9 //發現服務時調用的方法

10 - (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(nullable NSError *)error;

11 //在服務中發現子服務回調的方法

12 - (void)peripheral:(CBPeripheral *)peripheral didDiscoverIncludedServicesForService:(CBService *)service error:(nullable NSError *)error;

13 //發現服務的特征值后回調的方法

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

15 //特征值更新時回調的方法

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

17 //向特征值寫數據時回調的方法

18  - (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error;

19  //特征值的通知設置改變時觸發的方法

20  - (void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error;

21  //發現特征值的描述信息觸發的方法

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

23  //特征的描述值更新時觸發的方法

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

25  //寫描述信息時觸發的方法

26  - (void)peripheral:(CBPeripheral *)peripheral didWriteValueForDescriptor:(CBDescriptor *)descriptor error:(nullable NSError *)error;

View Code

 

五、服務對象CBService

    服務對象是用來管理外設提供的一些數據服務的,其中屬性如下:

//對應的外設

@property(assign, readonly, nonatomic) CBPeripheral *peripheral;

//是否是初等服務

@property(readonly, nonatomic) BOOL isPrimary;

//包含的自服務 http://www.cnblogs.com/roucheng/

@property(retain, readonly, nullable) NSArray<CBService *> *includedServices;

//服務中的特征值

@property(retain, readonly, nullable) NSArray<CBCharacteristic *> *characteristics;

 

六、服務的特征值CBCharacteristic

        通過綁定服務中的特征值來進行數據的讀寫操作,其中屬性如下:

//對應的服務對象

@property(assign, readonly, nonatomic) CBService *service;

//特征值的屬性 枚舉如下

/*

typedef NS_OPTIONS(NSUInteger, CBCharacteristicProperties) {

    CBCharacteristicPropertyBroadcast,//允許廣播特征

    CBCharacteristicPropertyRead,//可讀屬性

    CBCharacteristicPropertyWriteWithoutResponse,//可寫並且接收回執

    CBCharacteristicPropertyWrite,//可寫屬性

    CBCharacteristicPropertyNotify,//可通知屬性

    CBCharacteristicPropertyIndicate,//可展現的特征值

    CBCharacteristicPropertyAuthenticatedSignedWrites,//允許簽名的特征值寫入

    CBCharacteristicPropertyExtendedProperties,

    CBCharacteristicPropertyNotifyEncryptionRequired,

    CBCharacteristicPropertyIndicateEncryptionRequired

};

*/

@property(readonly, nonatomic) CBCharacteristicProperties properties;

//特征值的數據 

@property(retain, readonly, nullable) NSData *value;

//特征值的描述

@property(retain, readonly, nullable) NSArray<CBDescriptor *> *descriptors;

//是否是當前廣播的特征

@property(readonly) BOOL isBroadcasted;

//是否是正在通知的特征

@property(readonly) BOOL isNotifying;

 

七、讀寫請求對象CBATTRequest

        服務對象是外設向中心設備提供的相關數據服務,獲取到相應服務后,中心設備可以進行讀寫請求,讀寫對象屬性如下:

//對應的中心設備

@property(readonly, nonatomic) CBCentral *central;

//對應的特征值

@property(readonly, nonatomic) CBCharacteristic *characteristic;

//讀寫數據值

@property(readwrite, copy, nullable) NSData *value;

 

八、開發流程

1. 新建Central Manager實例並進行監聽藍牙設備狀態 

2. 開始搜索外圍設備,通過delegate獲得數據 

3. 連接外圍設備,delegate通知連接結果 

4. 獲得外圍設備的服務,delegate獲得結果 

5. 獲得服務的特征,delegate獲得結果 

6. 根據服務和特征給外圍設備發送數據 

7. 根據delegate回調,從外圍設備讀數據

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM