700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > iOS的蓝牙连接 数据接收及发送

iOS的蓝牙连接 数据接收及发送

时间:2021-05-11 22:14:54

相关推荐

iOS的蓝牙连接 数据接收及发送

说明:苹果官方开发文档中涉及一些名词:Central(中心设备)、Peripheral(外围设备)、advertising(广告)、Services(服务)、Characteristic(特征)等,我都用括号内的中文进行翻译,并且不做详细解释,可能存在误导,请谅解。

目录:

步骤1.建立一个Central Manager实例进行蓝牙管理

步骤2.搜索外围设备

步骤3.连接外围设备

步骤4.获得外围设备的服务

步骤5.获得服务的特征

步奏6.从外围设备读数据(直接读取和订阅两种方法)

步骤7.给外围设备发送数据

步骤一.开启Central Manager

CBCentralManager 是Core Bluetooth的一个对象,代表一个本地中心设备,在使用任何蓝牙传输前,你必须给CBCentralManager实例分配内存和初始化。可以通过CBCentralManager类的initWithDelegate:queue:options: 方法:

view source print ?1.<p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Courier; color: rgb(102, 102, 102); -webkit-text-stroke-color: rgb(102, 102, 102); -webkit-text-stroke-width: initial;">myCentralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:nil];</p>

在这个例子中,self会被设置为接收所有中心设备角色的事件的代理,设置dispatch queue为nil后,central manager会通过主线程来处理这些事件。ViewController需要实现CBCentralManagerDelegate,CBPeripheralDelegate这两个代理

当创建了一个central manager,会回调代理的centralManagerDidUpdateState:方法,你必须实现这个代理方法来确定中心设备是否支持BLE以及是否可用。

步骤二.搜索正在广告的外围设备

中心设备的第一个任务是搜索你的APP可以连接的外围设备,正如上一篇文章中提到的,广告(advertising)是外围设备使其被周围设备发现的主要方法,你可以通过CBCentralManager类的scanForPeripheralsWithServices:options: 方法,搜索任何正在广告的外围设备:

view source print ?1.[myCentralManager scanForPeripheralsWithServices:nil options:nil];

你调用了scanForPeripheralsWithServices:options: 方法来搜索可连接的外围设备后,central manager 在每次搜索到一个外围设备都会回调其代理的centralManager:didDiscoverPeripheral:advertisementData:RSSI: 方法。任何被搜索到的外围设备都以CBPeripheral类的方式返回。像下面的代码,你可以实现这个代理方法来罗列出所有被搜索到的蓝牙设备:

