导语:
最近几天在做一个关于蓝牙与血压计和血氧仪交互方面的东西,刚开始使用的是babybluetooth(但封装的内容我不会使用,于是就自己写了一个工具类)。在此带来一些关于蓝牙开发的分享。
1.蓝牙基础知识
CoreBluetooth框架的核心是peripheral(外设)和central(中心),发起连接的是 central,被连接的设备为 peripheral。在移动端开发中,我们通常使用的是中心模式。
2.中心模式的流程
- 建立中心
- 扫描外设(discover)
- 连接外设(connect)
- 扫描外设中的服务和特征(discover)
- 4.1 获取外设的 services
- 4.2 获取外设的 Characteristics,获取Characteristics的值,获 Characteristics的 Descriptor 和 Descriptor 的值
- 与外设做数据交互(explore and interact)
- 订阅 Characteristic 的通知
- 断开连接(disconnect)
3.具体实现代码
创建中心管理者
#import <CoreBluetooth/CoreBluetooth.h>
typedef NS_ENUM(NSInteger,BDBlueToothType){
BDBlueToothType_Oximeter = 1,//血氧仪
BDBlueToothType_Hamnatodynamometer //血压计
};
//蓝牙搜索到的设备数组
typedef void(^PeripheralBlock)(NSMutableArray *);
//读到的数据
typedef void(^ReadValueBlock)(NSString *);
@interface BDBlueToothHelper : NSObject<CBCentralManagerDelegate, CBPeripheralDelegate>
//外设
@property(nonatomic, strong) CBPeripheral* myPeripheral;
//中心管理工具
@property (nonatomic, strong) CBCentralManager* myCentralManager;
初始化开始扫描
self.myCentralManager = [[CBCentralManager alloc]initWithDelegate:self queue:nil options:nil];
必须实现的代理方法
//查看蓝牙服务
- (void)centralManagerDidUpdateState:(CBCentralManager *)central{
switch (central.state) {
case CBCentralManagerStatePoweredOn:
BDLog(@"蓝牙已打开, 请扫描外设!");
//搜索外设
[self.myCentralManager scanForPeripheralsWithServices:nil options:@{CBCentralManagerOptionShowPowerAlertKey:@YES}];
break;
case CBCentralManagerStatePoweredOff:
BDLog(@"蓝牙关闭...");
break;
default:
break;
}
}
#pragma mark 搜索到设备之后会调用代理方法
- (void)centralManager:(CBCentralManager *)central // 中心管理者
didDiscoverPeripheral:(CBPeripheral *)peripheral // 外设
advertisementData:(NSDictionary *)advertisementData // 外设携带的数据
RSSI:(NSNumber *)RSSI{ // 外设发出的蓝牙信号强度
BDLog(@"已发现 peripheral: %@ rssi: %@, name: %@ advertisementData: %@", peripheral, RSSI, peripheral.name, advertisementData);
//这里可以做一些过滤操作
if ([_myPeripherals containsObject:peripheral]) {
}else
{
//找到的设备必须持有它,否则CBCentralManager中也不会保存peripheral
[_myPeripherals addObject:peripheral];
}
//将搜索的设备回调给控制器的tableview使用,刷新表格
if (self.perlists) {
self.perlists(_myPeripherals);
}
}
建立连接
//self.myPeripheral在cell点击时的赋值
[self.myCentralManager connectPeripheral:self.myPeripheral options:nil];
一个主设备最多能连7个外设,每个外设最多只能给一个主设备连接,连接成功、失败、断开都会进入到相应的代理
#pragma mark 连接外设成功
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral{
BDLog(@"成功外设连接");
//设置外设的代理
[self.myPeripheral setDelegate:self];
//外设发现服务,传nil代表不过滤
// 这里会触发外设的代理方法 - (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
[self.myPeripheral discoverServices:nil];
}
#pragma mark 掉线时调用 丢失连接
- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"蓝牙连接已断开" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"关闭", nil];
[alert show];
[self closeConnect];
BDLog(@"丢失连接");
}
#pragma mark 连接外设失败
- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error{
BDLog(@"连接外设失败%@", error);
}
扫描服务和特征
#pragma mark 发现服务
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error{
BDLog(@"发现服务!");
for(CBService* s in peripheral.services){
NSLog(@"%d :服务 UUID: %@(%@)", i, s.UUID.data, s.UUID);
//扫描每个service的Characteristics,扫描到后会进入方法: -(void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
[peripheral discoverCharacteristics:nil forService:s];
[self.nServices addObject:s];
}
}
#pragma mark 发现外设服务里的特征的时候调用的代理方法
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error{
for(CBCharacteristic* c in service.characteristics){
BDLog(@"特征 UUID: %@ (%@)", c.UUID.data, c.UUID);
if (self.toothType == BDBlueToothType_Oximeter) {//血氧仪
if([c.UUID isEqual:[CBUUID UUIDWithString:@"FFF1"]]){
self.writeCharacteristic = c;
BDLog(@"找到WRITE : %@", c);
}else if([c.UUID isEqual:[CBUUID UUIDWithString:@"FFF4"]]){
self.readCharacteristic = c;
[self.myPeripheral setNotifyValue:YES forCharacteristic:c];
[self.myPeripheral readValueForCharacteristic:c];
NSLog(@"找到READ : %@", c);
}
}else if (self.toothType == BDBlueToothType_Hamnatodynamometer){//血压计
if ([c.UUID isEqual:[CBUUID UUIDWithString:@"FFE1"]]) {
BDLog(@"血压计 %zd - %@",c.properties,c.descriptors);
self.readCharacteristic = c;
self.writeCharacteristic = c;
[self.myPeripheral setNotifyValue:YES forCharacteristic:c];
[self.myPeripheral readValueForCharacteristic:c];
}
}
}
}
与外设进行数据交互
#pragma mark 获取外设发来的数据,不论是read和notify,获取数据都从这个方法中读取
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{
[peripheral readRSSI];
NSData* data = characteristic.value;
if (!data.length) {
return;
}
NSString* value = [self hexadecimalString:data];
if (self.toothType == BDBlueToothType_Oximeter) { //血氧仪
if([characteristic.UUID isEqual:[CBUUID UUIDWithString:@"FFF4"]]){
if (value.length) {
self.readValue(value);
}
// BDLog(@"characteristic : %@, data : %@, value : %@", characteristic, data, value);
}
}else if (self.toothType == BDBlueToothType_Hamnatodynamometer){ //血压计
if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:@"FFE1"]]) {
if (value.length) {
self.readValue(value);
}
// BDLog(@"characteristic : %@, data : %@, value : %@", characteristic, data, value);
}
}
}
//中心读取外设实时数据
- (void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{
if(error){
BDLog(@"Error changing notification state: %@", error.localizedDescription);
}
if(characteristic.isNotifying){
[peripheral readValueForCharacteristic:characteristic];
}else{
BDLog(@"Notification stopped on %@. Disconnting", characteristic);
[self.myCentralManager cancelPeripheralConnection:self.myPeripheral];
}
}
//向peripheral中写入数据
- (void)writeToPeripheral:(NSString *)data{
if(!_writeCharacteristic){
BDLog(@"writeCharacteristic is nil!");
return;
}
NSData* value = [self dataWithHexstring:data];
[_myPeripheral writeValue:value forCharacteristic:_writeCharacteristic type:CBCharacteristicWriteWithResponse];
}
//向peripheral中写入数据后的回调函数
- (void)peripheral:(CBPeripheral*)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{
if (error) {
BDLog(@"error %@",error);
}
BDLog(@"write value success : %@", characteristic);
}
用到的私有方法
//将传入的NSData类型转换成NSString并返回
- (NSString*)hexadecimalString:(NSData *)data{
NSString* result;
const unsigned char* dataBuffer = (const unsigned char*)[data bytes];
if(!dataBuffer){
return nil;
}
NSUInteger dataLength = [data length];
NSMutableString* hexString = [NSMutableString stringWithCapacity:(dataLength * 2)];
for(int i = 0; i < dataLength; i++){
[hexString appendString:[NSString stringWithFormat:@"%02lx", (unsigned long)dataBuffer[i]]];
}
result = [NSString stringWithString:hexString];
return result;
}
//将传入的NSString类型转换成NSData并返回
- (NSData*)dataWithHexstring:(NSString *)hexstring{
NSMutableData* data = [NSMutableData data];
int idx;
for(idx = 0; idx + 2 <= hexstring.length; idx += 2){
NSRange range = NSMakeRange(idx, 2);
NSString* hexStr = [hexstring substringWithRange:range];
NSScanner* scanner = [NSScanner scannerWithString:hexStr];
unsigned int intValue;
[scanner scanHexInt:&intValue];
[data appendBytes:&intValue length:1];
}
return data;
}
相关的蓝牙文档