You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
379 lines
14 KiB
379 lines
14 KiB
// |
|
// BLETools.m |
|
// Lookfit |
|
// |
|
// Created by lemo. on 2020/3/21. |
|
// Copyright © 2020 Sheldon. All rights reserved. |
|
// |
|
|
|
#import "BLETools.h" |
|
#import "NSString+methods.h" |
|
#import "JZLocationConverter.h" |
|
|
|
@implementation BLETools |
|
|
|
/** |
|
将时间戳按指定格式时间输出 |
|
|
|
@param spString 秒 |
|
@param formatStr 指定格式 |
|
|
|
*/ |
|
+ (NSString*)nsdateToTime:(long long)spString formatStr:(NSString *)formatStr |
|
{ |
|
NSDate *date = [NSDate dateWithTimeIntervalSince1970:spString]; |
|
NSTimeZone *zone = [NSTimeZone systemTimeZone]; // 获得系统的时区 |
|
NSTimeInterval time = [zone secondsFromGMTForDate:date]; |
|
[date dateByAddingTimeInterval:time]; |
|
NSDateFormatter *dateFormat=[[NSDateFormatter alloc]init]; |
|
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh"]; |
|
[dateFormat setLocale:locale]; |
|
[dateFormat setCalendar: [[NSCalendar alloc] |
|
initWithCalendarIdentifier:NSCalendarIdentifierGregorian]]; |
|
[dateFormat setDateFormat:formatStr]; |
|
NSString* string=[dateFormat stringFromDate:date]; |
|
|
|
return string; |
|
} |
|
|
|
/** |
|
将指定格式时间转NSDate输出 |
|
*/ |
|
+ (NSDate *)nsdateToformatStr:(NSString *)formatStr { |
|
NSDateFormatter *dateFormat=[[NSDateFormatter alloc]init]; |
|
[dateFormat setCalendar: [[NSCalendar alloc] |
|
initWithCalendarIdentifier:NSCalendarIdentifierGregorian]]; |
|
NSInteger formatStrCount = [formatStr componentsSeparatedByString:@"-"].count; |
|
NSArray *timeArr = @[@"yyyy-",@"MM-",@"dd-",@"HH-",@"mm-",@"ss-"]; |
|
if (formatStrCount && formatStrCount <= 6) { |
|
NSMutableString *format = [[NSMutableString alloc] init]; |
|
for (NSInteger i = 0; i < formatStrCount; i ++) { |
|
[format appendString:timeArr[i]]; |
|
} |
|
[dateFormat setDateFormat:[format substringToIndex:format.length - 1]]; |
|
NSDate *date = [dateFormat dateFromString:formatStr]; |
|
return date; |
|
} |
|
return nil; |
|
} |
|
|
|
/** |
|
将字符串变成数组 |
|
|
|
@param valueStr 字符串 |
|
@return 返回一个数组 |
|
*/ |
|
+ (NSArray *)stringByArrayData:(NSString *)valueStr |
|
{ |
|
NSString *tempStr = [valueStr stringByReplacingOccurrencesOfString:@"|" withString:@"-"]; |
|
NSString *tempstring = [tempStr stringByReplacingOccurrencesOfString:@":" withString:@"-"]; |
|
NSString *spacestring = [tempstring stringByReplacingOccurrencesOfString:@" " withString:@"-"]; |
|
NSArray *valueArray = [spacestring componentsSeparatedByString:@"-"]; |
|
return valueArray; |
|
} |
|
|
|
+ (NSString *)getCurrentLanguage |
|
{ |
|
NSArray *languages = [NSLocale preferredLanguages]; |
|
NSString *currentLanguage = [languages objectAtIndex:0]; |
|
return currentLanguage; |
|
} |
|
|
|
|
|
/** 获取当前是星期几 把当前日期变成星期 */ |
|
+ (NSInteger)getNowWeekday { |
|
NSDate *date = [NSDate date]; |
|
NSCalendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; // 指定日历的算法 |
|
NSDateComponents *comps = [calendar components:NSCalendarUnitWeekday fromDate:date]; |
|
NSInteger week = [comps weekday]; |
|
return week; |
|
} |
|
|
|
/** |
|
若是中文直接转拼音 |
|
*/ |
|
+ (NSString *)transformPinyin:(NSString *)chinese |
|
{ |
|
if (![BLETools isChinese:chinese]) { |
|
return chinese; |
|
} |
|
NSMutableString *pinyin = [chinese mutableCopy]; |
|
CFStringTransform((__bridge CFMutableStringRef)pinyin, NULL, kCFStringTransformMandarinLatin, NO); |
|
CFStringTransform((__bridge CFMutableStringRef)pinyin, NULL, kCFStringTransformStripCombiningMarks, NO); |
|
//去掉空格 |
|
return [[NSString stringWithFormat:@"%@",pinyin] stringByReplacingOccurrencesOfString:@" " withString:@""]; |
|
} |
|
|
|
/** |
|
检测是否是中文 |
|
*/ |
|
+(BOOL)isChinese:(NSString *)str |
|
{ |
|
NSString *match = @"(^[\u4e00-\u9fa5]+$)"; |
|
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF matches %@", match]; |
|
return [predicate evaluateWithObject:str]; |
|
} |
|
|
|
/** |
|
两个时间差(分钟) |
|
|
|
@param startTime 开始时间 |
|
@param endTime 结束时间 |
|
@return 分钟 (字符串) |
|
*/ |
|
+(NSString *)compareStartTime:(NSString *)startTime EndTime:(NSString *)endTime |
|
{ |
|
//同一天内 如果开始时间为9:01 结束时间为21:01 那么这条数据就是无效的 |
|
//不在一天内如果开始时间为 21:02 结束时间为22:01 那么这条数据也是无效的 |
|
if ((![[startTime substringWithRange:NSMakeRange(0, 10)] isEqualToString:[endTime substringWithRange:NSMakeRange(0, 10)]]&&[endTime substringWithRange:NSMakeRange(11, 2)].integerValue >= 21) |
|
|| ([[startTime substringWithRange:NSMakeRange(0, 10)] isEqualToString:[endTime substringWithRange:NSMakeRange(0, 10)]]&& [startTime substringWithRange:NSMakeRange(11, 2)].integerValue < 21 && [startTime substringWithRange:NSMakeRange(11, 2)].integerValue >= 9)) { |
|
return @"0"; |
|
} |
|
|
|
else{ |
|
long long start = [NSString changeTimeToMinutes:startTime]; |
|
long long end = [NSString changeTimeToMinutes:endTime]; |
|
NSString *compareStr = [NSString stringWithFormat:@"%lld",(end-start)/60]; |
|
return compareStr; |
|
} |
|
} |
|
|
|
/** |
|
本地语言转换 |
|
|
|
@param language 系统语言 |
|
*/ |
|
+(NSString*)laguageTransform:(NSString*)language |
|
{ |
|
NSDictionary * dict =@{@"zh-Hans-CN":@"zh_CN",// zh-Hans-CN, 简体中文 |
|
@"zh-Hant-HK":@"zh_TW",// zh-Hant-HK, 香港繁体 |
|
@"zh-Hant-HK":@"zh_TW",// zh-Hant-CN, 繁体中文 |
|
@"zh-Hant-MO":@"zh_TW",// zh-Hant-MO, 繁体中文(澳门) |
|
@"zh-Hant-TW":@"zh_TW",// zh-Hant-TW 繁体中文(台湾) |
|
@"en-CN":@"en_US", // en-CN, 英文 |
|
@"en-AU":@"en_US", // en-AU, 英文(澳大利亚) |
|
@"en-IN":@"en_US", // en-IN, 英文(印度) |
|
@"en-GB":@"en_GB", // en-GB, 英文(英国) |
|
@"es-CN":@"es_SA", // es-CN, 西班牙 |
|
@"es-419":@"es_SA", // es-419, 西班牙(拉丁美洲) |
|
@"es-MX":@"es_SA", // es-MX, 西班牙(墨西哥) |
|
@"de-AT":@"de_DE", // de-AT, 德文(奥地利) |
|
@"de-CN":@"de_DE", // de-CN, 德文 |
|
@"de-DE":@"de_DE", // de-DE, 德文(德国) |
|
@"de-CH":@"de_DE", // de-CH, 德文(瑞士) |
|
@"fr-BE":@"fr_FR", // fr-BE, 法文(比利时) |
|
@"fr-FR":@"fr_FR", // fr-FR, 法文(法国) |
|
@"fr-CH":@"fr_FR", // fr-CH, 法文(瑞士) |
|
@"fr-CA":@"fr_FR", // fr-CA, 法文(加拿大) |
|
@"fr-CN":@"fr_FR", // fr-CN, 法文 |
|
@"pt-PT":@"pt_PT", // pt-PT, 葡萄牙文 |
|
@"pt-BR":@"pt_BZ", // pt-BR, 葡萄牙(巴西) |
|
@"ko-CN":@"ko", // ko-CN, 韩文 |
|
@"ja-CN":@"ja_JP", // ja-CN, 日文 |
|
@"ru-CN":@"ru_RU", // ru-CN 俄文 |
|
@"ro-CN":@"ro_RO", // ro-CN, 罗马尼亚 |
|
@"it_CN":@"it_IT", // it-CN, 意大利 |
|
@"el_CN":@"el_GR", // el-CN, 希腊文 |
|
@"tr-CN":@"tr-TR" // tr-CN, 土耳其 |
|
}; |
|
NSArray * dictKeyArray = [dict allKeys]; |
|
if ([dictKeyArray containsObject:language]) |
|
{ |
|
return dict[language]; |
|
}; |
|
return nil; |
|
} |
|
|
|
/** |
|
获取时间戳 |
|
|
|
@param date 日期 |
|
@param hour 小时 |
|
@param minute 分钟 |
|
@param seconds 秒 |
|
@return 时间戳 |
|
*/ |
|
+ (NSInteger)zeroOfSecondsTimestampWithDate:(NSDate *)date hour:(NSInteger)hour minute:(NSInteger)minute seconds:(NSInteger)seconds |
|
{ |
|
NSCalendar *calendar = [NSCalendar currentCalendar]; |
|
NSDateComponents *components = [calendar components:NSUIntegerMax fromDate:[NSDate date]]; |
|
components.hour = hour; |
|
components.minute = minute; |
|
components.second = seconds; |
|
NSTimeInterval ts = (NSInteger)[[calendar dateFromComponents:components] timeIntervalSince1970]; |
|
return ts; |
|
} |
|
|
|
/** |
|
获取时间戳 |
|
|
|
@param time 2017-01-01-12-12-12格式的时间 |
|
@return 时间戳字符串 |
|
*/ |
|
+ (NSString *)getTimestampWithTime:(NSString *)time{ |
|
|
|
if (!time.length) { |
|
return @""; |
|
} |
|
|
|
NSArray *arr = [time componentsSeparatedByString:@"-"]; |
|
NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init]; |
|
[inputFormatter setCalendar: [[NSCalendar alloc] |
|
initWithCalendarIdentifier:NSCalendarIdentifierGregorian]]; |
|
// [inputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]]; |
|
switch (arr.count) { |
|
case 3: |
|
[inputFormatter setDateFormat:@"yyyy-MM-dd"]; |
|
break; |
|
|
|
case 4: |
|
[inputFormatter setDateFormat:@"yyyy-MM-dd-HH"]; |
|
break; |
|
|
|
case 5: |
|
[inputFormatter setDateFormat:@"yyyy-MM-dd-HH-mm"]; |
|
break; |
|
|
|
|
|
case 6: |
|
[inputFormatter setDateFormat:@"yyyy-MM-dd-HH-mm-ss"]; |
|
break; |
|
|
|
|
|
default: |
|
break; |
|
} |
|
|
|
NSDate* inputDate = [inputFormatter dateFromString:time]; |
|
NSLog(@"date = %@", inputDate); |
|
|
|
NSString *timestamp = [NSString stringWithFormat:@"%ld",(long)[inputDate timeIntervalSince1970]]; |
|
return timestamp; |
|
} |
|
|
|
+ (int)heartRateGrade:(NSInteger)heartRate |
|
{ |
|
if (heartRate >= 150 && heartRate<= 180) { |
|
return 3; |
|
}else if (heartRate >= 120 && heartRate< 150){ |
|
return 2; |
|
}else if (heartRate < 120){ |
|
return 1; |
|
} |
|
return 1; |
|
} |
|
|
|
/** |
|
将周期字符串转成对应日期字符串(1100000-周一,周二) |
|
*/ |
|
+ (NSString *)cycleStrConversion:(NSString *)cycleStr { |
|
|
|
NSArray *cycleArr = @[NSLocalizedString(@"周一 ", nil), |
|
NSLocalizedString(@"周二 ", nil), |
|
NSLocalizedString(@"周三 ", nil), |
|
NSLocalizedString(@"周四 ", nil), |
|
NSLocalizedString(@"周五 ", nil), |
|
NSLocalizedString(@"周六 ", nil), |
|
NSLocalizedString(@"周日 ", nil)]; |
|
NSMutableString *tempStr = [[NSMutableString alloc] init]; |
|
|
|
for (int i = 0; i < 7; i++) { |
|
|
|
if ([[cycleStr substringWithRange:NSMakeRange(i, 1)] isEqualToString:@"1"]) { |
|
[tempStr appendString:cycleArr[i]]; |
|
} |
|
} |
|
return tempStr.length ? (NSString *)tempStr : NSLocalizedString(@"仅一次", nil); |
|
} |
|
|
|
+ (NSString *)stringDecode:(NSString *)str { |
|
NSString *result = [(NSString *)str stringByReplacingOccurrencesOfString:@"+" withString:@" "]; |
|
result = [result stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; |
|
return result; |
|
} |
|
|
|
+ (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString |
|
{ |
|
if (jsonString == nil) { |
|
return nil; |
|
} |
|
|
|
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding]; |
|
NSError *err; |
|
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData |
|
options:NSJSONReadingMutableContainers |
|
error:&err]; |
|
|
|
if(err) |
|
{ |
|
NSLog(@"json解析失败:%@",err); |
|
return nil; |
|
} |
|
return dic; |
|
} |
|
|
|
///16进制字符串转data |
|
+ (NSData *)convertHexStrToData:(NSString *)str |
|
{ |
|
if (!str || [str length] == 0) { |
|
return nil; |
|
} |
|
|
|
NSMutableData *hexData = [[NSMutableData alloc] initWithCapacity:20]; |
|
NSRange range; |
|
if ([str length] % 2 == 0) { |
|
range = NSMakeRange(0, 2); |
|
} else { |
|
range = NSMakeRange(0, 1); |
|
} |
|
for (NSInteger i = range.location; i < [str length]; i += 2) { |
|
unsigned int anInt; |
|
NSString *hexCharStr = [str substringWithRange:range]; |
|
NSScanner *scanner = [[NSScanner alloc] initWithString:hexCharStr]; |
|
|
|
[scanner scanHexInt:&anInt]; |
|
NSData *entity = [[NSData alloc] initWithBytes:&anInt length:1]; |
|
[hexData appendData:entity]; |
|
|
|
range.location += range.length; |
|
range.length = 2; |
|
} |
|
return hexData; |
|
} |
|
|
|
// 将NSLog打印信息保存到Document目录下的文件中 |
|
+ (void)redirectNSlogToDocumentFolder |
|
{ |
|
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); |
|
NSString *documentDirectory = [paths objectAtIndex:0]; |
|
|
|
NSString *fileName = [NSString stringWithFormat:@"%@.log",[[NSDate alloc] initWithTimeIntervalSinceNow:8*3600]]; // 注意不是NSData! |
|
NSString *logFilePath = [documentDirectory stringByAppendingPathComponent:fileName]; |
|
|
|
// 将log输入到文件 |
|
freopen([logFilePath cStringUsingEncoding:NSASCIIStringEncoding],"a+",stderr); |
|
} |
|
|
|
/** |
|
将步数转换为卡路里,公里(此算法与手环算法一致) |
|
|
|
@param steps 步数 |
|
@param type 0:卡路里 1:公里 |
|
*/ |
|
+ (NSString *)stepsConversionCaloriesAndKmStpes:(NSString *)steps type:(NSInteger)type { |
|
if (!steps.integerValue) { |
|
return @"0"; |
|
} |
|
// 卡路里跟公里数根据计算(与手环计算公式一致) |
|
if (type) { |
|
// 公里 = 步数*(0.415*身高)/100000 |
|
NSInteger height = 175; |
|
return [NSString stringWithFormat:@"%.3f",steps.integerValue * (0.415 * height) / 100000]; |
|
} |
|
// 卡路里 = 步数 *((体重- 15)* 0.000693 + 0.005895) |
|
CGFloat weight = 65.0; |
|
return [NSString stringWithFormat:@"%.3f",steps.integerValue * ((weight - 15) * 0.000693 + 0.005895)]; |
|
} |
|
|
|
|
|
@end
|
|
|