蓝牙连接以及协议数据解析

来源:互联网 发布:留学低龄化的数据 编辑:程序博客网 时间:2024/05/16 04:48

1.声明属性以及引入相关库

NSMutableArray *pers;//这个必须有,用于记录搜索到的设备,没有导致连接不上

manager = [[CBCentralManager alloc]initWithDelegate:self queue:dispatch_get_main_queue()];


2.代理方法

-(void)centralManagerDidUpdateState:(CBCentralManager *)central{

switch (central.state) {

case CBCentralManagerStateUnknown:

NSLog(@">>>CBCentralManagerStateUnknown");

break;

case CBCentralManagerStateResetting:

NSLog(@">>>CBCentralManagerStateResetting");

break;

case CBCentralManagerStateUnsupported:

NSLog(@">>>CBCentralManagerStateUnsupported");

break;

case CBCentralManagerStateUnauthorized:

NSLog(@">>>CBCentralManagerStateUnauthorized");

break;

case CBCentralManagerStatePoweredOff:

NSLog(@">>>CBCentralManagerStatePoweredOff");

break;

case CBCentralManagerStatePoweredOn:{

NSLog(@">>>CBCentralManagerStatePoweredOn");

//开始扫描周围的外设

/*

第一个参数nil就是扫描周围所有的外设,扫描到外设后会进入

- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI;

*/

//            NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber  numberWithBool:YES], CBCentralManagerScanOptionAllowDuplicatesKey, nil];

[manager scanForPeripheralsWithServices:nil options:nil];

hudView.hidden=NO;

hudView.contentLab.text=@"开始扫描设备";

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

hudView.hidden=YES;

});

[manager scanForPeripheralsWithServices:nil options:nil];

break;

}

default:

break;

}

}

//扫描到设备会进入方法

-(void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI{

NSLog(@"当扫描到设备:%@",peripheral.name);

//    [peripherals addObject:peripheral];

//    [tableview reloadData];

//接下来可以连接设备

// 这个是我的设备名,根据实际情况,修改

if ([peripheral.name hasPrefix:@"ABG"]){

/*

一个主设备最多能连7个外设,每个外设最多只能给一个主设备连接,连接成功,失败,断开会进入各自的委托

- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral;//连接外设成功的委托

- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error;//外设连接失败的委托

- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error;//断开外设的委托

*/

//找到的设备必须持有它,否则CBCentralManager中也不会保存peripheral,那么CBPeripheralDelegate中的方法也不会被调用!!

manager.delegate=self;

[pers addObject:peripheral];

self.per=peripheral;

//连接设备

[manager connectPeripheral:peripheral options:nil];

hudView.hidden=NO;

hudView.contentLab.text=[NSString stringWithFormat:@"扫描到设备%@,并开始连接",peripheral.name];

}

}

//连接到Peripherals-成功

- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral

{

NSLog(@">>>连接到名称为(%@)的设备-成功",peripheral.name);

//设置的peripheral委托CBPeripheralDelegate

//@interface ViewController : UIViewController

[peripheral setDelegate:self];

//扫描外设Services,成功后会进入方法:-(void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error{

[peripheral discoverServices:nil];

//    self.nameLab.text=peripheral.name;

//    self.contactStateLab.text=@"链接中";

hudView.hidden=NO;

hudView.contentLab.text=[NSString stringWithFormat:@"链接设备%@成功,",peripheral.name];

}

//连接到Peripherals-失败

-(void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error

{

NSLog(@">>>连接到名称为(%@)的设备-失败,原因:%@",[peripheral name],[error localizedDescription]);

//自定义的mbprogress

hudView.hidden=NO;

hudView.contentLab.text=[NSString stringWithFormat:@"链接设备%@失败,",peripheral.name];

}

//Peripherals断开连接

- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error{

NSLog(@">>>外设连接断开连接 %@: %@\n", [peripheral name], [error localizedDescription]);

//    self.contactStateLab.text=@"链接断开";

//这句话确保了能保持连接,断了重新连接

[manager connectPeripheral:peripheral options:nil];

hudView.contentLab.text=[NSString stringWithFormat:@"设备%@链接断开",peripheral.name];

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

hudView.hidden=YES;

});

}

//扫描到Services

