我正在viewController.m
中扫描BLE - (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
discoveredPeripherals=[[NSMutableArray alloc]init];
_CBManager=[[CBCentralManager alloc]initWithDelegate:self queue:nil];
NSDictionary *options=@{CBCentralManagerScanOptionAllowDuplicatesKey:[NSNumber numberWithBool:NO]};
NSArray *servicesUUIDsList=@[[CBUUID UUIDWithString:IMMEDIATE_ALERT_SERVICE_UUID],[CBUUID UUIDWithString:TX_POWER_LEVEL_SERVICE_UUID],[CBUUID UUIDWithString:BATTERY_LEVEL_SERVICE_UUID]];
[self.CBManager scanForPeripheralsWithServices:servicesUUIDsList options:options];
}
可用的蓝牙设备将扫描,我正在通过tableView显示它们。
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
CBPeripheral *peripheral;
static NSString *cellID=@"Devices";
cell=(UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellID];
if (!cell)
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
peripheral=[discoveredPeripherals objectAtIndex:indexPath.row];
NSString *string1=(__bridge_transfer NSString *)CFUUIDCreateString(NULL, (__bridge CFUUIDRef)(peripheral.identifier));
cell.textLabel.text=@"LaTag";
[cell.textLabel setTextColor:[UIColor greenColor]];
cell.detailTextLabel.text=string1;
return cell;
}
如果用户选择任何人BLE,则connect方法将调用,设备将连接。通过传递我在另一个视图中连接的uuid地址。
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
NSString *tag=(__bridge_transfer NSString *)CFUUIDCreateString(NULL, (__bridge CFUUIDRef)(peripheral.identifier));
NSLog(@"%@", tag);
if ([tag isEqual:address])
{
self.currentPeripheral=peripheral;
[self.CBManager connectPeripheral:peripheral options:Nil];
sleep(3);
}
}
问题是,如果我再次返回上一个视图,它将扫描并显示之前连接的标签。请帮助不要扫描已连接的标签。