HomeKit 二维码生成规则



- (NSString *)reverseString:(NSString *)text {
    NSInteger length = text.length;
    NSMutableString *rev = [NSMutableString string];
    for (int i = 0; i < length; i++) {
        unichar str = [text characterAtIndex:length-i-1];
        [rev appendFormat:@"%c",str];
    }
    return rev;
}

- (NSString *)gen_homekit_setup_uri_withCategory:(int)category
                                 withPsw:(NSString *)psw
                             withSetupId:(NSString *)setupId {
    int version = 0;
    int reserved = 0;
    int flags = 2;
    
    long payload = 0;
    payload |= (version & 0x7);
    
    payload <<= 4;
    payload |=(reserved & 0xf);  // reserved bits
    
    payload <<= 8;
    payload |= (category & 0xff);
    
    payload <<= 4;
    payload |= (flags & 0xf);
    
    payload <<= 27;
    payload |= ([psw stringByReplacingOccurrencesOfString:@"-" withString:@""].intValue) & 0x7fffffff;
    
    NSString *BASE36 = @"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    NSString *encodedPayload = @"";
    for (int i = 0; i < 9; i++) {
        int index = payload % 36;
        unichar temp_code = [BASE36 characterAtIndex:index];
        encodedPayload = [encodedPayload stringByAppendingFormat:@"%c", temp_code];
        payload /= 36;
    }
    return [NSString stringWithFormat:@"X-HM://%@%@",
            [self reverseString:encodedPayload],
            setupId];
}

-(void)test() {
     // X-HM://00527Y91Q1QJ8
     int accsory_cateory = 5;
     NSString *psw = @"123-45-678";
     NSString *setupId = @"1QJ8";
     NSLog(@"%@",[self gen_homekit_setup_uri_withCategory:5 withPsw: psw withSetupId: setupId]);
}

你可能感兴趣的:(HomeKit 二维码生成规则)