我正在使用corebluetooth应用程序。当我开始在tableview中发现设备时,当我选择要连接的设备时使用did connect delegate方法在索引路径中选择行我得到并且在控制台中出错我的代码用于发现和连接设备是指定如下
用于发现外围设备
- (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
我已经给了他上面的代码但连接外围委托方法不起作用。请帮助我提前谢谢
答案 0 :(得分:0)
您应该添加peripheral.name
对象并在periferalDefices
中检索名称,而不是将peripheral
添加到cellForRowAtIndexPath
数组中。那么你的didSelectRowAtIndexPath
就像你写的那样