iOS蓝牙开发(三)实现外设功能
生活随笔
收集整理的這篇文章主要介紹了
iOS蓝牙开发(三)实现外设功能
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
有時我們需要將我們的設備作為外設,來為其他中心設備提供服務。需要以下步驟:
1.創建一個CBPeripheralManager實例
2.基于peripheral實例,創建services和characteristics實例。
3.發布services和characteristics到你設備本地數據庫
4.廣播你創建的服務
5.對中心設備的讀寫請求做出相應,向訂閱數據的中心設備發送數據更新通知
myPeripheralManager =[[CBPeripheralManager alloc] initWithDelegate:self queue:nil options:nil];通過唯一的UUID來標示你創建的characteristics和services,UUID為128位,可以通過命令行uuidgen來產生多個UUID,一些通用的UUID為16位標示,但是系統會默認將其補充為128位。自己生成的UUID必須為128位。
CBUUID *heartRateServiceUUID = [CBUUID UUIDWithString: @"180D"];//180D為通用的心率服務IDCBUUID *myCustomServiceUUID =[CBUUID UUIDWithString:@"71DA3FD1-7E10-41C1-B16F-4430B506CDE7"];myCharacteristic =[[CBMutableCharacteristic alloc] initWithType:myCharacteristicUUIDproperties:CBCharacteristicPropertyReadvalue:myValue permissions:CBAttributePermissionsReadable];//創建特征myService = [[CBMutableService alloc] initWithType:myServiceUUID primary:YES];//創建服務myService.characteristics = @[myCharacteristic];//[myPeripheralManager addService:myService];//發布服務,發布之后會加入到本地數據庫,之后就不能修改服務了// CBPeripheralManagerDelegate - (void)peripheralManager:(CBPeripheralManager *)peripheraldidAddService:(CBService *)serviceerror:(NSError *)error {if (error) {NSLog(@"Error publishing service: %@", [error localizedDescription]);}... }發布服務到本地數據庫之后就可以廣播服務了
[myPeripheralManager startAdvertising:@{ CBAdvertisementDataServiceUUIDsKey :@[myFirstService.UUID, mySecondService.UUID] }];- (void)peripheralManagerDidStartAdvertising:(CBPeripheralManager *)peripheralerror:(NSError *)error {if (error) {NSLog(@"Error advertising: %@", [error localizedDescription]);}... }?對中心設備的讀寫請求相應
- (void)peripheralManager:(CBPeripheralManager *)peripheraldidReceiveReadRequest:(CBATTRequest *)request {if ([request.characteristic.UUID isEqual:myCharacteristic.UUID]) {...if (request.offset > myCharacteristic.value.length) {[myPeripheralManager respondToRequest:requestwithResult:CBATTErrorInvalidOffset];return;}else {request.value = [myCharacteristic.valuesubdataWithRange:NSMakeRange(request.offset,myCharacteristic.value.length - request.offset)];[myPeripheralManager respondToRequest:request withResult:CBATTErrorSuccess];}} }對于寫請求,在寫代理方法中類似實現方式,修改值使用
myCharacteristic.value = request.value;方法。對于中心設備注冊監聽的特征
- (void)peripheralManager:(CBPeripheralManager *)peripheralcentral:(CBCentral *)central didSubscribeToCharacteristic:(CBCharacteristic *)characteristic {NSLog(@"Central subscribed to characteristic %@", characteristic);... }NSData *updatedValue = // fetch the characteristic's new valueBOOL didSendValue = [myPeripheralManager updateValue:updatedValueforCharacteristic:characteristic onSubscribedCentrals:nil];//如果用于更新的隊列滿了,會返回NO,當有隊列有空位時會調用peripheralManagerIsReadyToUpdateSubscribers: 方法,你可以在這個方法里重新發送更新數據其他需要注意的事項:
1.限制廣播數據的大小,最大為28byte + 10byte(專用于localNameKey),在廣播的字典里,只能傳入?CBAdvertisementDataLocalNameKey?and?CBAdvertisementDataServiceUUIDsKey類型的數據。
2.在不需要時,停止廣播
[myPeripheralManager stopAdvertising];3.對于敏感數據,進行配對驗證,對于需要訂閱的數據設置,訂閱權限。
myCharacteristic = [[CBMutableCharacteristic alloc]initWithType:myCharacteristicUUIDproperties:CBCharacteristicPropertyRead | CBCharacteristicPropertyNotifyvalue:nil permissions:CBAttributePermissionsReadable];emailCharacteristic = [[CBMutableCharacteristic alloc]initWithType:emailCharacteristicUUIDproperties:CBCharacteristicPropertyRead| CBCharacteristicPropertyNotifyEncryptionRequiredvalue:nil permissions:CBAttributePermissionsReadEncryptionRequired];總結
以上是生活随笔為你收集整理的iOS蓝牙开发(三)实现外设功能的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: iOS-蓝牙开发
- 下一篇: 麒麟官方应用商店下载地址