-(void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error{

//  NSLog(@">>>扫描到服务:%@",peripheral.services);

if (error)

{

NSLog(@">>>Discovered services for %@ with error: %@", peripheral.name, [error localizedDescription]);

return;

}

for (CBService *service in peripheral.services) {

NSLog(@"%@",service.UUID);

//扫描每个service的Characteristics,扫描到后会进入方法: -(void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error

/这个1808是我的设备的入口,需根据实际改变

if ([service.UUID.UUIDString isEqualToString:@"1808"]) {

[peripheral discoverCharacteristics:nil forService:service];

}

//        if ([service.UUID.UUIDString isEqualToString:@"180A"]) {

//            [peripheral discoverCharacteristics:nil forService:service];

//        }

//        if ([service.UUID.UUIDString isEqualToString:@"180F"]) {

//            [peripheral discoverCharacteristics:nil forService:service];

//        }

}

}

//扫描到Characteristics

-(void) peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error{

NSString *name=[NSString stringWithFormat:@"发现特征的服务:%@ (%@)",service.UUID.data ,service.UUID];

NSLog(@"%@",name);

for (CBCharacteristic *c in service.characteristics) {

//        [self updateLog:[NSString stringWithFormat:@"特征 UUID: %@ (%@)",c.UUID.data,c.UUID]];

NSLog(@"%@",c.UUID);

//        if ([c.UUID isEqual:[CBUUID UUIDWithString:@"FF01"]]) {

////            _writeCharacteristic = c;

//        }

//

if ([c.UUID isEqual:[CBUUID UUIDWithString:@"FFF4"]]) {

chara=c;

[peripheral readValueForCharacteristic:c];

[peripheral setNotifyValue:YES forCharacteristic:c];

}

//        if ([c.UUID isEqual:[CBUUID UUIDWithString:@"FF04"]]) {

//            [peripheral readValueForCharacteristic:c];

//        }

//

//        if ([c.UUID isEqual:[CBUUID UUIDWithString:@"FF05"]]) {

//            [peripheral readValueForCharacteristic:c];

//            [peripheral setNotifyValue:YES forCharacteristic:c];

//        }

//

//        if ([c.UUID isEqual:[CBUUID UUIDWithString:@"FFA1"]]) {

//            [peripheral readRSSI];

//        }

//        [_nCharacteristics addObject:c];

}

}

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

//打印出characteristic的UUID和值

//!注意,value的类型是NSData,具体开发时,会根据外设协议制定的方式去解析数据

//该蓝牙协议是返回数值是ascii码

//    NSLog(@"characteristic uuid:%@  value:%@",characteristic.UUID,characteristic.value);

//    NSData * data = characteristic.value;

//    //将接收到的十六进制数据 转成 十六进制字符串

//    Byte *byte    = (Byte *)[data bytes];

//    // 取出其中用用的两位

//    Byte b[]      = {byte[1],byte[2]};

//    // 在转化成NSData

//    NSData *adata = [[NSData alloc] initWithBytes:b length:sizeof(b)];

//    NSLog(@"adata = %@", adata);

//    // 转化成字符串

//    NSString *str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];

//    NSLog(@"%@",str);

NSData * data = characteristic.value;

Byte * resultByte = (Byte *)[data bytes];

//

for(int i=0;i<[data length];i++){

printf("testByteFF02[%d] = %d\n",i,resultByte[i]);

//        NSString *string = [NSString stringWithFormat:@"%c",resultByte[i]];

//        printf("testByteFF02[%d] = %@\n",i,string);

if (resultByte[3]==0x00&&resultByte[4]==0x00&&resultByte[5]==0x00&&resultByte[6]==0x01) {

NSLog(@"停止测试");

hudView.hidden=NO;

hudView.contentLab.text=@"停止测量";

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

hudView.hidden=YES;

});

}

if (resultByte[7]==0xcf) {

NSLog(@"停止测试");

hudView.hidden=NO;

hudView.contentLab.text=@"停止测量";

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

hudView.hidden=YES;

});

}

int asc=resultByte[i];

if (asc==0xfe) {

int num;

if (resultByte[2]==0) {

num=resultByte[1];

}else{

num=resultByte[2];

}

NSString *aString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

NSLog(@"测量中,当前值:%d",num);

if(num!=254){

hudView.hidden=NO;

hudView.contentLab.text=[NSString stringWithFormat:@"当前测量值%d",num];

}

//            self.textview.text=[NSString stringWithFormat:@"测量中,当前值:%d",num];

}

