【发布时间】:2016-05-13 14:35:37
【问题描述】:
我是 IOS 编程新手,我尝试将 BLE 外围设备存储在 tableView 中。 我唯一做的就是将它存储在一个 NSArray 中:这是我的代码:
-(void) centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *,id> *)advertisementData RSSI:(NSNumber *)RSSI
{
NSLog (@"Discovered peripheral: %@", [peripheral name]);
NSLog (@"peripheral services before connected: %@, RSSI Value : %@",advertisementData, RSSI);
NSLog(@"adversting data %@",[NSString stringWithFormat:@"%@",[advertisementData description]]);
NSString *localName = [advertisementData objectForKey:CBAdvertisementDataLocalNameKey];
NSArray *foundArray = [advertisementData objectForKey:CBAdvertisementDataLocalNameKey];;
[self.centralManager stopScan];
NSLog(@"Scanning stopped");
NSLog(@"foundArray is %@",foundArray);
self.tblfound = [[UITableView alloc] initWithFrame:CGRectMake(0, 10, 320, 300) style:UITableViewStylePlain];
self.tblfound.dataSource = self;
self.tblfound.delegate = self;
[self.view addSubview:self.tblfound];
}
- (void)centralManager:(CBCentralManager *)central didRetrievePeripherals:(NSArray *)peripherals
{
for (CBPeripheral *peripheral in peripherals) {
NSLog(@"Retrieved Peripheral %@", peripheral.name);
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// static NSString *tableIdentifier = @"BLEDeviceList";
//CBPeripheral *peripheral = [self.BTLEDevices objectAtIndex:indexPath.row];
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
cell.selectionStyle = UITableViewCellSelectionStyleGray;
}
cell.textLabel.text = [foundArray objectAtIndex:indexPath.row];
return cell;
}
我不确定我是否正确,但我没有找到任何可以帮助我的示例。如果您有任何可以帮助我的链接或代码,请不要犹豫。 非常感谢 ==) !
【问题讨论】:
-
我建议您首先阅读有关如何使用
UITableView的教程(已设置文本)。然后,您可以增加为自己提供外围设备阵列的复杂性。
标签: objective-c tableview bluetooth-lowenergy cbperipheral