storyBoard里拖tableView、tableViewCell。记得拖代理
大致思路:注册中心管理设备—》判断蓝牙是否开启——>扫描—》扫描发现设备—》加到数组里,刷新—》点击cell、连接结束扫描—》连接上外设、连接出错、断开连接三个方法。
#import "ViewController.h"
#import <CoreBluetooth/CoreBluetooth.h>
#import <CoreLocation/CoreLocation.h>
@interface ViewController ()<CBCentralManagerDelegate,UITableViewDataSource, UITableViewDelegate>
//展示外设
@property (weak, nonatomic) IBOutlet UITableView *tableView;
//中央设备管理者
@property (nonatomic, strong)CBCentralManager *mgr;
//用于添加扫描到的外设
@property (nonatomic, strong)NSMutableArray *dataArr;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//判断中央设备蓝牙状态
//tableview注册
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
self.mgr = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
// NSLog(@"%@", self.mgr);
// Do any additional setup after loading the view, typically from a nib.
}
// 扫描外设
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
//将外设对象加到容器里
[self.dataArr addObject:peripheral];
[self.tableView reloadData];
NSLog(@"%@", RSSI);
NSLog(@"%@", advertisementData);
}
// 这个方法是必须执行的!!!!!!!!!!
- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
NSLog(@"------%ld", central.state);
// 判读是否是枚举。如果是,就开始扫描
if ([self panduan]) {
[self.mgr scanForPeripheralsWithServices:nil options:nil];
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
CBPeripheral *peripheral = (CBPeripheral *)self.dataArr[indexPath.row];
// cell.textLabel.text = @"nnnnnn";
// cell.textLabel.text = [NSString stringWithFormat:@"%@", peripheral.RSSI];
return cell;
}
// 点击cell出发的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//链接蓝牙外设
[self.mgr connectPeripheral:((CBPeripheral *)self.dataArr[indexPath.row]) options:nil];
//结束扫描
[self.mgr stopScan];
}
//链接上外设
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
NSLog(@"%s", __func__);
}
//链接出错
- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
{
NSLog(@"%s", __func__);
}
// 断开连接
- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
{
NSLog(@"%s", __func__);
}
//承载容器的懒加载
- (NSMutableArray *)dataArr
{
if (!_dataArr) {
_dataArr = [NSMutableArray array];
}
return _dataArr;
}
//判断蓝牙状态是否可用
- (BOOL)panduan
{
NSLog(@"%@", self.mgr);
if (self.mgr.state == CBCentralManagerStatePoweredOn) {
return YES;
}
return NO;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
#import "ViewController.h"
#import <CoreBluetooth/CoreBluetooth.h>
#import <CoreLocation/CoreLocation.h>
@interface ViewController ()<CBCentralManagerDelegate,UITableViewDataSource, UITableViewDelegate>
//展示外设
@property (weak, nonatomic) IBOutlet UITableView *tableView;
//中央设备管理者
@property (nonatomic, strong)CBCentralManager *mgr;
//用于添加扫描到的外设
@property (nonatomic, strong)NSMutableArray *dataArr;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//判断中央设备蓝牙状态
//tableview注册
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
self.mgr = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
// NSLog(@"%@", self.mgr);
// Do any additional setup after loading the view, typically from a nib.
}
// 扫描外设
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
//将外设对象加到容器里
[self.dataArr addObject:peripheral];
[self.tableView reloadData];
NSLog(@"%@", RSSI);
NSLog(@"%@", advertisementData);
}
// 这个方法是必须执行的!!!!!!!!!!
- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
NSLog(@"------%ld", central.state);
// 判读是否是枚举。如果是,就开始扫描
if ([self panduan]) {
[self.mgr scanForPeripheralsWithServices:nil options:nil];
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
CBPeripheral *peripheral = (CBPeripheral *)self.dataArr[indexPath.row];
// cell.textLabel.text = @"nnnnnn";
// cell.textLabel.text = [NSString stringWithFormat:@"%@", peripheral.RSSI];
return cell;
}
// 点击cell出发的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//链接蓝牙外设
[self.mgr connectPeripheral:((CBPeripheral *)self.dataArr[indexPath.row]) options:nil];
//结束扫描
[self.mgr stopScan];
}
//链接上外设
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
NSLog(@"%s", __func__);
}
//链接出错
- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
{
NSLog(@"%s", __func__);
}
// 断开连接
- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
{
NSLog(@"%s", __func__);
}
//承载容器的懒加载
- (NSMutableArray *)dataArr
{
if (!_dataArr) {
_dataArr = [NSMutableArray array];
}
return _dataArr;
}
//判断蓝牙状态是否可用
- (BOOL)panduan
{
NSLog(@"%@", self.mgr);
if (self.mgr.state == CBCentralManagerStatePoweredOn) {
return YES;
}
return NO;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end