http://www.oschina.net/code/snippet_2367905_49028
OS BLE 4.0 实现搜索周边蓝牙设备并显示其信号强度(RSSI)
源码:https://git.oschina.net/laughingzhong/MyBluetoothDemo.git
1. [代码][Objective-C]代码 跳至 [1] [全屏预览]
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
|
#import "ViewController.h"
#import "TableViewCell.h"
#import "PeripheralViewController.h"
#define ScanTimeInterval 1.0
@interface
ViewController ()
@property
(
nonatomic
,strong)
NSMutableArray
*devicesArray;
@property
(
nonatomic
,strong) CBCentralManager *centralManager;
@property
(
nonatomic
,strong) CBPeripheral *selectedPeripheral;
@property
(
nonatomic
,strong)
NSTimer
*scanTimer;
@end
@implementation
ViewController
- (
void
)dealloc
{
_devicesArray =
nil
;
_centralManager =
nil
;
_selectedPeripheral =
nil
;
_scanTimer =
nil
;
}
- (
void
)viewDidLoad {
[
super
viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_devicesArray = [[
NSMutableArray
alloc] initWithCapacity:1];
[
self
initWithLeftBarButton];
[
self
initWithRightBarButton];
[
self
initWithTableView];
[
self
initWithCBCentralManager];
}
#pragma mark - UI
- (
void
)initWithLeftBarButton
{
UIButton
*button = [
UIButton
buttonWithType:
UIButtonTypeRoundedRect
];
[button setFrame:CGRectMake(0.0, 0.0, 60.0, 40.0)];
[button setBackgroundColor:[
UIColor
clearColor]];
[button setTitle:@
"搜索"
forState:
UIControlStateNormal
];
[button addTarget:
self
action:
@selector
(startScanPeripherals) forControlEvents:
UIControlEventTouchUpInside
];
UIBarButtonItem
*item = [[
UIBarButtonItem
alloc] initWithCustomView:button];
[
self
.navigationItem setLeftBarButtonItem:item];
}
- (
void
)initWithRightBarButton
{
UIButton
*button = [
UIButton
buttonWithType:
UIButtonTypeRoundedRect
];
[button setFrame:CGRectMake(0.0, 0.0, 60.0, 40.0)];
[button setBackgroundColor:[
UIColor
clearColor]];
[button setTitle:@
"停止"
forState:
UIControlStateNormal
];
[button addTarget:
self
action:
@selector
(stopScan) forControlEvents:
UIControlEventTouchUpInside
];
UIBarButtonItem
*item = [[
UIBarButtonItem
alloc] initWithCustomView:button];
[
self
.navigationItem setRightBarButtonItem:item];
}
- (
void
)initWithTableView
{
if
(!_tableView) {
_tableView = [[
UITableView
alloc] initWithFrame:
self
.view.bounds style:
UITableViewStylePlain
];
[_tableView setDelegate:
self
];
[_tableView setDataSource:
self
];
[_tableView setBackgroundColor:[
UIColor
clearColor]];
[_tableView setSeparatorStyle:
UITableViewCellSeparatorStyleSingleLine
];
[_tableView setTranslatesAutoresizingMaskIntoConstraints:
NO
];
}
if
(_tableView && _tableView.superview !=
self
.view) {
[
self
.view addSubview:_tableView];
NSArray
*h = [
NSLayoutConstraint
constraintsWithVisualFormat:@
"H:|[_tableView]|"
options:0 metrics:
nil
views:
NSDictionaryOfVariableBindings
(_tableView)];
NSArray
*v = [
NSLayoutConstraint
constraintsWithVisualFormat:@
"V:|[_tableView]|"
options:0 metrics:
nil
views:
NSDictionaryOfVariableBindings
(_tableView)];
[
self
.view addConstraints:h];
[
self
.view addConstraints:v];
}
}
#pragma mark - ScanTimer
- (
void
)startScanPeripherals
{
if
(!_scanTimer) {
_scanTimer = [
NSTimer
timerWithTimeInterval:ScanTimeInterval target:
self
selector:
@selector
(scanForPeripherals) userInfo:
nil
repeats:
YES
];
[[
NSRunLoop
mainRunLoop] addTimer:_scanTimer forMode:
NSDefaultRunLoopMode
];
}
if
(_scanTimer && !_scanTimer.valid) {
[_scanTimer fire];
}
}
- (
void
)stopScan
{
if
(_scanTimer && _scanTimer.valid) {
[_scanTimer invalidate];
_scanTimer =
nil
;
}
[_centralManager stopScan];
}
#pragma mark - CBCentralManager
- (
void
)initWithCBCentralManager
{
if
(!_centralManager) {
dispatch_queue_t queue = dispatch_get_main_queue();
_centralManager = [[CBCentralManager alloc] initWithDelegate:
self
queue:queue options:@{CBCentralManagerOptionShowPowerAlertKey:
@YES
}];
[_centralManager setDelegate:
self
];
}
}
- (
void
)scanForPeripherals
{
if
(_centralManager.state == CBCentralManagerStateUnsupported) {
//设备不支持蓝牙
}
else
{
//设备支持蓝牙连接
if
(_centralManager.state == CBCentralManagerStatePoweredOn) {
//蓝牙开启状态
//[_centralManager stopScan];
[_centralManager scanForPeripheralsWithServices:
nil
options:@{CBCentralManagerScanOptionAllowDuplicatesKey:[
NSNumber
numberWithBool:
NO
]}];
}
}
}
- (
void
)connectPeripheral
{
}
- (
void
)didReceiveMemoryWarning {
[
super
didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - UITableView Datasource && Delegate
- (
NSInteger
)numberOfSectionsInTableView:(
UITableView
*)tableView
{
return
1;
}
- (
NSInteger
)tableView:(
UITableView
*)tableView numberOfRowsInSection:(
NSInteger
)section
{
return
_devicesArray.count;
}
- (
NSString
*)tableView:(
UITableView
*)tableView titleForHeaderInSection:(
NSInteger
)section
{
if
(section == 0) {
return
@
"Peripherals Nearby"
;
}
else
{
return
nil
;
}
}
- (CGFloat)tableView:(
UITableView
*)tableView heightForRowAtIndexPath:(
NSIndexPath
*)indexPath
{
return
50.0;
}
- (
UITableViewCell
*)tableView:(
UITableView
*)tableView cellForRowAtIndexPath:(
NSIndexPath
*)indexPath
{
static
NSString
*cellID = @
"cellID"
;
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if
(!cell) {
cell = [[TableViewCell alloc] initWithStyle:
UITableViewCellStyleValue1
reuseIdentifier:cellID];
}
NearbyPeripheralInfo *info = [_devicesArray objectAtIndex:indexPath.row];
[cell setPeripheral:info];
return
cell;
}
- (
void
)tableView:(
UITableView
*)tableView didSelectRowAtIndexPath:(
NSIndexPath
*)indexPath
{
if
(_centralManager.state == CBCentralManagerStateUnsupported) {
//设备不支持蓝牙
}
else
{
//设备支持蓝牙连接
if
(_centralManager.state == CBCentralManagerStatePoweredOn) {
//蓝牙开启状态
//连接设备
NearbyPeripheralInfo *info = [_devicesArray objectAtIndex:indexPath.row];
[_centralManager connectPeripheral:info.peripheral options:@{CBConnectPeripheralOptionNotifyOnConnectionKey:
@YES
,CBConnectPeripheralOptionNotifyOnNotificationKey:
@YES
,CBConnectPeripheralOptionNotifyOnDisconnectionKey:
@YES
}];
}
}
}
#pragma mark - CBCentralManager Delegate
- (
void
)centralManagerDidUpdateState:(CBCentralManager *)central
{
switch
(central.state) {
case
CBCentralManagerStatePoweredOff:
NSLog
(@
"CBCentralManagerStatePoweredOff"
);
break
;
case
CBCentralManagerStatePoweredOn:
NSLog
(@
"CBCentralManagerStatePoweredOn"
);
break
;
case
CBCentralManagerStateResetting:
NSLog
(@
"CBCentralManagerStateResetting"
);
break
;
case
CBCentralManagerStateUnauthorized:
NSLog
(@
"CBCentralManagerStateUnauthorized"
);
break
;
case
CBCentralManagerStateUnknown:
NSLog
(@
"CBCentralManagerStateUnknown"
);
break
;
case
CBCentralManagerStateUnsupported:
NSLog
(@
"CBCentralManagerStateUnsupported"
);
break
;
default
:
break
;
}
}
//发现蓝牙设备
- (
void
)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(
NSDictionary
*)advertisementData RSSI:(
NSNumber
*)RSSI
{
// NSLog(@"\nperipheral is :\n%@\nadvertisementData is :\n%@\nRSSI is :%d",peripheral,advertisementData,[RSSI intValue]);
BOOL
isExist =
NO
;
NearbyPeripheralInfo *info = [[NearbyPeripheralInfo alloc] init];
info.peripheral = peripheral;
info.advertisementData = advertisementData;
info.RSSI = RSSI;
if
(_devicesArray.count == 0) {
[_devicesArray addObject:info];
NSIndexPath
*path = [
NSIndexPath
indexPathForRow:0 inSection:0];
[_tableView insertRowsAtIndexPaths:@[path] withRowAnimation:
UITableViewRowAnimationFade
];
}
else
{
for
(
int
i = 0;i < _devicesArray.count;i++) {
NearbyPeripheralInfo *originInfo = [_devicesArray objectAtIndex:i];
CBPeripheral *per = originInfo.peripheral;
if
([peripheral.identifier.UUIDString isEqualToString:per.identifier.UUIDString]) {
isExist =
YES
;
[_devicesArray replaceObjectAtIndex:i withObject:info];
[_tableView reloadData];
}
}
if
(!isExist) {
[_devicesArray addObject:info];
NSIndexPath
*path = [
NSIndexPath
indexPathForRow:(_devicesArray.count - 1) inSection:0];
[_tableView insertRowsAtIndexPaths:@[path] withRowAnimation:
UITableViewRowAnimationFade
];
}
}
}
//连接蓝牙设备成功
- (
void
)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
NSLog
(@
"%s"
,__FUNCTION__);
[
self
stopScan];
_selectedPeripheral = peripheral;
PeripheralViewController *viewController = [[PeripheralViewController alloc] initWithNibName:
nil
bundle:
nil
];
viewController.currentPeripheral = _selectedPeripheral;
[
self
.navigationController pushViewController:viewController animated:
YES
];
}
//连接蓝牙设备失败
- (
void
)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(
NSError
*)error
{
NSLog
(@
"%s"
,__FUNCTION__);
}
//断开连接
- (
void
)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(
NSError
*)error
{
NSLog
(@
"%s"
,__FUNCTION__);
}
@end
|