view source print ?01.- (void)centralManager:(CBCentralManager *)central02.03.didDiscoverPeripheral:(CBPeripheral *)peripheral04.05.advertisementData:(NSDictionary *)advertisementData06.07.RSSI:(NSNumber *)RSSI {08.09.10.11.NSLog(@"Discovered %@", peripheral.name);12.13....

当你已经找到了一个你需要连接的外围设备,停止搜索其它设备来节省电量。

view source print ?1.[myCentralManager stopScan];2.NSLog(@"Scanning stopped”);

步骤三.连接一个你搜索到并希望连接的外围设备

在你搜索到一个外围设备,并且它广告了你需要的服务,你可以请求和这个外围设备连接,通过调用 CBCentralManager类的connectPeripheral:options:方法,简而言之,调用这个方法并且说明你需要连接的外围设备:

view source print ?1.[myCentralManager connectPeripheral:peripheral options:nil];

假设连接成功了,central manager 会回调其代理方法centralManager:didConnectPeripheral: ,你可以实现这个方法来打印“外围设备连接”:

view source print ?01.- (void)centralManager:(CBCentralManager *)central02.03.didConnectPeripheral:(CBPeripheral *)peripheral {04.05.06.07.NSLog(@"Peripheral connected");08.09....view source print ?1.

在你开始和外围设备交互之前,你应该设置外围设备的代理以确保它能接收合适的回调,像这样

view source print ?1.peripheral.delegate = self;

步骤四.搜索你已经连接的外围设备的服务

在你与外围设备建立连接之后,你可以开始勘察它的数据。第一步是勘察外围设备提供了什么服务,因为外围设备广告的数据有大小限制,你或许会发现外围设备提供比它广告的还有更多服务。你可以通过 CBPeripheral类的discoverServices: 方法来发现一个外围设备提供的所有服务:

view source print ?1.[peripheral discoverServices:nil];

当发现了具体的服务,外围设备(已经连接的 CBPeripheral类)会回调其代理的peripheral:didDiscoverServices: 方法。Core Bluetooth 建立CBService 类的array数组——存储外围设备的每一个服务。像下面的代码,你可以实现这个代理方法来获得发现的服务列表:

view source print ?01.- (void)peripheral:(CBPeripheral *)peripheral02.03.didDiscoverServices:(NSError *)error {04.05.06.07.for(CBService *service in peripheral.services) {08.09.NSLog(@"Discovered service %@", service);10.11....12.13.}14.15.

五.搜索服务的特征

假定你已经找到了你需要的服务,下一步就是探索服务的所有“特征”(characteristics),搜索一个服务的所有特征只要调用 CBPeripheral类的discoverCharacteristics:forService:方法,参数为具体的服务:

view source print ?1.NSLog(@"Discovering characteristics for service %@", interestingService);2.3.[peripheral discoverCharacteristics:nil forService:interestingService];

当发现了指定服务的特征,外围设备会回调peripheral:didDiscoverCharacteristicsForService:error:代理方法。Core Bluetooth建立存储CBCharacteristic实例的array数组———每一个都代表一个发现了的特征。下面的示例就是实现这个代理方法来简单地打印每个发现了的特征:

view source print ?01.- (void)peripheral:(CBPeripheral *)peripheral02.03.didDiscoverCharacteristicsForService:(CBService *)service04.05.error:(NSError *)error {06.07.08.09.for(CBCharacteristic *characteristic in service.characteristics) {10.11.NSLog(@"Discovered characteristic %@", characteristic);12.13....14.15.}16.17....

六.取得特征的值(就是从外围设备取数据)

一个特征包含一个代表外围设备服务的简单值,例如,有一个健康温度计服务,这个服务有一个温度测量特征,而这个特征有一个摄氏温度的值,你可以直接读取或者订阅这个特征来取得这个特征对应的值。

直接读取特征的值

你得到你希望得到的服务的某个特征后,你可以通过调用CBPeripheral类的readValueForCharacteristic:方法来读取这个特征对应的值,参数为需要读取的特征:

view source print ?1.NSLog(@"Reading value for characteristic %@", interestingCharacteristic);2.3.[peripheral readValueForCharacteristic:interestingCharacteristic];

当你试图去读一个特征对应的值,外围设备会回调它的代理方法

peripheral:didUpdateValueForCharacteristic:error:去取值,如果值成功返回,你可以通过特征的value属性来获得它:

view source print ?01.- (void)peripheral:(CBPeripheral *)peripheral02.03.didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic04.05.error:(NSError *)error {06.07.08.09.NSData *data = characteristic.value;10.11.// parse the data as needed12.13....

订阅一个特征的值

在某些使用情况下,通过 readValueForCharacteristic: 读取一个特征的值会很有效率,但是这不是获得值改变的最有效率方法,对于大部分特征值的改变——例如,你在给定时间的心率,你应该通过订阅来获得它。当你订阅了一个特征的值,你可以在值改变的时候接收到通知。你可以通过 CBPeripheral类的setNotifyValue:forCharacteristic: 方法来订阅你需要的特征,参数为YES,以及需要订阅的特征:

view source print ?1.[peripheral setNotifyValue:YES forCharacteristic:interestingCharacteristic];

当你试图去订阅(或取消订阅)一个特征时,外围设备会调用 它的代理的peripheral:didUpdateNotificationStateForCharacteristic:error:方法,如果订阅请求失败,你可以通过实现这个方法来获得错误原因:

view source print ?01.- (void)peripheral:(CBPeripheral *)peripheral02.03.didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic04.05.error:(NSError *)error {06.07.08.09.if(error) {10.11.NSLog(@"Error changing notification state: %@",12.13.[error localizedDescription]);14.15.}16.17....

当你成功订阅了某个特征,在特征的值改变时,外围设备会通知你的app,每次值改变,外围设备会调用 其代理的peripheral:didUpdateValueForCharacteristic:error:方法。你可以通过实现这个方法来获得改变的值,这个方法和上面直接读取时回调的方法一样。

步骤七.写值到特征中(就是发数据给外围设备)

在某些使用情况下,需要写数据到特征中,这是可行的。例如,你的app与一个BLE电子温度调节器交互,你或许需要提供给调节器一个值来设定房间的温度,如果特征的值可以被写,你可以通过 CBPeripheral类的writeValue:forCharacteristic:type:方法把数据(NSData的实例)写到特征的值里:

view source print ?1.NSLog(@"Writing value for characteristic %@", interestingCharacteristic);2.3.[peripheral writeValue:dataToWrite forCharacteristic:interestingCharacteristic4.5.type:CBCharacteristicWriteWithResponse];

当你试图去写特征的值时,你需要说明你需要用哪种类型的写的方法。上面的例子中,写的方法为CBCharacteristicWriteWithResponse,用这个方法,外围设备会让你的app知道写操作是否成功(就是)。更多写的方法请看 CBPeripheral Class Reference里面的CBCharacteristicWriteType枚举。

使用CBCharacteristicWriteWithResponse方法给外围设备写数据时,会回调 其代理的peripheral:didWriteValueForCharacteristic:error:方法。如果写数据失败,可以通过这个方法找到失败的原因。像这样:

view source print ?01.- (void)peripheral:(CBPeripheral *)peripheral02.03.didWriteValueForCharacteristic:(CBCharacteristic *)characteristic04.05.error:(NSError *)error {06.07.08.09.if(error) {10.11.NSLog(@"Error writing characteristic value: %@",12.13.[error localizedDescription]);14.15.}16.17....

简单写了个测试demo,见下图,输入框为待发送的文本,Label为接收到的文本,我的蓝牙测试模块是返回我发送的数据。

这个demo只连接指定蓝牙名字的蓝牙设备,测试时,请将第十行的HMSoft改为自己的外围设备名称,这个demo比较水,每次viewdidload后就去搜索蓝牙设备并连接,不便于测试,自己可以加断点调试。

另外由于我的测试的蓝牙设备不支持CBCharacteristicWriteWithResponse,发送数据时都返回没有权限的错误,所以我用的CBCharacteristicWriteWithoutResponse。成功后不会回调didWriteValueForCharacteristic方法

下载demo :/dolacmeng/Bluetooth

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。