繁体   English   中英

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

[英]Store UUID of discover peripheral in CoreBluetooth LE iOS

我尝试下面的代码将已发现设备的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。 我不知道为什么。 请给我一些建议。 非常感谢。

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

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

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

例如

- (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方法。 希望有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM