IOS Ble藍牙開發實現方法

發布時間:2019-12-26 18:44 來源:互聯網 當前欄目:web技術類

本篇博文闡述如何開發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
        
 
  • 1、
  • 2、
  • 3、
  • 4、
  • 5、
  • 6、
  • 7、
  • 8、
  • 9、
  • 10、
  • 11、
  • 12、
  • 13、
  • 14、
  • 15、
  • 16、
  • 17、
  • 18、
  • 19、
  • 20、
  • 21、
  • 22、
  • 23、
  • 24、
  • 25、
  • 1、
  • 2、
  • 3、
  • 4、
  • 5、
  • 6、
  • 7、
  • 8、
  • 9、
  • 10、
  • 11、
  • 12、
  • 13、
  • 14、
  • 15、
  • 16、
  • 17、
  • 18、
  • 19、
  • 20、
  • 21、
  • 22、
  • 23、
  • 24、
  • 25、