美文网首页
OC-获取蓝牙mac地址

OC-获取蓝牙mac地址

作者: SK丿希望 | 来源:发表于2020-03-31 17:28 被阅读0次

只需要把蓝牙设备信息传入即可 (测试蓝牙打印机可行)

+ (NSString *)getMacWithperipheral:(CBPeripheral *)peripheral {
    NSString *value = @"000000000000";
    NSMutableString *macString = [[NSMutableString alloc] init];
    for (CBService *service in peripheral.services) {
        for (CBCharacteristic *characteristic in service.characteristics) {
            if ([characteristic.UUID.UUIDString isEqualToString:@"2A25"]) {
                value = [[NSString alloc] initWithData:characteristic.value encoding:NSUTF8StringEncoding];
                NSLog(@"%@", value);
            }
        }
    }
    [macString appendString:[[value substringWithRange:NSMakeRange(0, 2)] uppercaseString]];
    [macString appendString:@":"];
    [macString appendString:[[value substringWithRange:NSMakeRange(2, 2)] uppercaseString]];
    [macString appendString:@":"];
    [macString appendString:[[value substringWithRange:NSMakeRange(4, 2)] uppercaseString]];
    [macString appendString:@":"];
    [macString appendString:[[value substringWithRange:NSMakeRange(6, 2)] uppercaseString]];
    [macString appendString:@":"];
    [macString appendString:[[value substringWithRange:NSMakeRange(8, 2)] uppercaseString]];
    [macString appendString:@":"];
    [macString appendString:[[value substringWithRange:NSMakeRange(10, 2)] uppercaseString]];
    NSLog(@"mac == %@",macString);
    return  macString;
}

相关文章

网友评论

      本文标题:OC-获取蓝牙mac地址

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