CoreBluetooth - 未为服务设置UUID

时间:2014-08-14 09:49:27

标签: ios objective-c macos uuid core-bluetooth

我正在尝试在mac和Iphone之间使用蓝牙。但UUID似乎没有设置在Mac上。我在mac上使用:

 self.characteristic = [[CBMutableCharacteristic alloc] initWithType:[CBUUID UUIDWithString:@"DDCA9B49-A6F5-462F-A89A-C2144083CA7F"] properties:CBCharacteristicPropertyNotify value:nil permissions:CBAttributePermissionsReadable];

    self.service = [[CBMutableService alloc] initWithType:[CBUUID UUIDWithString:@"BA6878AE-4F70-41AD-B793-8F8AF2A2811E"] primary:YES];
    self.service.characteristics = @[self.characteristic];

    NSLog(@"UUID SERVICE : %@",self.service.UUID.description);//WTF ?
    [self.peripheralManager addService:self.service];

但是NSlog打印

UUID SERVICE : Unknown (<ba6878ae 4f7041ad b7938f8a f2a2811e>)

而且我认为这就是为什么Iphone在尝试scanForPeripheralsWithService(在Iphone上)时找不到mac的原因

 self.serviceAlert = @"BA6878AE-4F70-41AD-B793-8F8AF2A2811E";

//add the services to the Array
self.services = [NSArray arrayWithObjects:
                 [CBUUID UUIDWithString:self.serviceAlert],
                 nil];

self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil options:nil];

[self.centralManager scanForPeripheralsWithServices:self.services options:nil];

知道服务的UUID在Mac上没有正确设置的原因吗?

编辑:我用

[peripheral startAdvertising:@{ CBAdvertisementDataLocalNameKey: @"hello" }];

在didAddServicemethod中;我可以找到[self.centralManager scanForPeripheralsWithServices:nil options:nil]

的服务

1 个答案:

答案 0 :(得分:0)

我找到了解决方案,我只是非常愚蠢。服务广告不是好消息

[peripheral startAdvertising:@{ CBAdvertisementDataServiceUUIDsKey : @[self.service.UUID] }];

这是做这件事的好方法。