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

IOS 获取蓝牙mac地址

作者: zttjhm | 来源:发表于2019-01-09 17:26 被阅读0次

方法一

- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error {

    for (CBService *service in peripheral.services) {

        [peripheral discoverCharacteristics:nil forService:service];

    }

}

- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error {

    for (CBCharacteristic *characteristic in service.characteristics) {

        if ([characteristic.UUID.UUIDString isEqualToString:@"2A25"]) {

            [peripheral setNotifyValue:YES forCharacteristic:characteristic];

            [peripheral readValueForCharacteristic:characteristic];

        }

    }

}

-(void)peripheral:(CBPeripheral*)peripheral didUpdateValueForCharacteristic:(CBCharacteristic*)characteristic error:(NSError*)error

{

    NSString *value = [[NSString alloc] initWithData:characteristic.value encoding:NSUTF8StringEncoding];

    NSLog(@"MAC地址是macString:%@",value);

  }

方法二// 发现蓝牙外设 peripheral

- (void)centralManager:(CBCentralManager*)central didDiscoverPeripheral:(CBPeripheral*)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber*)RSSI{

    NSDictionary*dic = advertisementData[@"kCBAdvDataServiceData"];

    if( dic ) {

        CBUUID *uuid = [CBUUID UUIDWithString:@"FDA5"]; 

        NSData*data = dic[uuid];

        constintMAC_BYTE_LENGTH =6;

        Byte    bytes[MAC_BYTE_LENGTH +1] = {0};

        if([datalength] >= MAC_BYTE_LENGTH) {

            [datagetBytes:bytesrange:NSMakeRange([datalength] - MAC_BYTE_LENGTH, MAC_BYTE_LENGTH)];

            NSMutableArray *macs = [NSMutableArray array];

            for(inti =0;i < MAC_BYTE_LENGTH ;i ++) {

                NSString*strByte = [NSStringstringWithFormat:@"%02x",bytes[i]];

                [macsaddObject:strByte];

            }

            NSString *strMac = [macs componentsJoinedByString:@":"];

        }

    }

}

相关文章

网友评论

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

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