国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

CoreBluetooth-外圍模式

2019-11-11 07:46:28
字體:
來源:轉載
供稿:網友

這里只能介紹下外圍模式開發的一般流程了,沒經歷過具體項目的洗禮,具體的坑點沒接觸到,以后如果有機會再加以補充。

一般流程模式

1.打開peripheralManager,設置peripheralManager的代理

2.創建characteristics,characteristics的PRoperties 創建service,把characteristics添加到service中,再把service添加到peripheralManager中

3.開啟廣播advertising

4.對central的操作進行響應

4.1 讀characteristics請求4.2 寫characteristics請求4.3 訂閱和取消訂閱characteristics

代碼流程

1.準備工作,引入頭文件和代理

#import <CoreBluetooth/CoreBluetooth.h>@interface ViewController ()<CBPeripheralManagerDelegate>@property (nonatomic,strong) CBPeripheralManager * perManager;@end

2.打開peripheralManager,設置peripheralManager的代理

_perManager = [[CBPeripheralManager alloc] initWithDelegate:self queue:nil];/* 會自動回調方法 - (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral;只有在peripheral.state == CBManagerStatePoweredOn的狀態下創建characteristics 和 service才有用*/

3.創建characteristics,characteristics的description 創建service,把characteristics添加到service中,再把service添加到peripheralManager中

- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral{ if (peripheral.state == CBManagerStatePoweredOn) { //服務 uuid換成自己的 CBUUID * serviceUUID = [CBUUID UUIDWithString:@"1111"]; CBMutableService * service = [[CBMutableService alloc] initWithType:serviceUUID primary:YES]; /*特征值 uuid換成自己的 properties也換成自己的,這里設置成可讀、可寫、廣播*/ CBUUID * characteristicUUID = [CBUUID UUIDWithString:@"2222"]; CBCharacteristic * characteristic = [[CBMutableCharacteristic alloc] initWithType:characteristicUUID properties:(CBCharacteristicPropertyRead|CBCharacteristicPropertyWrite|CBCharacteristicPropertyNotify) value:nil permissions:CBAttributePermissionsReadable]; //特征值加入到服務 service.characteristics = @[characteristic]; /*注冊服務 注冊成功后的服務才能廣播 注冊后會自動回調方法 - (void)peripheralManager:(CBPeripheralManager *)peripheral didAddService:(CBService *)service error:(nullable NSError *)error; */ [_perManager addService:service]; }}

4.開啟廣播advertising 。 調用方法- (void)startAdvertising:(nullable NSDictionary < NSString *, id > *)advertisementData;來開啟廣播, 參數可選: CBAdvertisementDataLocalNameKey CBAdvertisementDataTxPowerLevelKey CBAdvertisementDataServiceUUIDsKey CBAdvertisementDataServiceDataKey CBAdvertisementDataManufacturerDataKey CBAdvertisementDataOverflowServiceUUIDsKey CBAdvertisementDataIsConnectable CBAdvertisementDataSolicitedServiceUUIDsKey

- (void)peripheralManager:(CBPeripheralManager *)peripheral didAddService:(CBService *)service error:(NSError *)error{ //開啟廣播服務 [_perManager startAdvertising:@{CBAdvertisementDataServiceUUIDsKey:[CBUUID UUIDWithString:@"1111"]}];}//開啟廣播的回調- (void)peripheralManagerDidStartAdvertising:(CBPeripheralManager *)peripheral error:(NSError *)error {}

5.1 讀characteristics請求

// 讀數據請求- (void)peripheralManager:(CBPeripheralManager *)peripheral didReceiveReadRequest:(CBATTRequest *)request { //請求的響應 NSData *data = request.characteristic.value; //center想讀取的數據 [request setValue:data]; [_perManager respondToRequest:request withResult:CBATTErrorSuccess];}

5.2 寫characteristics請求

//寫數據請求- (void)peripheralManager:(CBPeripheralManager *)peripheral didReceiveWriteRequests:(NSArray<CBATTRequest *> *)requests { //請求的響應 //需要轉換成CBMutableCharacteristic對象才能進行寫值 CBMutableCharacteristic *c =(CBMutableCharacteristic *)request.characteristic; c.value = request.value; [_perManager respondToRequest:[requests firstObject] withResult:CBATTErrorSuccess];}

5.3 訂閱和取消訂閱characteristics

//訂閱characteristics-(void)peripheralManager:(CBPeripheralManager *)peripheral central:(CBCentral *)central didSubscribeToCharacteristic:(CBCharacteristic *)characteristic{ NSLog(@"訂閱了 %@的數據",characteristic.UUID); //每秒執行一次給主設備發送一個當前時間的秒數 timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(sendData:) userInfo:characteristic repeats:YES];}//取消訂閱characteristics-(void)peripheralManager:(CBPeripheralManager *)peripheral central:(CBCentral *)central didUnsubscribeFromCharacteristic:(CBCharacteristic *)characteristic{ NSLog(@"取消訂閱 %@的數據",characteristic.UUID); //取消回應 [timer invalidate];}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 武山县| 仙居县| 金寨县| 桐城市| 中方县| 尼木县| 廊坊市| 黑水县| 双江| 安吉县| 城固县| 丽江市| 长丰县| 鹿邑县| 兴安县| 罗定市| 霞浦县| 定兴县| 铜梁县| 城口县| 乾安县| 叶城县| 新邵县| 公主岭市| 桦甸市| 桃园县| 江门市| 腾冲县| 海淀区| 延津县| 大关县| 沂南县| 桂平市| 汕头市| 铜梁县| 达孜县| 黄大仙区| 化州市| 绵竹市| 惠水县| 乃东县|