在didSelectRowAtIndexPath中连接到外围设备

时间:2014-10-14 03:20:03

标签: objective-c iphone uitableview core-bluetooth

我正在使用CoreBluetooth开发应用程序。我能够从应用程序中发现设备并在表格视图中显示其名称。我面临以下要求的问题:

        
  • 首次使用时,用户从列表中选择一个外围设备。应使用蓝牙串行端口配置文件(SPP)与外围设备建立连接。发现和连接非常简单,只需单击即可完成,从而最大限度地减少用户交互。
  •     
  • 一旦在外围设备和应用程序之间建立了连接,该应用程序就会记住该设备并始终寻找并连接到该设备以供所有后续使用。

我已经为此编写了以下代码,用于扫描和发现

- (void)scan
{
    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:NO], CBCentralManagerScanOptionAllowDuplicatesKey, nil];
    NSLog(@"options are %@",options);

    [self.centralManager scanForPeripheralsWithServices:nil
                                                options:nil];
    _txtLogMessage.text = [NSString stringWithFormat:@"%@ \n %@",_txtLogMessage.text,@"Scanning started"];
    NSLog(@"Scanning started");
}

- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral
                                                                          *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
    [central connectPeripheral:peripheral options:nil];
    self.discoveredPeripheral = peripheral;
    _logData = [NSString stringWithFormat:@"Did discover peripheral. peripheral: %@ rssi: %@, UUID: %@   advertisementData: %@ ", peripheral, RSSI, peripheral.UUID, advertisementData];

    NSLog(@"%@",_logData);


    [_periferalDevices addObject:peripheral.name];
    [_tableView reloadData];
}

用于连接设备

- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
    peripheral.delegate = self;

    [self.discoveredPeripheral discoverServices:nil];
    NSLog(@"Peripheral Connected");
    NSLog(@" started  time is %@",_timestring);

    //[self.centralManager stopScan];
    NSLog(@"Scanning stopped");
    [self.data setLength:0];

    [peripheral discoverServices:nil];

    [peripheral discoverServices:@[[CBUUID UUIDWithString:TRANSFER_SERVICE_UUID]]];
}

在表格视图单元格中,用于索引路径的行目标能够获取要查看的数据,但在连接到外围设备时面临问题,同时在表格视图中选择特定行

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:CellIdentifier];
    }
    cell.textLabel.text = [_periferalDevices objectAtIndex:indexPath.row];

    return  cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    _discoveredPeripheral.delegate = self;
    [self.centralManager connectPeripheral:[_periferalDevices objectAtIndex:indexPath.row]
                                   options:nil];
    NSLog(@"discovered peripheral is %@",[_periferalDevices objectAtIndex:indexPath.row]);
    NSString *datstr=[_discoveredPeripheral valueForKey:@"identifier"];
    NSLog(@"value for key is ........%@",datstr);
}

1 个答案:

答案 0 :(得分:0)

当您尝试使用外围设备对象时出现的错误源于您将外围设备的名称添加到阵列而不是外围设备本身的事实。因此,替换

[_periferalDevices addObject:peripheral.name]; 

使用

[_periferalDevices addObject:peripheral];

而且,显然,只要您插入名称,您就必须修复。例如,您的cellForRowAtIndexPath带有一行说明:

cell.textLabel.text = [_periferalDevices objectAtIndex:indexPath.row];

您可能希望用以下内容替换它:

CBPeripheral *peripheral = _periferalDevices[indexPath.row];
cell.textLabel.text = peripheral.name;

您的didDiscover...正在立即连接。它可能不应该。您应该只尝试连接到用户选择的设备。如果您再次尝试连接,这可能会导致问题(如果您尝试连接两次,我不知道它会起什么作用)。你的didSelectRow...连接了用户选择的那些,所以你应该把它留在那里。

你说"我正面临着连接问题"。那么,didFailToConnectPeripheral报告到底是什么?

顺便说一句,我不清楚你为什么两次打电话给discoverServices。要么发现所有服务(慢),要么只发现特定服务,但你可能不必同时做这两项服务。


我的原始答案(基于原始代码示例)如下所示。


您的代码中包含:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    NSString *cellText = cell.textLabel.text;

    if([cellText isEqualToString:@""])
    {

    }
    else
    {
        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Connected to "
                                                      message:cellText delegate:self cancelButtonTitle:@"ok"
                                            otherButtonTitles: nil];
        [alert show];
    }

    _discoveredPeripheral.delegate = self;
    [self.centralManager connectPeripheral:_discoveredPeripheral options:nil];
    NSLog(@"discovered peripheral is %@", _discoveredPeripheral);
    NSString *datstr = [_discoveredPeripheral valueForKey:@"identifier"];
    NSLog(@"value for key is ........%@", datstr);
}

您正在检索所选单元格的信息(虽然Hot Licks是正确的,您应该转到模型,而不是查看该信息的视图),检索cellText,但您继续使用{ {1}},这是一个与用户刚刚选择的单元格没有明显关系的变量。

您希望维护外围设备阵列的模型,当用户点击单元格时,使用_discoveredPeripheral在模型阵列中查找外围设备,并使用indexPath.row你的连接代码。

相关问题