首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >IOS:当用户选择行时,从表视图连接外围设备

IOS:当用户选择行时,从表视图连接外围设备
EN

Stack Overflow用户
提问于 2014-10-14 13:57:10
回答 1查看 753关注 0票数 0

我正在开发核心蓝牙应用程序。当我开始在表视图中发现设备时,当我使用did select row at index path中的did connect委托方法选择要连接的设备时,在控制台中出现错误,我发现和连接设备的代码如下所示

用于发现外围设备

代码语言:javascript
复制
 - (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:  
    (CBPeripheral 
    *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
    {

    self.discoveredPeripheral = peripheral;
    _logData = [NSString stringWithFormat:@"Did discover peripheral. peripheral: %@ rssi: %@,  
    `enter code here`UUID: %@   advertisementData: %@ ", peripheral, RSSI, peripheral.UUID,  
      advertisementData];
    NSLog(@"%@",_logData);


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


 }

用于连接外围设备

代码语言:javascript
复制
  - (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral   

   *)peripheral
  {
   peripheral.delegate = self;

  [self.discoveredPeripheral discoverServices:nil];
   NSLog(@"Peripheral Connected");
   NSLog(@" started  time is %@",_timestring);
  // Stop scanning
 //[self.centralManager stopScan];
 NSLog(@"Scanning stopped");
  [self.data setLength:0];
[peripheral discoverServices:nil];


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

我的困惑是在下面的代码中,在选择选定行中的特定外围设备后,我们必须编写什么代码才能在表视图中连接发现的设备。

代码语言:javascript
复制
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath   
    *)indexPath
    {
   if (_periferalDevices.count >0 )
  {
    CBPeripheral *peripheral =[_periferalDevices objectAtIndex:indexPath.row];
    _discoveredPeripheral = peripheral;
    NSLog(@"selected peripheral - %@",peripheral);
   [self.centralManager connectPeripheral:peripheral options:nil];
    [_periferalDevices removeObject:peripheral];
    NSLog(@"selected peripheral state - %d",peripheral.state);

     [tableView reloadData];

   NSString *strID = [NSString   stringWithFormat:@"%@",_discoveredPeripheral.identifier];
     strID = [strID substringFromIndex: [strID length] - 20];
     UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"conected" message:@""  
      delegate:self cancelButtonTitle:@"ok" otherButtonTitles: nil];
    [alert show];

 }

  }
  i am getting the errors as specified below
 2014-10-14 11:36:46.697 Bluetooth_iph[1705:368603] selected peripheral - MetaWatch 56
 2014-10-14 11:36:50.385 Bluetooth_iph[1705:368603] -[__NSCFString identifier]: unrecognized    
 selector sent to instance 0x14e67190
 2014-10-14 11:36:50.387 Bluetooth_iph[1705:368603] *** Terminating app due to uncaught   
 exception 'NSInvalidArgumentException', reason: '-[__NSCFString identifier]: unrecognized    
 selector sent to instance 0x14e67190'

  *** First throw call stack:

  (0x28b47f87 0x36576c77 0x28b4d37d 0x28b4b259 0x28a7cd68 0x28818c7d 0xfb6db 0x2c0dcc57  
  0x2c18e2d7 0x2c040449 0x2bfbc30f 0x28b0e845 0x28b0bf29 0x28b0c32b 0x28a59db1 0x28a59bc3  
   0x2fdbc051 0x2c025a31 0xfc57d 0x36b12aaf)

    libc++abi.dylib: terminating with uncaught exception of type NSException 

我已经给出了上面的代码,但是连接外设的委托方法不是working.please,提前帮我谢谢

EN

回答 1

Stack Overflow用户

发布于 2014-10-14 14:09:45

您应该添加peripheral对象并在cellForRowAtIndexPath中检索名称,而不是将peripheral.name添加到periferalDefices数组中。然后,您的didSelectRowAtIndexPath将与您编写的一样

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26353576

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档