if (asc==0xfd) {

//            Byte  byte1=resultByte[2];

//            int num1=[self hBytesToInt:];

[manager cancelPeripheralConnection:peripheral];

UInt16 asc1=resultByte[3];

UInt16 asc2=resultByte[4];

NSString *str= [NSString stringWithFormat:@"%c%c",asc2,asc1];

NSString *num= [NSString stringWithFormat:@"%ld",strtoul([str UTF8String],0,16)];

//            NSLog(@"测量结束,收缩压%@",str);

NSLog(@"测量结束,收缩压%@",num);

UInt16 asc3=resultByte[5];

UInt16 asc4=resultByte[6];

NSString *str2= [NSString stringWithFormat:@"%c%c",asc4,asc3];

NSString *num2= [NSString stringWithFormat:@"%ld",strtoul([str2 UTF8String],0,16)];

//            NSLog(@"测量结束,收缩压%@",str);

NSLog(@"测量结束,舒张压%@",num2);

UInt16 asc5=resultByte[7];

UInt16 asc6=resultByte[8];

NSString *str3= [NSString stringWithFormat:@"%c%c",asc6,asc5];

NSString *num3= [NSString stringWithFormat:@"%ld",strtoul([str3 UTF8String],0,16)];

//            NSLog(@"测量结束,收缩压%@",str);

if ([num isEqualToString:@"0"]||[num2 isEqualToString:@"0"]||[num3 isEqualToString:@"0"]) {

hudView.hidden=NO;

hudView.contentLab.text=@"测量异常";

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

hudView.hidden=YES;

});

return;

}

if ([num isEqualToString:hasGaoya]&&[num2 isEqualToString:hasDiya]&&[num3 isEqualToString:hasXinglu]) {

return;

}else{

hasGaoya=num;

hasDiya=num2;

hasXinglu=num3;

hudView.hidden=NO;

hudView.contentLab.text=[NSString stringWithFormat:@"测量结束,高压:%@;低压:%@,心率:%@",num,num2,num3];

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

hudView.hidden=YES;

});

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];

HomeBloodSCell *bloodScell=[mytab cellForRowAtIndexPath:indexPath];

bloodScell.gaoyaLab.text=num;

bloodScell.diyaLab.text=num2;

bloodScell.xingluLab.text=num3;

NSLog(@"测量结束,心率%@",num3);

SelfDataModel *selfdata=[SelfDataModel returnModelBySelectFMDB];

NSDate *date=[NSDate date];

NSDateFormatter *form=[[NSDateFormatter alloc]init];

[form setDateFormat:@"YYYY-MM-dd HH:mm:ss"];

NSString *datestr=[form stringFromDate:date ];

NSString *url=[[HJInterfaceManager sharedInstance]uploadXueya];

NSMutableDictionary *mdic=[[NSMutableDictionary alloc]init];

mdic[@"userId"]=selfdata.idNum;

mdic[@"dbp"]=num2;

mdic[@"sbp"]=num;

mdic[@"pulse"]=num3;

mdic[@"measureTime"]=datestr;

[HJHttpManager PostRequestWithUrl:url param:mdic finish:^(NSData *data) {

NSDictionary *dic=(NSDictionary *)data;

if([dic[@"status"] isEqualToString:@"S"]){

[MBProgressHUD showSuccess:@"数据上传成功"];

HomeBloodSCell *cell=[mytab cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];

NSString *str=dic[@"value"][@"measurements"];

cell.statusLab.text=str;

if ([str isEqualToString:@"偏低"]) {

cell.statusLab.textColor=[UIColor colorWithHexString:@"2aa764"];

//        self.pointImage.transform=CGAffineTransformMakeRotation(M_PI/6);

}

if ([str isEqualToString:@"理想"]) {

cell.statusLab.textColor=[UIColor colorWithHexString:@"62c342"];

cell.statusImage.transform=CGAffineTransformMakeRotation(M_PI/2-M_PI/6);

}

if ([str isEqualToString:@"正常"]) {

cell.statusLab.textColor=[UIColor colorWithHexString:@"c9e93c"];

cell.statusImage.transform=CGAffineTransformMakeRotation(M_PI/6*5-M_PI/6);

}

if ([str isEqualToString:@"轻度"]) {

cell.statusLab.textColor=[UIColor colorWithHexString:@"ffc102"];

cell.statusImage.transform=CGAffineTransformMakeRotation(M_PI/6*7-M_PI/6);

}

if ([str isEqualToString:@"中度"]) {

cell.statusLab.textColor=[UIColor colorWithHexString:@"f78758"];

cell.statusImage.transform=CGAffineTransformMakeRotation(M_PI/2*3-M_PI/6);

}

if ([str isEqualToString:@"偏高"]) {

cell.statusLab.textColor=[UIColor colorWithHexString:@"e74c3c"];

cell.statusImage.transform=CGAffineTransformMakeRotation(M_PI/6*11-M_PI/6);

}

}else{

[MBProgressHUD showError:dic[@"message"]];

}

CDLog(@"请求成功");

} failed:^(NSError *error) {

[MBProgressHUD showError:@"请求失败"];

}];

}

