ios设备app作为蓝牙外设端

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ios设备app作为蓝牙外设端相关的知识,希望对你有一定的参考价值。

参考技术A

苹果手机可以作为蓝牙外设端,被蓝牙中央端来扫描连接交互数据,实现模拟蓝牙外设硬件。通过阅读CoreBluetooth库,可以找到一个CBPeripheralManager的类,该类主要的作用就是允许你来管理发布services,把这些services广告给其他的设备。如果想详细了解该类的属性和方法,建议去看看CoreBluetooth/CBPeripheralManager.h。
下面来模拟一个简单的蓝牙外设端。

上面中,serviceUUID为给外设配置的服务的UUID,可以有多个,characteristicUUID为给service添加的特征的UUID,也可以有多个,可自定义可读可写等等属性.characteristicValue为characteristic的值,localName和serviceUUIDs分别为键CBAdvertisementDataLocalNameKey和CBAdvertisementDataServiceUUIDsKey对应的value。由于作为外设端时发送广播包,广播包中提供包含着两个键值对,如果有另外的键值对想要发送,例如CBAdvertisementDataServiceDataKey,会出现错误。

首先要导入蓝牙库
#import <CoreBluetooth/CoreBluetooth.h>
遵守协议

创建peripheralManager对象

创建了peripheralManager对象后会自动调用回调方法didUpdateState
- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral
if (peripheral.state != CBPeripheralManagerStatePowerOn)
return;


给外设配置服务和特征
- (void)configServiceAndCharacteristicForPeripheral
CBMutableCharacteristic *writeReadCharacteristic = [[CBMutableCharacteristic alloc] initWithType:characteristicUUID properties:CBCharacteristicPropertyWrite | CBCharacteristicPropertyRead value:nil permissions:CBAttributePermissionsReadEncryptionRequired | CBAttributePermissionsWriteEncryptionRequired];
CBMutableService *service = [[CBMutableService alloc] initWithType:serviceUUID primary:YES];
[service setCharacteristics:@[writeReadCharacteristic]];
[self.peripheralManager addService:service];

调用上面的方法时,会监听didAddService:
- (void)peripheralManager:(CBPeripheralManager *)peripheral didAddService:(CBService *)service error:(NSError *)error

开始广播

调用上 面方法时,会监听DidStartAdvertising:
- (void)peripheralManagerDidStartAdvertising:(CBPeripheralManager *)peripheral error:(NSError *)error

当中央端连接上了此设备并订阅了特征时会回调 didSubscribeToCharacteristic:
- (void)peripheralManager:(CBPeripheralManager *)peripheral central:(CBCentral *)central didSubscribeToCharacteristic:(CBCharacteristic *)characteristic
[self.peripheralManager updateValue:characteristicValue forCharacteristic:characteristic onSubscribedCentrals:nil];

当中央端取消订阅时会调用didUnsubscribeFromCharacteristic:
- (void)peripheralManager:(CBPeripheralManager *)peripheral central:(CBCentral *)central didUnsubscribeFromCharacteristic:(CBCharacteristic *)characteristic

当接收到中央端读的请求时会调用didReceiveReadRequest:
- (void)peripheralManager:(CBPeripheralManager *)peripheral didReceiveReadRequest:(CBATTRequest *)request
if (request.characteristic.properties & CBCharacteristicPropertyRead)
NSData *data = request.characteristic.value;
[request setValue:data];
[self.peripheralManager respondToRequest:request withResult:CBATTErrorSuccess];
else
[self.peripheralManager respondToRequest:request withResult:CBATTErrorReadNotPermitted];


当接收到中央端写的请求时会调用didReceiveWriteRequest:
- (void)peripheralManager:(CBPeripheralManager *)peripheral didReceiveWriteRequests:(NSArray<CBATTRequest *> *)requests
CBATTRequest *request = requests[0];
if (request.characteristic.properties & CBCharacteristicPropertyWrite)
CBMutableCharacteristic *c = (CBMutableCharacteristic *)request.characteristic;
c.value = request.value;
[self.peripheralManager respondToRequest:request withResult:CBATTErrorSuccess];
else
[self.peripheralManager respondToRequest:request withResult:CBATTErrorWriteNotPermitted];


手机app可以作为一个蓝牙外设端来模拟外设硬件,但广播包里的数据只能包含localName和serviceUUID,相对于外设硬件来说还是有一些不足之处。

IOS蓝牙设备数据回调的坑

1.手机端向蓝牙设备发送写入数据后会有一个回调方法

//写入数据后的回调

- (void)peripheral:(CBPeripheral *)peripheral

didWriteValueForCharacteristic:(CBCharacteristic *)

characteristic error:(nullable NSError *)error

因为我们这边的设备是血压仪,是向外设发送开始测量之后,然后把测量的数据返回来,所以要进到上面的方法里。

2.数据返回后会进入下面的方法

//获取的charateristic的值

-(void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{

self.UUID2data = [NSData data];

    self.UUID2data = characteristic.value;

    NSString *datastring = [self convertDataToHexStr:self.UUID2data];

    NSLog(@"datastring = %@",datastring);

    NSString *str = @"550f03";

    if ([datastring containsString:str]) {

       NSString *UUID2str = [datastring substringWithRange:NSMakeRange(6, 16)];

        self.UUID2TF.text = UUID2str;

    }

因为我们的数据包前面三位是固定的而且没什么用,所以要截取掉,这样有用的数据就出来了,还需要提一下characteristic.value是16进制的需要进行转换在展示

以上是关于ios设备app作为蓝牙外设端的主要内容,如果未能解决你的问题,请参考以下文章

iOS 连接蓝牙2.0的外设怎么实现

IOS蓝牙设备数据回调的坑

iOS中的蓝牙 CoreBluetooth蓝牙系列

iOS蓝牙接收外设数据自动中断

获取蓝牙低功耗外设的MAC地址

iOS设置蓝牙外设播放音频