IOS 获取蓝牙mac地址

方法一

- (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地址)