本篇博文阐述如何开发Ble蓝牙。在蓝牙中的一些常见服务,扫描,以及链接;
主蓝牙类文件.h主蓝牙类文件.mUUID文件蓝牙列表展示的文件一:引入Ble蓝牙的框架<CoreBluetooth/CoreBluetooth.h>
BuleHelp.h
#import <Foundation/Foundation.h>//导入蓝牙框架#import <CoreBluetooth/CoreBluetooth.h>#import "DeviceModel.h"#import "Constants.h"#import "CommonUserDefaults.h"#import "CommonUtil.h"#import "TempDB.h"#define COMMAND_PACKET_MIN_SIZE 7@interface BlueHelp : NSObject//蓝牙的设备搜索显示在列表中@property (nonatomic,strong) NSMutableArray <CBPeripheral*>*periperals;//连接peripheral@property(nonatomic,strong) CBPeripheral *peripheral;//连接peripheral@property(nonatomic,strong) CBPeripheral *selectperipheral;//中心管理者@property (nonatomic,strong) CBCentralManager *centerManager;@property (nonatomic,strong) DeviceModel *deviceModel;//设备列表@property (nonatomic,strong) NSMutableArray *deviceList;@property (nonatomic,strong) NSMutableArray *commandarray;//是否进行ota升级@property (nonatomic) BOol isOta;@property (nonatomic) BOol isWritePacketDataSuccess;@property (strong,nonatomic) Nsstring * checkSumType;/*! * @property isApplicationValID * * @discussion flag used to check whether the application writing is success * */@property (nonatomic) BOol isApplicationValID;/*! * @property checkSum * * @discussion checkSum received from the device for writing a single row * */@property (assign) uint8_t checkSum;/*! * @property startRowNumber * * @discussion Device flash start row number * */@property (nonatomic) int startRowNumber;/*! * @property endRowNumber * * @discussion Device flash end row number * */@property (nonatomic) int endRowNumber;/*! * @property siliconIDString * * @discussion siliconID from the device response * */@property (strong,nonatomic) Nsstring *siliconIDString;/*! * @property siliconRevString * * @discussion silicon rev from the device response * */@property (strong,nonatomic) Nsstring *siliconRevString;//是否发送数据@property (nonatomic) BOol isSendData;@property (strong,nonatomic) CommonUtil *commonUtil;@property (strong,nonatomic) TempDB *tempDB;@property (strong,nonatomic) NSDate *currentDate;//时间格式化@property(strong,nonatomic) NSDateFormatter *dateformatter;@property(strong,nonatomic) Nsstring *devicename;//@property (nonatomic,strong) NSUserDefaults *userDefaults;@property (nonatomic,strong) CommonUserDefaults *userDefaults;//发送温度数据@property (nonatomic,strong) CBCharacteristic *sendtempcharateristic;//发送OTA数据@property (nonatomic,strong) CBCharacteristic *sendotacharateristic;//高/低温度数据//@property (nonatomic,strong) CBCharacteristic *sendhighalarmcharateristic;////@property (nonatomic,strong) CBCharacteristic *sendlowalarmcharateristic;//ota@property (nonatomic,strong) CBCharacteristic *senddfucharateristic;//发送字符串'CR'清除机子上的最大值(3个字节)@property (nonatomic,strong) CBCharacteristic *senddcrstrateristic;//发送字符串'PD'机子关机(3个字节)@property (nonatomic,strong) CBCharacteristic *senddoutstrateristic;//静音@property (strong,nonatomic) CBCharacteristic *sendmutealarmcharateristic;//calset@property(strong,nonatomic) CBCharacteristic *sendcalsetcharateristic;//intervaltime@property(strong,nonatomic) CBCharacteristic *sendintervaltimecharateristic;//alarmswitch@property(strong,nonatomic) CBCharacteristic *sendalarmswitchcharateristic;//tempunit@property(strong,nonatomic) CBCharacteristic *sendtempunitcharateristic;@property(strong,nonatomic) CBCharacteristic *sendlowalarmswitchcharateristic;///<===============方法区块=======================>+ (ID)sharedManager;-(NSMutableArray *)getDeviceList;-(NSMutableArray *)getPeriperalList;-(voID)startScan;//连接蓝牙-(voID)contentBlue:(int) row;//断开蓝牙-(voID)disContentBle;//断开ota的蓝牙连接-(voID)disContentOtable;//温度符号-(voID)writeTempunit:(Nsstring *)value;//写入报警开关-(voID)writealarmSwitch:(Nsstring *)value;//写入mute alarm-(voID)writeMutealarm:(Nsstring *)value;//写入CR CLERVulE-(voID)writeClearCR:(Nsstring *)value;//写入interval time-(voID)writeIntervalTime:(Nsstring *)value;//写入cal set-(voID)writeCalSet:(Nsstring *)value;//写入设备的开关按钮-(voID)writeBluePD:(Nsstring *)value;//写入低温报警//-(voID)writeLowalarm:(Nsstring *)value;//////写入高温报警//-(voID)writeHighAlarm:(Nsstring *)value;//OTA固件升级 又称为DFU-(voID)writeUpdateOTA:(Nsstring*)value;-(voID)writeBlueOTA:(Nsstring *)value;-(voID)wirteBlueOTAData:(NSData *)value;-(voID)writeLowalarmSwitch:(Nsstring *)value;-(voID) discovercharacteristicsWithCompletionHandler:(voID (^) (BOol success,NSError *error)) handler;-(voID)updateValueForCharacteristicWithCompletionHandler:(voID (^) (BOol success,ID command,NSError *error)) handler;-(voID) stopUpdate;-(voID) setCheckSumType:(Nsstring *) type;-(NSData *) createCommandPacketWithCommand:(uint8_t)commandCode dataLength:(unsigned short)dataLength data:(NSDictionary *)packetDataDictionary;-(voID) writeValuetocharacteristicWithData:(NSData *)data bootLoaderCommandCode:(unsigned short)commandCode;/* * 停止扫描 */-(voID)stopScan;//是否是第一次连接设备@property(assign,nonatomic) BOol isconnected;//当前的时间@property(nonatomic,assign) long currentTime;@property(nonatomic,strong) Nsstring *macAddre;@property(nonatomic,strong) Nsstring *macname;-(voID)disMainOtable;@end
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<Nsstring *,ID> *)advertisementData RSSI:(NSNumber *)RSS{ //判断如果数组中不包含当前扫描到的外部设备才保存 if (![_periperals containsObject:peripheral]){ //包扣3种设备 都需要去搜索 if([peripheral.name containsstring:@"TMW012BT"]||[peripheral.name containsstring:@"OTA"]){ NSLog(@"DATA: %@",advertisementData); NSArray *keys = [advertisementData allKeys]; for (int i = 0; i < [keys count]; ++i) { ID key = [keys objectAtIndex: i]; Nsstring *keyname = (Nsstring *) key; NSData *value = [advertisementData objectForKey: key]; if([keyname isEqualToString:@"kCBAdvDataManufacturerData"]){ NSLog(@"value is %@",value); Nsstring *result = [self convertDataToHexStr:value]; NSLog(@"reslut is %@",result); if(result!=nil&&result.length>4){ Nsstring *d = [result substringFromIndex:4]; NSLog(@"D IS %@",d); // 小写 //Nsstring *lower = [test lowercaseString]; // 大写 Nsstring *upper = [d uppercaseString]; _macAddre = [Nsstring stringWithFormat:@"S/N:%@",upper]; } } if([keyname isEqualToString:@"kCBAdvDataLocalname"]){ Nsstring *aStr= (Nsstring*)value; NSLog(@"astr is %@",aStr); _macname = aStr; } } _deviceModel = [DeviceModel new]; _deviceModel.devicename = _macname; //蓝牙地址 _deviceModel.deviceAddre = _macAddre; int RSSi = [RSS intValue]; Nsstring *range = [Nsstring stringWithFormat:@"%d",RSSi]; Nsstring *rs = [Nsstring stringWithFormat:@"RSSI %@dBm",range]; //添加到里面 [self.periperals addobject:peripheral]; _deviceModel.deviceRSSi = rs; [_deviceList addobject:_deviceModel]; } }}
//解析广播里面的数据-(voID)getAdvertisementData:(NSDictionary<Nsstring *,ID> *) advertisementData{ NSArray *keys = [advertisementData allKeys]; for (int i = 0; i < [keys count]; ++i) { ID key = [keys objectAtIndex: i]; Nsstring *keyname = (Nsstring *) key; NSObject *value = [advertisementData objectForKey: key]; if([keyname isEqualToString:@"kCBAdvDataManufacturerData"]){ printf(" key: %s\n",[keyname cStringUsingEnCoding: NSUTF8StringEnCoding]); Nsstring *values = (Nsstring *) value; NSLog(@"values is %@",values); } }}
/* * 连接蓝牙设备给外部调用的方法 * 传入的是相对应的行数 */-(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]; }}
此篇文章阐述到这,基本BLE蓝牙开发实现都详细的描述了。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
总结以上是内存溢出为你收集整理的IOS Ble蓝牙开发实现方法全部内容,希望文章能够帮你解决IOS Ble蓝牙开发实现方法所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)