在CoreBluetooth LE iOS中存储发现外围设备的UUID

时间:2013-12-25 07:22:05

标签: ios objective-c uuid bluetooth-lowenergy

我尝试使用下面的代码将已发现设备的UUID存储到Array,然后在此阵列中选择UUID进行连接但不运行。

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

    NSLog (@"Discovered peripheral: %@", [peripheral name]);
    [foundArray addObject:peripheral.name];

    NSLog (@"UUID peripheral: %@", [peripheral UUID]);

    CFUUIDRef uuid = [peripheral UUID];
    [discoverUUIDArray addObject: (__bridge id)(uuid)];
    NSLog(@"Before = %@", uuid);

    NSLog (@"peripheral services before connected: %@", [peripheral services]);

    NSLog(@"adversting data %@",[NSString stringWithFormat:@"%@",[advertisementData description]]);

    self.activePeripheral = peripheral;

    NSLog(@"foundArray is %@", foundArray);
    NSLog(@"discoverUUIDArray is %@", discoverUUIDArray);
    [self.tblFound reloadData];

}

然后我将这些Discover设备加载到tableview,当用户选择要连接的设备时,我的应用程序挂起。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(self.manager.state == CBCentralManagerStatePoweredOn)
    {
        NSLog(@"You selected %@ to connect",[foundArray objectAtIndex:indexPath.row]);
        CFUUIDRef uuidGet = (__bridge CFUUIDRef) [discoverUUIDArray objectAtIndex:indexPath.row];
        NSLog(@"UUID is %@",[discoverUUIDArray objectAtIndex:indexPath.row]);
        [self.manager connectPeripheral:[discoverUUIDArray objectAtIndex:indexPath.row] options:nil];

    }
}

选择要连接的设备时,会显示错误:

[__NSCFType state]: unrecognized selector sent to instance 0x17d70950
2013-12-25 15:17:40.699 TestPeripheral[213:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFType state]: unrecognized selector sent to instance 0x17d70950'

discoverUUIDArray存储发现设备的UUID。我不知道为什么。请给我一些建议。非常感谢。

2 个答案:

答案 0 :(得分:3)

在iOS7中不推荐使用

CFUUIDRef uuid = [peripheral UUID]; 如果需要,可以使用peripheral.identifier作为NSUUID存储在数组中

如果意图存储UUID's以供将来检索,请将其保存在NSUserDefaults而不是运行时数组

答案 1 :(得分:0)

您可以创建一个NSMutableArray来存储所有已发现设备的列表,然后使用此列表填充UITableView并连接(如果用户选择)。

e.g。

- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
    NSString *localName = [advertisementData objectForKey:CBAdvertisementDataLocalNameKey];
    if (![localName isEqual:@""]) {
        [self.BTLEDevices addObject:peripheral];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *tableIdentifier = @"BLEDeviceList";
    CBPeripheral *peripheral = [self.BTLEDevices objectAtIndex:indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tableIdentifier forIndexPath:indexPath];
    cell.textLabel.text = peripheral.name;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    CBPeripheral *peripheral = [self.BTLEDevices objectAtIndex:[indexPath.row integerValue]];  
    peripheral.delegate = self;
    [self.BTLECentralManager connectPeripheral:peripheral options:nil];
}

不要忘记创建UITableView对象并将其添加到视图中,我也遗漏了其他CBPeripheral方法。希望有所帮助。