IOSCoreBluetooth学习笔记_第1页
IOSCoreBluetooth学习笔记_第2页
IOSCoreBluetooth学习笔记_第3页
IOSCoreBluetooth学习笔记_第4页
免费预览已结束,剩余1页可下载查看

下载本文档

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

文档简介

1、学习好资料欢迎下载一、 概览Bluetooth设备在通讯中的角色定位Central Device (中心设备)Peripheral Device (周边设备)Central and peripheral devicesClientServerWants data |Has dataCentralPeripheralAdvertising and discoveryCentralAdPeripheral二、 执行通用的Central角色任务步骤:i. Starting Up a Central Manager(启动一个 Central Manager)myCe ntralMa nager =CB

2、CentralManager alloc initWithDelegate:self queue:nil options:nil;2. Discovering Peripheral Devices That Are Advertising 发现正在广播的周边设备)第一、 通过Service UUID寻找设备myCentralManager scanForPeripheralsWithServicesnil options:nil;其中 scanForPeripheralsWithServices:nil 在 App 实际应用中会使用 CBUUID objects 取匹配 Service第二、判

3、断是否发现设备- (void)ce ntralMa nager:(CBCe ntralMa nager *)ce ntral didDiscoverPeripheral(CBPeripheral peripheral advertiseme ntData:(NSDict ionary *)advertiseme ntData RSSI:(NSNumber *)RSSI NSLog(Discovered %, peripheral. name);第三、找到设备后,停止搜索myCe ntralMa nager stopScai);NSLog(Sca nning stopped);Discovei3

4、. Connecting to a Peripheral Device After You ve(连接到发现的周边设备)第一、请求连接到周边设备myCe ntralMa nager connectPeripheral:peripheral optionsinjl第二、判断是否连接成功-(void)ce ntralMa nager:(CBCe ntralMa nager *)ce ntral didConnectPeripherat(CBPeripheral *)peripheral NSLog(Peripheral conn ected);在通讯前,需设置 peripheral.delegat

5、e = self;re4. Discovering the Services of a Peripheral That YouCo nn ected To发现已连接的周边设备的Services)第一、发现周边设备所有广播的 Servicesperipheral discoverServices:nil;当然如果nil替换成特定的 UUID,则可以找到特定的 Services第二、判断是否发现Services-(void)peripheral:(CBPeripheral *)peripheraldidDiscoverServices:(NSError *)error for (CBService

6、 *service in peripheral.services) NSLog(Discovered service %, service);5. Discovering the Characteristics of a Service 发现一个服务的第一、 发现Services的所有特性NSLog(Discovering characteristics for service %, interestingService); peripheral discoverCharacteristicsnil forService:i nterest in gService;实际应用中,可使用具体 UU

7、ID获取相应的特性第二、判断是否发现Service的特性-(void)peripheral:(CBPeripheral peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error for (CBCharacteristic *characteristic in service.characteristics) NSLog(Discovered characteristic %, characteristic);6. Retrieving the Value of a Ch

8、aracteristic 取得一个特性值)(1) Readi ng the Value of a Characteristic (读取一个特性值)第一、制定特性,calling: readValueForCharacteristic:方法NSLog(Readi ng value for characteristic %, i nteresti ngCharacteristic);peripheral readValueForCharacteristic:i nteresti ngCharacteristic;第二、判断是否读到特性值-(void)peripheral:(CBPeripheral

9、 *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error NSData *data = characteristic.value;/ parse the data as n eeded注意:不是所有的特性值都是可读的(readable。通过访问CBCharacteristicPropertyRead 可以知道特性值是否可读。如果一个特性的值不可读,使用 peripheral:didUpdateValueForCharacteristic:erro

10、r:就会返回 一个错误。(2) Subscribing to a Characteristic(订制Vad个特性值)尽管通过readValueForCharacteristic:方法能够得到特性值,但是对于一个变化的特性值就不是很 有效了。大多数的特性值是变化的,比如一个心率监测应用,如果需要得到特性值,就需要 通过预定的方法获得。当预定了一个特性值,当值改变时,就会收到设备发出的通知。第一、设置通知值peripheral setNotifyValue:YES forCharacteristic:interestingCharacteristic;其中第一个参数一定要为YES第二、判断是否订制

11、特性值成功-(void)peripheral:(CBPeripheral peripheral didUpdateNotificationStateForCharacteristic(CBCharacteristiccharacteristic error:(NSError *)error if (error) NSLog(Error changing notification state: %, error localizedDescriptio n);注意:不是所有特性都是允许订制值的。可以参考CBCharacteristic Class Refere nce。第三、取得订制的特性值如果成

12、功订制的特性值,当值有变化时,设备就会通知APP。每次值变化,callperipheral:didUpdateValueForCharacteristic:error:方法取得具体的值。7. Writing the Value of a Characteristic (写入一个特性值)第一、写特性值如果一个特性的值是可写的,可通过call writeValue:forCharacteristic:type: 方法实现NSLog(Writ ing value for characteristic %, i nteresti ngCharacteristic);peripheral writeVa

13、lue:dataToWrite forCharacteristic:i nteresti ngCharacteristic type:CBCharacteristicWriteWithRespo nse;当试图写一个特性值时,需要指定想要执行的写类型。在上面例子中,类型是CBCharacteristicWriteWithResponse ,这个类型表明:设备会让 APP知道是否写成功。更多类 型可以查看 CBCharacteristicWriteType IN CBPeripheral Class Reference第二、判断是否写入成功-(void)peripheral:(CBPeripheral *)peripheraldidWriteValueForCharacteristic:(CBChara

温馨提示

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

评论

0/150

提交评论