CoreBlueTootch的实现
2015-06-18 10:23
357 查看
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
大致思路:注册中心管理设备—》判断蓝牙是否开启——>扫描—》扫描发现设备—》加到数组里,刷新—》点击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
相关文章推荐
- android通过蓝牙接收文件打开时无法自动选择合适的应用程序
- Android提高之蓝牙隐藏API探秘
- Android编程之蓝牙测试实例
- Android提高之蓝牙传感应用实例
- 蓝牙与红外传输
- 蓝牙-信息时代的黑马
- 玩转Blueteeth Chat蓝牙交友
- SyncML在蓝牙车载免提装置中的应用
- 手机指令大全,需要可以看看
- 蓝牙英文-译文
- Android蓝牙开发浅谈
- 安卓通过蓝牙发送文件
- 安卓蓝牙传送文件
- Android蓝牙开发
- Core Bluetooth for iOS 6
- windows phone 8中使用蓝牙
- bluetooth控制Arduino单片机的红外发射装置(二)——Android部分
- 宏佳电子 CC2540 USB DONGLE 使用 BTool 调试BLE 说明
- 安卓 手机和蓝牙通信连接