美文网首页
HomeKit 二维码生成规则

HomeKit 二维码生成规则

作者: F森 | 来源:发表于2019-03-08 00:00 被阅读0次
    
    
    - (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 二维码生成规则

          本文链接:https://www.haomeiwen.com/subject/qaokpqtx.html