蓝牙扫描的坑

今天公司有同事要求手机连接一个蓝牙设备, 要求是手机自动连接过去.简单翻了一下Apple的文档还找了几个网页,觉得了然于心了.于是先写出了进行扫描的代码:

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

#import "ViewController.h"
#import <CoreBluetooth/CoreBluetooth.h>
@interface ViewController () <CBCentralManagerDelegate> {
CBCentralManager *bleManager_;
}
@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
bleManager_ = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
[bleManager_ scanForPeripheralsWithServices:nil options:nil];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[bleManager_ stopScan];
}

- (void)centralManagerDidUpdateState:(CBCentralManager *)central {
NSLog( @"%@", central );
switch ( central.state ) {
case CBCentralManagerStateUnknown: {
NSLog( @"%@", @"CBCentralManagerStateUnknown" );
break;
}
case CBCentralManagerStateResetting: {
NSLog( @"%@", @"CBCentralManagerStateResetting" );
break;
}
case CBCentralManagerStateUnsupported: {
NSLog( @"%@", @"CBCentralManagerStateUnsupported" );
break;
}
case CBCentralManagerStateUnauthorized: {
NSLog( @"%@", @"CBCentralManagerStateUnauthorized" );
break;
}
case CBCentralManagerStatePoweredOff: {
NSLog( @"%@", @"CBCentralManagerStatePoweredOff" );
break;
}
case CBCentralManagerStatePoweredOn: {
NSLog( @"%@", @"CBCentralManagerStatePoweredOn" );

break;
}
}
}

- (void)centralManager:(CBCentralManager *)central
didDiscoverPeripheral:(CBPeripheral *)peripheral
advertisementData:(NSDictionary<NSString *, id> *)advertisementData
RSSI:(NSNumber *)RSSI {
NSLog( @"BLE found %@", peripheral.name );
}

- (void)centralManager:(CBCentralManager *)central
didConnectPeripheral:(CBPeripheral *)peripheral {
NSLog( @"BLE connected %@", peripheral.name );
}

- (void)centralManager:(CBCentralManager *)central
didFailToConnectPeripheral:(CBPeripheral *)peripheral
error:(nullable NSError *)error {
NSLog( @"BLE connected %@ faild %@", peripheral.name, error );
}

@end

真机运行, 发现打印了 CBCentralManagerStatePoweredOn ,但是之后就没有后文了. 发现外设的代理方法没有调用.但是直接通过手机进行扫描,能够发现设备.反复尝试,还是没有得到预期的结果.

后来想到,自己尝试在手机上反复开关蓝牙让列表刷新,注意到蓝牙开关是有一定时间的.结合自己在代码中:

1
2
bleManager_ = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
[bleManager_ scanForPeripheralsWithServices:nil options:nil];

初始化了直接就开始扫描, 可能是由于蓝牙的Manager还没有准备好吧,于是尝试修改这个开始扫描到判定Manager的状态为PowerOn中:

1
2
3
4
5
6
case CBCentralManagerStatePoweredOn: {
NSLog( @"%@", @"CBCentralManagerStatePoweredOn" );
[bleManager_ scanForPeripheralsWithServices:nil options:nil];

break;
}

再进行打印,发现终于出现了各个外设了.