本篇博文阐述如何开发Ble蓝牙。在蓝牙中的一些常见服务,扫描,以及链接;
主蓝牙类文件.h 主蓝牙类文件.m UUID文件 蓝牙列表展示的文件一:引入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