IOS:当用户选择行时,从表视图连接外围设备
我正在开发corebluetooth应用程序。当我开始在tableview中查找设备时,以及当我使用索引路径上的did select行中的did connect委托方法选择要连接的设备时,我在控制台中获得了错误信息,下面指定了查找和连接设备的代码 用于发现外围设备IOS:当用户选择行时,从表视图连接外围设备,ios,uitableview,core-bluetooth,Ios,Uitableview,Core Bluetooth,我正在开发corebluetooth应用程序。当我开始在tableview中查找设备时,以及当我使用索引路径上的did select行中的did connect委托方法选择要连接的设备时,我在控制台中获得了错误信息,下面指定了查找和连接设备的代码 用于发现外围设备 - (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral: (CBPeripheral *)peripheral adve
- (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];
}
用于连接外围设备
- (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]]];
}
下面的代码让我感到困惑,在选定行中选择特定外围设备后,我们必须编写哪些代码来连接表视图中发现的设备
- (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
我已经给出了上述代码,但是连接外围设备委托方法不起作用。请帮助我提前感谢,而不是添加
外围设备。名称到外围设备属性数组中,您应该添加外围设备对象,并在cellForRowAtIndexPath
中检索名称。然后,您的didSelectRowAtIndexPath
将与您编写的一样我能够获取设备的名称,但无法连接到设备,在我给[self.centralManager connectPeripheral:peripheral options:nil]时出现一些错误;在代码中,我已经用错误更新了代码,请在上面找到并帮助感谢,这行代码给出了错误。它表示您正在访问NSString上的identifier属性(您不能这样做),因此在某个地方您混淆了外围设备和字符串添加异常断点或查看堆栈跟踪以查看哪一行有问题。我看不到您在此处显示的代码中存在问题。您是否更改了[\u periferalDevices addObject:peripheral.name]
到[\u periferalDevices addObject:peripheral]代码>?我也改变了这一点,但目的是获得如下错误..'NSInvalidArgumentException',原因:'-[CBPeripheral isEqualToString:]:发送到实例0x175a3ad0的无法识别的选择器'您的数组现在包含CBPeripherals
,而不是字符串,因此您需要在该行中使用peripheral.identifier