//            self.textview.text=[NSString stringWithFormat:@"测量结束,收缩压%@\n\n测量结束,舒张压%@\n\n测量结束,心率%@",num,num2,num3];

}

}

//    NSString *productNumber = [reciveString substringWithRange:NSMakeRange(7, 17)];

//    [productDefault setObject:productNumber forKey:@"productNumber"];

//    [productDefault synchronize];

}

//中心读取外设实时数据

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

if (error) {

NSLog(@"Error changing notification state: %@", error.localizedDescription);

}

// Notification has started

if (characteristic.isNotifying) {

NSString *reciveString = [NSString stringWithFormat:@"%@", [self hexadecimalString:characteristic.value]];

NSUserDefaults *productDefault = [NSUserDefaults standardUserDefaults];

//    NSString *productNumber = [reciveString substringWithRange:NSMakeRange(7, 17)];

[peripheral readValueForCharacteristic:characteristic];

} else { // Notification has stopped

// so disconnect from the peripheral

NSLog(@"Notification stopped on %@.  Disconnecting", characteristic);

//        [self updateLog:[NSString stringWithFormat:@"Notification stopped on %@.  Disconnecting", characteristic]];

[manager cancelPeripheralConnection:peripheral];

}

}

//搜索到Characteristic的Descriptors

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

//打印出Characteristic和他的Descriptors

NSLog(@"characteristic uuid:%@",characteristic.UUID);

for (CBDescriptor *d in characteristic.descriptors) {

NSLog(@"Descriptor uuid:%@",d.UUID);

}

}

//获取到Descriptors的值

-(void)peripheral:(CBPeripheral *)peripheral didUpdateValueForDescriptor:(CBDescriptor *)descriptor error:(NSError *)error{

//打印出DescriptorsUUID 和value

//这个descriptor都是对于characteristic的描述,一般都是字符串,所以这里我们转换成字符串去解析

NSLog(@"characteristic uuid:%@  value:%@",[NSString stringWithFormat:@"%@",descriptor.UUID],descriptor.value);

}

#pragma mark 写数据后回调

- (void)peripheral:(CBPeripheral *)peripheral

didWriteValueForCharacteristic:(CBCharacteristic *)characteristic

error:(NSError *)error {

if (error) {

NSLog(@"Error writing characteristic value: %@",

[error localizedDescription]);

return;

}

NSLog(@"写入%@成功",characteristic);

[peripheral readValueForCharacteristic:characteristic];

}

//向设备写入数据让其停止

-(IBAction)stopBtnDown:(id)sender{

//    char byte[] = {0xFD,0xFD,0xFB,0xFB,0x05,0x0C,0x0D,0x0A};

Byte byte[] = {0XFD,0XFD,0XFB,0XFB,0X05,0X0C,0X0D,0X0A};

NSData * data = [NSData dataWithBytes:byte length:8];

[self.per writeValue:data forCharacteristic:chara type:CBCharacteristicWriteWithResponse];

//    NSString *str=@"[0xFD,0xFD,0xFB,0xFB,0x05,0x0C,0x0D,0x0A]";

//    NSData *data=[str dataUsingEncoding:NSUTF8StringEncoding];

//    [self.per writeValue:data forCharacteristic:chara type:CBCharacteristicWriteWithResponse];

}

-(IBAction)showList:(id)sender{

Byte byte[] = {0xFD,0xFD,0x07,0x07,0x07,0x07,0x0D,0x0A};

NSData * data = [NSData dataWithBytes:byte length:8];

[self.per writeValue:data forCharacteristic:chara type:CBCharacteristicWriteWithResponse];

}

1 0
原创粉丝点击