iOS如果获取蓝牙Mac地址

目录(?)[+]

在做iOS蓝牙开发的时候肯定会遇到这种问题,苹果自带的CoreBluetooth SDK中不能获取到蓝牙的Mac地址,只能得到UUID,但是UUID并不是唯一的,换一个设备再连接UUID就不一样了。而且Android手机是可以获取到Mac地址的,不能很好的兼容Android手机,Android是不可能获取到UUID,为了很好的兼容,只能iOS想办法获取Mac地址了。幸运的是iOS也不是没有办法获取到Mac地址。

主要思路

虽然苹果官方的API没有获取Mac地址的方法,但是当我翻看蓝牙的文档的时候,我发现蓝牙有提供一个设备信息的service[service UUID:0x180A],里面提供了两个characteristic:获取芯片的Mac地址(0x2A23 )和获取软件的版本号(0x2A26)。如下图所示: 

实现

根据这个思路:

[peripheral discoverServices:@[[CBUUID UUIDWithString:@"180A"]]];
[service discoverCharacteristics:@[[CBUUID UUIDWithString:@"2A23"]]];
[peripheral readValueForCharacteristic:characteristic]
NSString *value = [NSString stringWithFormat:@"%@",characteristic.value];
NSMutableString *macString = [[NSMutableString alloc] init];
[macString appendString:[[value substringWithRange:NSMakeRange(16, 2)] uppercaseString]];
[macString appendString:@":"];
[macString appendString:[[value substringWithRange:NSMakeRange(14, 2)] uppercaseString]];
[macString appendString:@":"];
[macString appendString:[[value substringWithRange:NSMakeRange(12, 2)] uppercaseString]];
[macString appendString:@":"];
[macString appendString:[[value substringWithRange:NSMakeRange(5, 2)] uppercaseString]];
[macString appendString:@":"];
[macString appendString:[[value substringWithRange:NSMakeRange(3, 2)] uppercaseString]];
[macString appendString:@":"];
[macString appendString:[[value substringWithRange:NSMakeRange(1, 2)] uppercaseString]];
//00:E0:4C:3F:14:DE                            
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

这里可一个大家推荐一个非常好用的蓝牙开发的库,跟CoreBluetooth的接口一摸一样,只是数据是用过block返回的,用起来更加的方便。MPBluetoothKit 
Github地址

下面是写的MPBluetoothKit 获取Mac地址的代码

    CBUUID *macServiceUUID = [CBUUID UUIDWithString:@"180A"];
    CBUUID *macCharcteristicUUID = [CBUUID UUIDWithString:@"2A23"];
    [mPeripheral discoverServices:@[macServiceUUID] withBlock:^(MPPeripheral *peripheral, NSError *error) {
        if(peripheral.services.count){
            MPService *service = [peripheral.services objectAtIndex:0];
            [service discoverCharacteristics:@[macCharcteristicUUID] withBlock:^(MPPeripheral *peripheral, MPService *service, NSError *error) {
                for(MPCharacteristic *characteristic in service.characteristics){
                    if([characteristic.UUID isEqual:macCharcteristicUUID]){
                        [characteristic readValueWithBlock:^(MPPeripheral *peripheral, MPCharacteristic *characteristic, NSError *error){
                            NSString *value = [NSString stringWithFormat:@"%@",characteristic.value];
                            NSMutableString *macString = [[NSMutableString alloc] init];
                            [macString appendString:[[value substringWithRange:NSMakeRange(16, 2)] uppercaseString]];
                            [macString appendString:@":"];
                            [macString appendString:[[value substringWithRange:NSMakeRange(14, 2)] uppercaseString]];
                            [macString appendString:@":"];
                            [macString appendString:[[value substringWithRange:NSMakeRange(12, 2)] uppercaseString]];
                            [macString appendString:@":"];
                            [macString appendString:[[value substringWithRange:NSMakeRange(5, 2)] uppercaseString]];
                            [macString appendString:@":"];
                            [macString appendString:[[value substringWithRange:NSMakeRange(3, 2)] uppercaseString]];
                            [macString appendString:@":"];
                            [macString appendString:[[value substringWithRange:NSMakeRange(1, 2)] uppercaseString]];
                            NSLog(@"macString:%@",macString);
                        }];
                    }
                }

            }];
        }
    }];                          
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

是不是一个很方便库啊?当然这是小弟开源的一个库,如果喜欢的话就去Github帮忙star一下哈 ^_^

update 2016.1.7

因为有网友要求写一个例子,所以我写了一个DEMO放在Github上了 
BluetoothMacAddressDemo

你可能感兴趣的:(iOS,开发)