-
Notifications
You must be signed in to change notification settings - Fork 25
/
ABBluetoothViewController.m
147 lines (114 loc) · 4.02 KB
/
ABBluetoothViewController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
//
// ABBluetoothViewController.m
// TestSDKPod
//
// Created by liaojinhua on 14-5-8.
// Copyright (c) 2014年 AprilBrother. All rights reserved.
//
#import "ABBluetoothViewController.h"
#import "ABModifyViewController.h"
@interface ABBluetoothViewController () <ABBluetoothManagerDelegate>
@property (nonatomic, strong) ABBluetoothManager *beaconManager;
@property (nonatomic, strong) NSMutableArray *tableData;
@end
@implementation ABBluetoothViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.beaconManager = [[ABBluetoothManager alloc] init];
self.beaconManager.delegate = self;
_tableData = [NSMutableArray array];
self.refreshControl = [[UIRefreshControl alloc] init];
[self.refreshControl addTarget:self
action:@selector(startRangeBeacons)
forControlEvents:UIControlEventValueChanged];
[self.beaconManager addCustomBeaconNameFilter:@"aikaka"];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self startRangeBeacons];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[self stopRangeBeacons];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"ModifySegue"]) {
ABModifyViewController *vc = segue.destinationViewController;
vc.beacon = sender;
}
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_tableData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
ABBeacon *beacon = _tableData[indexPath.row];
CBPeripheral *peripheral = beacon.peripheral;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"beaconCell"];
cell.textLabel.text = peripheral.name;
NSMutableString *detailString = [[NSMutableString alloc] init];
// fot the newest EEK iBeacon, you can get Major, Minor, Mac Address , battery level when scan iBeacons.
if (beacon.major) {
[detailString appendFormat:@"Major:%@ ",beacon.major];
}
if (beacon.minor) {
[detailString appendFormat:@"Minor:%@ ",beacon.minor];
}
if (beacon.macAddress) {
[detailString appendFormat:@"Mac:%@ ",beacon.macAddress];
}
if (beacon.batteryLevel) {
[detailString appendFormat:@"Battery:%@ ", beacon.batteryLevel];
}
if (beacon.rssi == 127) {
cell.userInteractionEnabled = NO;
cell.textLabel.textColor = [UIColor lightGrayColor];
[detailString appendFormat:@"rssi : "];
} else {
cell.userInteractionEnabled = YES;
cell.textLabel.textColor = [UIColor blackColor];
[detailString appendFormat:@"rssi : %ld", (long)beacon.rssi];
}
cell.detailTextLabel.text = detailString;
cell.detailTextLabel.numberOfLines = 0;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:@"ModifySegue" sender:_tableData[indexPath.row]];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 60;
}
#pragma mark - ABBeaconManagerDelegate
- (void)beaconManager:(ABBeaconManager *)manager didDiscoverBeacons:(NSArray *)beacons{
[self.refreshControl endRefreshing];
[_tableData removeAllObjects];
[_tableData addObjectsFromArray:beacons];
[self.tableView reloadData];
}
#pragma mark - Custom methods
- (void)startRangeBeacons
{
[self stopRangeBeacons];
[_beaconManager startAprilBeaconsDiscovery];
// [_beaconManager startAprilSensorsDiscovery]; // only find april sensors
// [_beaconManager startAprilLightDiscovery]; // only find april lights
}
- (void)stopRangeBeacons
{
[_beaconManager stopAprilBeaconDiscovery];
}
@end