记录蓝牙中用到的进制换算

最近蓝牙项目中,蓝牙手环中的指令比较多,比如同步时间,发送震动指令,来点提醒等等,根据和硬件定义的规则来发送指令。
比如同步时间的指令:


这里的年月日时分秒都有一定的位数限制
比如年是6位,月是4位,日是5位,这就需要将本地时间给拆分,封装出一个方法算出这串指令

直接上代码

/**
 同步时间的指令
 根据当前年月日时分秒 根据规则先转二进制,然后四位一组转十六进制
 @return 返回的数据
 */
+ (NSString *)senderTimeCommand {
    
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDate *nowdate = [NSDate date];
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit |
    NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSWeekCalendarUnit | NSWeekOfMonthCalendarUnit | NSWeekOfYearCalendarUnit;
    comps = [calendar components:unitFlags fromDate:nowdate];
    
    NSString *year = [[NSString stringWithFormat:@"%ld",(long)[comps year]] substringFromIndex:2];
    NSString *month = [NSString stringWithFormat:@"%ld",(long)[comps month]];
    NSString *day = [NSString stringWithFormat:@"%ld",(long)[comps day]];
    NSString *hour = [NSString stringWithFormat:@"%ld",(long)[comps hour]];
    NSString *minute = [NSString stringWithFormat:@"%ld",(long)[comps minute]];
    NSString *second = [NSString stringWithFormat:@"%ld",(long)[comps second]];
    // 十进制转换为二进制带位数
    NSString *yearByte = [self toBinarySystemWithDecimalSystem:[year intValue] length:6];//6位
    NSString *monthByte = [self toBinarySystemWithDecimalSystem:[month intValue] length:4];//4位
    NSString *dayByte = [self toBinarySystemWithDecimalSystem:[day intValue] length:5];//5位
    NSString *hourByte = [self toBinarySystemWithDecimalSystem:[hour intValue] length:5];//5位
    NSString *minuteByte = [self toBinarySystemWithDecimalSystem:[minute intValue] length:6];//6位
    NSString *secondByte = [self toBinarySystemWithDecimalSystem:[second intValue] length:6];//6位
    
    //将时间的二进制组合起来
    NSString *byteStr = [NSString stringWithFormat:@"%@%@%@%@%@%@",yearByte,monthByte,dayByte,hourByte,minuteByte,secondByte];
   //每4位转换成十六进制
    NSString *byte0 = [byteStr substringToIndex:4];
    NSString *byte1 = [byteStr substringWithRange:NSMakeRange(4, 4)];
    NSString *byte2 = [byteStr substringWithRange:NSMakeRange(8, 4)];
    NSString *byte3 = [byteStr substringWithRange:NSMakeRange(12, 4)];
    NSString *byte4 = [byteStr substringWithRange:NSMakeRange(16, 4)];
    NSString *byte5 = [byteStr substringWithRange:NSMakeRange(20, 4)];
    NSString *byte6 = [byteStr substringWithRange:NSMakeRange(24, 4)];
    NSString *byte7 = [byteStr substringWithRange:NSMakeRange(28, 4)];
    

    NSString *finalStr = [NSString stringWithFormat:@"ab000009000000000100010004%@%@%@%@%@%@%@%@",[self getHexByBinary:byte0],[self getHexByBinary:byte1],[self getHexByBinary:byte2],[self getHexByBinary:byte3],[self getHexByBinary:byte4],[self getHexByBinary:byte5],[self getHexByBinary:byte6],[self getHexByBinary:byte7]];
    BYLog(@"%@",finalStr);
    return finalStr;
}

上面用到的方法

//  十进制转二进制
- (NSString *)toBinarySystemWithDecimalSystem:(int)num length:(int)length
{
    int remainder = 0;      //余数
    int divisor = 0;        //除数
    
    NSString * prepare = @"";
    
    while (true)
    {
        remainder = num%2;
        divisor = num/2;
        num = divisor;
        prepare = [prepare stringByAppendingFormat:@"%d",remainder];
        
        if (divisor == 0)
        {
            break;
        }
    }
    //倒序输出
    NSString * result = @"";
    for (int i = length -1; i >= 0; i --)
    {
        if (i <= prepare.length - 1) {
            result = [result stringByAppendingFormat:@"%@",
                      [prepare substringWithRange:NSMakeRange(i , 1)]];
            
        }else{
            result = [result stringByAppendingString:@"0"];
            
        }
    }
    return result;
}

二进制转16进制


//2zhuan16
- (NSString *)getHexByBinary:(NSString *)binary {
    
    NSMutableDictionary *binaryDic = [[NSMutableDictionary alloc] initWithCapacity:16];
    [binaryDic setObject:@"0" forKey:@"0000"];
    [binaryDic setObject:@"1" forKey:@"0001"];
    [binaryDic setObject:@"2" forKey:@"0010"];
    [binaryDic setObject:@"3" forKey:@"0011"];
    [binaryDic setObject:@"4" forKey:@"0100"];
    [binaryDic setObject:@"5" forKey:@"0101"];
    [binaryDic setObject:@"6" forKey:@"0110"];
    [binaryDic setObject:@"7" forKey:@"0111"];
    [binaryDic setObject:@"8" forKey:@"1000"];
    [binaryDic setObject:@"9" forKey:@"1001"];
    [binaryDic setObject:@"A" forKey:@"1010"];
    [binaryDic setObject:@"B" forKey:@"1011"];
    [binaryDic setObject:@"C" forKey:@"1100"];
    [binaryDic setObject:@"D" forKey:@"1101"];
    [binaryDic setObject:@"E" forKey:@"1110"];
    [binaryDic setObject:@"F" forKey:@"1111"];
    
    if (binary.length % 4 != 0) {
        
        NSMutableString *mStr = [[NSMutableString alloc]init];;
        for (int i = 0; i < 4 - binary.length % 4; i++) {
            
            [mStr appendString:@"0"];
        }
        binary = [mStr stringByAppendingString:binary];
    }
    NSString *hex = @"";
    for (int i=0; i

然后发送这串指令,手环时间就同步了。

记录蓝牙中用到的进制换算_第1张图片
打工是不可能的.gif

你可能感兴趣的:(记录蓝牙中用到的进制换算)