蓝牙设备的Mac地址

mac地址即设备的物理地址
不同于安卓设备,iOS系统中苹果禁用了mac地址并将mac地址进行重新加密生成的UUID作为唯一标识进行返回。
因此开发者需要其他方式进行获取

一、扫描时获取

这一阶段,蓝牙锁可以通过向外广播数据,数据中携带mac地址,中心设备通过接收广播的数据来获取mac地址

+ (NSString *)convertToNSStringWithNSData:(NSData *)data
{
    NSMutableString *strTemp = [NSMutableString stringWithCapacity:[data length]*2];
    
    const unsigned char *szBuffer = [data bytes];
    
    for (NSInteger i=0; i < [data length]; ++i) {
        
        [strTemp appendFormat:@"%02lx",(unsigned long)szBuffer[i]];
        
    }
    
    return strTemp;
}

二、链接时获取

func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor descriptor: CBDescriptor, error: Error?) {
    if let data = descriptor.characteristic.value {
        let value = NSString(format: "%@", data as CVarArg)
        var macString = NSMutableString(string: "")
        let range1 = NSRange(location: 16, length: 2)
        let str1 = value.substring(with: range1).uppercased()
        macString.append(str1)
        macString.append(":")
        
        let range2 = NSRange(location: 14, length: 2)
        let str2 = value.substring(with: range2).uppercased()
        macString.append(str2)
        macString.append(":")
        
        let range3 = NSRange(location: 12, length: 2)
        let str3 = value.substring(with: range3).uppercased()
        macString.append(str3)
        macString.append(":")
        
        let range4 = NSRange(location: 5, length: 2)
        let str4 = value.substring(with: range4).uppercased()
        macString.append(str4)
        macString.append(":")
        
        let range5 = NSRange(location: 3, length: 2)
        let str5 = value.substring(with: range5).uppercased()
        macString.append(str5)
        macString.append(":")
        
        let range6 = NSRange(location: 1, length: 2)
        let str6 = value.substring(with: range6).uppercased()
        macString.append(str6)

        
        print(macString)
    }
}

你可能感兴趣的:(蓝牙设备的Mac地址)