2013-10-02 56 views
8

我在更新我的CoreBluetooth代码从iOS 6到iOS 7时遇到问题。我可以扫描外围设备并建立连接,但是,我无法使用提供的新CoreBluetooth方法重新连接外设内的iOS 7.下面来看看我如何努力实现重新连接:iOS 7 CoreBluetooth retrievePeripheralsWithIdentifiers not retrieve

- (void)retrievePeripheral:(NSString *)uuidString 
{ 

NSUUID *nsUUID = [[NSUUID UUID] initWithUUIDString:uuidString]; 

if(nsUUID) 
{   
    NSArray *peripheralArray = [centralManager retrievePeripheralsWithIdentifiers:@[nsUUID]]; 

    // Check for known Peripherals 
    if([peripheralArray count] > 0) 
    { 
     for(CBPeripheral *peripheral in peripheralArray) 
     { 
      NSLog(@"Connecting to Peripheral - %@", peripheral); 

      [self connectPeripheral:peripheral]; 
     } 
    } 
    // There are no known Peripherals so we check for connected Peripherals if any 
    else 
    { 
     CBUUID *cbUUID = [CBUUID UUIDWithNSUUID:nsUUID]; 

     NSArray *connectedPeripheralArray = [centralManager retrieveConnectedPeripheralsWithServices:@[cbUUID]]; 

     // If there are connected Peripherals 
     if([connectedPeripheralArray count] > 0) 
     { 
      for(CBPeripheral *peripheral in connectedPeripheralArray) 
      { 
       NSLog(@"Connecting to Peripheral - %@", peripheral); 

       [self connectPeripheral:peripheral]; 
      } 
     } 
     // Else there are no available Peripherals 
     else 
     { 
      // No Dice! 
      NSLog(@"There are no available Peripherals"); 
     } 
    } 
} 

} 

哪里uuidString是保存的外围UUID。

我总是进入没有可用外设的NSLog语句。我想我错过了一些非常明显的东西,有人可以指引我朝着正确的方向前进。

另外,我已经阅读了关于CoreBluetooth iOS 7更新问题的其他文章,并试图重新设置BLE设备和iOS设备,但无济于事。

在此先感谢! audible

回答

5

事实证明,我的错误处理了我在将消息发送到retrievePeripheral方法之前如何将外设标识符转换为NSString格式。

这是不正确的方法:

NSString *uuidString = [NSString stringWithFormat:@"%@", CFUUIDCreateString(NULL, (__bridge CFUUIDRef)([peripheral identifier]))]; 

这是正确的方式(在我的情况下工作):

NSString *uuidString = [NSString stringWithFormat:@"%@", [[peripheral identifier] UUIDString]]; 

根据苹果文档的NSUUID类不是免费电话与CoreFoundation的CFUUIDRef桥接,如下所述:

注意:NSUUID类不免费桥接CoreFoundation的CFUUIDRef。如果需要,请使用UUID字符串在CFUUIDNSUUID之间进行转换。两个NSUUID对象不保证可以通过指针值进行比较(因为CFUUIDRef是);使用isEqual:来比较两个NSUUID实例。

对于那些你们谁喜欢的是什么,这意味着像我在这里做一个视觉表现就是一个例子:)

在控制台内打印的周边信息:

<CBPeripheral: 0x14699430 identifier = DD2468AB-1865-B926-7FA4-AE3755D479D8, Name = "IDYNAMO EMV-A27F", state = disconnected> 

不正确的方法将外设的标识符转换为NSString得出:

1865B926-7FA4-AE37-55D4-79D800000000 

T他正确的外围设备的识别码转换为NSString收益的方式:

DD2468AB-1865-B926-7FA4-AE3755D479D8 

我希望这可以帮助别人对他们的BLE的旅程,并随时纠正我,如果我错了,以任何方式,因为我还在学习这个东西作为好。

谢谢!

听得见

+0

我想知道如果你能帮助我与我的代码转换为retrievePeripheralsWithIdentifiers - (空)centralManager:(CBCentralManager *) didDiscoverPeripheral中心:(CBPeripheral *)aPeripheral advertisementData:(NSDictionary的*)advertisementData RSSI:(NSNumber *)RSSI { NSMutableArray * peripherals = [self mutableArrayValueForKey:@“heartRateMonitors”]; if(![self.heartRateMonitors containsObject:aPeripheral]) [peripherals addObject:aPeripheral]; [self.manager retrievePeripherals:[NSArray arrayWithObject:(id)aPeripheral.UUID]]; } –

1

这是一篇旧帖子,但对于其他人寻找答案。

第二次/下一次调用retrieveConnectedPeripherals实际上需要Service UUID(CBUUID类型),而不是iOS首次连接的NSUUID。检索的是具有该服务UUID实施的外设的列表。例如,如果您有多个活动臂章,并且除了其他内容外,您还可以检索所有3个外围设备。在第一次调用KNOWN外设时,你必须在应用启动之间存储NSUUID,因为该ID是由iOS在第一次连接时创建的。希望帮助