蓝牙app开发-iOS蓝牙通信.docx_第1页
蓝牙app开发-iOS蓝牙通信.docx_第2页
蓝牙app开发-iOS蓝牙通信.docx_第3页
蓝牙app开发-iOS蓝牙通信.docx_第4页
蓝牙app开发-iOS蓝牙通信.docx_第5页
已阅读5页,还剩7页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

蓝牙app开发-iOS蓝牙通信一、引言深圳酷点网是蓝牙手机app开发企业,蓝牙4.0是设备近距离通信的一种方便手段,在iPhone引入蓝牙4.0后,设备之间的通讯变得更加简单。相关的蓝牙操作由专门的CoreBluetooth.framework进行统一管理。通过蓝牙进行通讯交互分为两方,一方为中心设备central,一方为外设peripheral,外设通过广播的方式向外发送信息,中心设备检索到外设发的广播信息,可以进行配对连接,进而进行数据交互。二、中心设备CBCentralManagerCBCentralManager是管理中心设备的管理类,其中重要方法如下:/设置中心设备代理property(assign, nonatomic, nullable) id delegate;/中心设备当前状态property(readonly) CBCentralManagerState state;/中心设备是否正在扫描property(readonly) BOOL isScanning NS_AVAILABLE(NA, 9_0);其中state是一个枚举,有关蓝牙是否可用的状态如下:typedef NS_ENUM(NSInteger, CBCentralManagerState) /状态未知 CBCentralManagerStateUnknown = 0, /连接断开 即将重置 CBCentralManagerStateResetting, /该平台不支持蓝牙 CBCentralManagerStateUnsupported, /未授权蓝牙使用 CBCentralManagerStateUnauthorized, /蓝牙关闭 CBCentralManagerStatePoweredOff, /蓝牙正常开启 CBCentralManagerStatePoweredOn,;下面这些方法用于初始化管理中心:/初始化方法/设置的代理需要遵守CBCentralManagerDelegate协议/queue可以设置蓝牙扫描的线程 传入nil则为在主线程中进行- (instancetype)initWithDelegate:(nullable id)delegate queue:(nullable dispatch_queue_t)queue;/此方法同上 在options字典中用于进行一些管理中心的初始化属性设置/字典中支持的键值如下 /roucheng/*NSString * const CBCentralManagerOptionShowPowerAlertKey 对应一个NSNumber类型的bool值,用于设置是否在关闭蓝牙时弹出用户提示NSString * const CBCentralManagerOptionRestoreIdentifierKey 对应一个NSString对象,设置管理中心的标识符ID*/- (instancetype)initWithDelegate:(nullable id)delegate queue:(nullable dispatch_queue_t)queue options:(nullable NSDictionary *)options;/根据获取所有已知设备- (NSArray *)retrievePeripheralsWithIdentifiers:(NSArray *)identifiers;/根据服务id获取所有连接的设备 - (NSArray *)retrieveConnectedPeripheralsWithServices:(NSArray *)serviceUUIDs;在初始化管理中心完成后,会回调代理中的如下方法,我们必须实现如下方法:/这个方法中可以获取到管理中心的状态- (void)centralManagerDidUpdateState:(CBCentralManager *)central;如果上面方法中管理中心状态为蓝牙可用,可以通过下面方法开启扫描外设:/serviceUUIDs用于扫描一个特点ID的外设 options用于设置一些扫描属性 键值如下/*/是否允许重复扫描 对应NSNumber的bool值,默认为NO,会自动去重NSString *const CBCentralManagerScanOptionAllowDuplicatesKey;/要扫描的设备UUID 数组 对应NSArray NSString *const CBCentralManagerScanOptionSolicitedServiceUUIDsKey;*/- (void)scanForPeripheralsWithServices:(nullable NSArray *)serviceUUIDs options:(nullable NSDictionary *)options;/停止扫描外设- (void)stopScan;扫描的结果会在如下代理方法中回掉:/peripheral 扫描到的外设/advertisementData是外设发送的广播数据/RSSI 是信号强度 /roucheng/- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI;扫描到外设后,通过下面方法可以连接一个外设:/*options中可以设置一些连接设备的初始属性键值如下/对应NSNumber的bool值,设置当外设连接后是否弹出一个警告NSString *const CBConnectPeripheralOptionNotifyOnConnectionKey;/对应NSNumber的bool值,设置当外设断开连接后是否弹出一个警告NSString *const CBConnectPeripheralOptionNotifyOnDisconnectionKey;/对应NSNumber的bool值,设置当外设暂停连接后是否弹出一个警告 /roucheng/NSString *const CBConnectPeripheralOptionNotifyOnNotificationKey;*/- (void)connectPeripheral:(CBPeripheral *)peripheral options:(nullable NSDictionary *)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中会传入如下键值对 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 *)dict;三、外设CBPeripheralManager从上面我们知道,中心设备是用来扫描周围的外设,两台设备的通讯中,必须有一个充当中心设备,一个充当外设,外设是由CBPeripheralManager进行管理,主要方法如下: 1 /设置外设管理中心代理 2 property(assign, nonatomic, nullable) id delegate; 3 /外设状态 枚举如中心设备 4 property(readonly) CBPeripheralManagerState state; 5 /是否正在发送广播 6 property(readonly) BOOL isAdvertising; 7 /用户的授权状态 8 + (CBPeripheralManagerAuthorizationStatus)authorizationStatus; 9 /初始化并设置代理 参数的具体含义与中心设备管理中心10 - (instancetype)initWithDelegate:(nullable id)delegate11 queue:(nullable dispatch_queue_t);12 - (instancetype)initWithDelegate:(nullable id)delegate13 queue:(nullable dispatch_queue_t)queue14 options:(nullable NSDictionary *)options;15 /开始发送广播 何问起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 *)advertisementData;36 /停止发送广播37 - (void)stopAdvertising;38 /设置一个连接的具体central设备的延时 枚举如下39 /*40 typedef NS_ENUM(NSInteger, CBPeripheralManagerConnectionLatency) 41 CBPeripheralManagerConnectionLatencyLow = 0,42 CBPeripheralManagerConnectionLatencyMedium,43 CBPeripheralManagerConnectionLatencyHigh44 NS_ENUM_AVAILABLE(NA, 6_0);45 */46 - (void)setDesiredConnectionLatency:(CBPeripheralManagerConnectionLatency)latency forCentral:(CBCentral *)central;47 /添加一个服务 /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 *)centrals;外设代理的相关方法如下: 1 /这个方法是必须实现的 状态可用后可以发送广播 2 - (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral; 3 /连接回复时调用的方法 和centralManager类似 4 - (void)peripheralManager:(CBPeripheralManager *)peripheral willRestoreState:(NSDictionary *)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 /收到读请求时触发的方法 何问起 14 - (void)peripheralManager:(CBPeripheralManager *)peripheral didReceiveReadRequest:(CBATTRequest *)request;15 /收到写请求时触发的方法16 - (void)peripheralManager:(CBPeripheralManager *)peripheral didReceiveWriteRequests:(NSArray *)requests;17 /外设准备更新特征值时调用的方法18 - (void)peripheralManagerIsReadyToUpdateSubscribers:(CBPeripheralManager *)peripheral;四、中心设备与外设对象CBCentral与CBPeripheral上面介绍了中心设备管理类与外设管理类,这些类用于将设备连接建立起来,器具的数据交换的服务和一些信息则是在对应的设备对象中。1、中心设备 CBCentral属性与方法/设备UUIDproperty(readonly, nonatomic) NSUUID *identifier;/中心设备最大接收的数据长度property(readonly, nonatomic) NSUInteger maximumUpdateValueLength;2、外设CAPeripheral属性与方法外设对象要比中心对象复杂的多,当centralManager连接到外设后,需要通过外设对象的代理方法进行数据交互,其中主要方法属性如下: 1 /设置代理 2 property(assign, nonatomic, nullable) id delegate; 3 /外设name 4 property(retain, readonly, nullable) NSString *name; 5 /信号强度 /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 *services;19 /获取当前信号强度20 - (void)readRSSI;21 /根据服务UUID寻找服务对象22 - (void)discoverServices:(nullable NSArray *)serviceUUIDs;23 /在服务对象UUID数组中寻找特定服务24 - (void)discoverIncludedServices:(nullable NSArray *)includedServiceUUIDs forService:(CBService *)service;25 /在一个服务中寻找特征值26 - (void)discoverCharacteristics:(nullable NSArray *)characteristicUUIDs forService:(CBService *)service;27 /从一个特征中读取数据28 - (void)readValueForCharacteristic:(CBCharacteristic *)characteristic;29 /写数据的最大长度 何问起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外设的代理方法如下: 1 /外设名称更改时回调的方法 2 - (void)peripheralDidUpdateName:(CBPeripheral *)peripheral NS_AVAILABLE(NA, 6_0); 3 /外设服务变化时回调的方法 4 - (void)peripheral:(CBPeripheral *)peripheral didModifyServices:(NSArray *)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 /读取信号强度回调的方法 柯乐义 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;/包含的自服务 /roucheng/property(retain, readonly, nullable) NSArray *includedServices;/服务中的特征值property(retain, readonly, nullable) NSArray *characteristics;六、服务的特征值CBCharacteristic通过绑定服务中的特征值来进行数据的读写操作,其中属性如下:/对应的服务对象property(assign, readonly, nonatomic) CBService *service;/特征值的属性 枚举如下/*typedef NS_O

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论