扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
这篇文章将为大家详细讲解有关IOS如何开发实现Ble蓝牙,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
公司主营业务:网站建设、成都网站设计、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。创新互联建站是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。创新互联建站推出大柴旦免费做网站回馈大家。
主蓝牙类文件.h主蓝牙类文件.mUUID文件蓝牙列表展示的文件
一:引入Ble蓝牙的框架
BuleHelp.h
#import
BuleHelp.m
//程序运行后,会自动调用的检查蓝牙的方法 并扫描蓝牙的方法- (void)centralManagerDidUpdateState:(CBCentralManager *)central{ if ([central state] == CBCentralManagerStatePoweredOff) { NSLog(@"CoreBluetooth BLE hardware is powered off"); } else if ([central state] == CBCentralManagerStatePoweredOn) { NSLog(@"CoreBluetooth BLE hardware is powered on and ready"); [self startScan]; } else if ([central state] == CBCentralManagerStateUnauthorized) { NSLog(@"CoreBluetooth BLE state is unauthorized"); } else if ([central state] == CBCentralManagerStateUnknown) { NSLog(@"CoreBluetooth BLE state is unknown"); } else if ([central state] == CBCentralManagerStateUnsupported) { NSLog(@"CoreBluetooth BLE hardware is unsupported on this platform"); }}
/* * 程序运行的时候开始扫描 */-(void)startScan{ _periperals = [[NSMutableArray alloc] init]; _deviceList = [[NSMutableArray alloc] init]; //2.利用中心设备扫描外部设备 [_centerManager scanForPeripheralsWithServices:nil options:nil];}/* * 停止扫描 */-(void)stopScan{ [_centerManager stopScan];}
/* * 设备中可以包含 Device addre地址 放在 key:kCBAdvDataManufacturerData中既可以得到 * 1.硬件开发工程师 把地址写入到设备的厂家信息中。 */- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary
//解析广播里面的数据-(void)getAdvertisementData:(NSDictionary
/* * 连接蓝牙设备给外部调用的方法 * 传入的是相对应的行数 */-(void)contentBlue:(int) row{ [_centerManager connectPeripheral:_periperals[row] options:nil];}-(void)disContentBle{ //关键的断开蓝牙 通知也要停止掉 if((_peripheral!=nil && _sendtempcharateristic!=nil)){ [_peripheral setNotifyValue:NO forCharacteristic:_sendtempcharateristic]; [self disconnectPeripheral:_peripheral]; }}/* * 断开ota的 连接 */-(void)disContentOtaBle{ if(_peripheral!=nil && _sendotacharateristic!=nil){ [_peripheral setNotifyValue:NO forCharacteristic:_sendotacharateristic]; [self disconnectPeripheral:_peripheral]; }}-(void)disMainOtaBle{ if(_peripheral!=nil){ [self disconnectPeripheral:_peripheral]; }}- (void) disconnectPeripheral:(CBPeripheral*)peripheral{ [_centerManager cancelPeripheralConnection:peripheral];}
//连接成功- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral{ NSLog(@"连接成功"); if(peripheral!=nil){ //停止扫描 这个用于自动连接的时候 [_centerManager stopScan]; //设备名称 _deviceName = peripheral.name; _selectperipheral = peripheral; peripheral.delegate = self; //再去扫描服务 [peripheral discoverServices:nil]; }}
//连接失败- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(nullable NSError *)error{ NSLog(@"连接失败,失败原因:%@",error); NSString *disContentBlue = @"discontentblue"; NSDictionary *blueDiscontent = [NSDictionary dictionaryWithObject:disContentBlue forKey:@"disconnect"]; //发送广播 连接失败 [[NSNotificationCenter defaultCenter] postNotificationName:@"disNofiction" object:nil userInfo:blueDiscontent];}
//断开连接- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error{ NSLog(@"断开连接"); NSString *disContentBlue = @"discontentblue"; //_blueiscon = @"Disconnect"; NSDictionary *blueDiscontent = [NSDictionary dictionaryWithObject:disContentBlue forKey:@"disconnect"]; //发送广播 连接失败 [[NSNotificationCenter defaultCenter] postNotificationName:@"disNofiction" object:nil userInfo:blueDiscontent];}
//只要扫描到服务就会调用,其中的外设就是服务所在的外设- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error{ if (error){ NSLog(@"扫描服务出现错误,错误原因:%@",error); }else{ //获取外设中所扫描到的服务 for (CBService *service in peripheral.services){ //拿到需要扫描的服务,例如FFF0 比如打印温度数据 //把所有的service打印出来 //从需要的服务中查找需要的特征 //从peripheral的services中扫描特征 [peripheral discoverCharacteristics:nil forService:service]; } }}
//只要扫描到特征就会调用,其中的外设和服务就是特征所在的外设和服务- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(nonnull CBService *)service error:(nullable NSError *)error{ if(error){ NSLog(@"扫描特征出现错误,错误原因:%@",error); }else{ //遍历特征,拿到需要的特征进行处理 for (CBCharacteristic *characteristic in service.characteristics){ NSLog(@"characteristic is %@",characteristic.UUID); //如果是温度数据处理 if([characteristic.UUID isEqual:BOOT_TEMPVALUE_UUID]){ //将全部的特征信息打印出来 _isconnected = true; _peripheral = peripheral; _sendtempcharateristic = characteristic; [peripheral setNotifyValue:YES forCharacteristic:characteristic]; } //如果是ota进行ota升级 else if([characteristic.UUID isEqual:BOOT_OTA_WIRTE_UUID]){ _peripheral = peripheral; _sendotacharateristic = characteristic; //设置通知 [peripheral setNotifyValue:YES forCharacteristic:characteristic]; _isOta = TRUE; [_userDefaults saveOta:_isOta]; //[_ setBool:_isOta forKey:@"isOTA"]; NSString *s = @"OTA"; NSDictionary *tempOta = [NSDictionary dictionaryWithObject:s forKey:@"ota"]; [[NSNotificationCenter defaultCenter] postNotificationName:@"tempOTA" object:nil userInfo:tempOta]; } //CAL else if([characteristic.UUID isEqual:BOOT_CAL_UUID]){ _sendcalsetcharateristic = characteristic; [peripheral setNotifyValue:YES forCharacteristic:characteristic]; } //interval time else if([characteristic.UUID isEqual:BOOT_INTERVALTIME_UUID]){ _sendintervaltimecharateristic = characteristic; [peripheral setNotifyValue:YES forCharacteristic:characteristic]; } //Tempunit else if([characteristic.UUID isEqual:BOOT_TEMPUNIT_UUID]){ _sendtempunitcharateristic = characteristic; [peripheral setNotifyValue:YES forCharacteristic:characteristic]; } //DFU else if([characteristic.UUID isEqual:BOOT_DFU_UUID]){ _senddfucharateristic = characteristic; [peripheral setNotifyValue:YES forCharacteristic:characteristic]; } //发送字符串'CR'清除机子上的最大值(3个字节) else if([characteristic.UUID isEqual:BOOT_CRSTRING_UUID]){ _senddcrstrateristic = characteristic; [peripheral setNotifyValue:YES forCharacteristic:characteristic]; } //发送字符串'PD'机子关机(3个字节) else if([characteristic.UUID isEqual:BOOT_OUTSTRING_UUID]){ _senddoutstrateristic = characteristic; [peripheral setNotifyValue:YES forCharacteristic:characteristic]; } else{ } } }}
/* 设置通知 */-(void)notifyCharacteristic:(CBPeripheral *)peripheral characteristic:(CBCharacteristic *)characteristic{ [peripheral setNotifyValue:YES forCharacteristic:characteristic]; [_centerManager stopScan];}/* 取消通知 */-(void)cancelNotifyCharacteristic:(CBPeripheral *)peripheral characteristic:(CBCharacteristic *)characteristic{ [peripheral setNotifyValue:NO forCharacteristic:characteristic];}
/* 接收数据解析 */- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{ //是否为Ota if(!self.isSendOta){ if(self.readtempcharater == characteristic){ NSString *tmpData = [self.cmUtil getTempTMWData:characteristic]; if(tmpData!=nil){ //开始处理数据 NSArray *data = [tmpData componentsSeparatedByString:@","]; TmpModel *tp = [[TmpModel alloc] init]; tp.tmp = data[0]; tp.max = data[1]; tp.unit = data[2]; //电量 tp.bat = data[3]; NSDictionary *tempDict = [NSDictionary dictionaryWithObject:tp forKey:@"tempData"]; [[NSNotificationCenter defaultCenter] postNotificationName:@"tempNofiction" object:nil userInfo:tempDict]; if([tp.bat isEqual:@"1"]){ //低电量广播 NSDictionary *LowDict = [NSDictionary dictionaryWithObject:tp forKey:@"lowData"]; [[NSNotificationCenter defaultCenter] postNotificationName:@"lowNofiction" object:nil userInfo:LowDict]; } } } }
//======================写入数据区块========================///* 写入发送时间 */-(void)writeIntervalTime:(NSString *)value periperalData:(CBPeripheral*)periperal{ if(value!=nil&&periperal!=nil&&self.sendtimecharater!=nil){ [self writeData:value forCharacteristic:self.sendtimecharater periperalData:periperal]; }}/* 写入温度单位 */-(void)writeTmpUnit:(NSString*)value periperalData:(CBPeripheral*)periperal{ if(value!=nil&&periperal!=nil&&self.sendunitcharater!=nil){ [self writeData:value forCharacteristic:self.sendunitcharater periperalData:periperal]; }}/* 写入Cal值 */-(void)writeCalValue:(NSString *)value periperalData:(CBPeripheral*)periperal{ if(value!=nil&&periperal!=nil&&self.sendcalcharater!=nil){ [self writeData:value forCharacteristic:self.sendcalcharater periperalData:periperal]; }}/* 发送Ota使得设备进入Ota */-(void)writeDfuValue:(NSString *)value periperalData:(CBPeripheral*)periperal{ if(value!=nil&&periperal!=nil&&self.senddfucharater!=nil){ [self writeData:value forCharacteristic:self.senddfucharater periperalData:periperal]; }}/* 发送Max Temp实现清零选项 */-(void)writeClearValue:(NSString *)value periperalData:(CBPeripheral*)periperal{ if(value!=nil&&periperal!=nil&&self.sendclearcharater!=nil){ [self writeData:value forCharacteristic:self.sendclearcharater periperalData:periperal]; }}/* 发送关机选项实现远程开关机 */-(void)writeCloseValue:(NSString *)value periperalData:(CBPeripheral*)periperal{ if(value!=nil&&periperal!=nil&&self.sendclosecharater!=nil){ [self writeData:value forCharacteristic:self.sendclosecharater periperalData:periperal]; }}/* 所有写入的数据处理 */-(void)writeData:(NSString *)value forCharacteristic:(CBCharacteristic *)characteristic periperalData:(CBPeripheral*)periperal{ NSData *data = [value dataUsingEncoding:NSUTF8StringEncoding]; if(characteristic.properties & CBCharacteristicPropertyWriteWithoutResponse) { [periperal writeValue:data forCharacteristic:characteristic type:CBCharacteristicWriteWithoutResponse]; }else { [periperal writeValue:data forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse]; }}
关于“IOS如何开发实现Ble蓝牙”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流