-
Notifications
You must be signed in to change notification settings - Fork 1
/
MiBandAlertController.m
61 lines (56 loc) · 1.76 KB
/
MiBandAlertController.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
#import <CoreBluetooth/CoreBluetooth.h>
#import "MiBandAlertController.h"
@interface MiBandAlertController ()
@property (nonatomic) MBALPeripheral *myPeripheral;
@end
static __strong __typeof(MBALCentralManager *) centralManager;
@implementation MiBandAlertController
- (MBALCentralManager *)currentCentralManager
{
return centralManager;
}
+ (MiBandAlertController *)mibandSharedInstance {
static __strong MiBandAlertController *wSharedInstance;
if (wSharedInstance == nil){
@synchronized(self){
if (wSharedInstance == nil){
wSharedInstance = [self new];
return wSharedInstance;
}
}
}
return wSharedInstance;
}
- (void)loadCentralManager
{
// create centralManager.
centralManager = [MBALCentralManager sharedCentralManager];
// get peripheral on powern on, after scan.
[centralManager setPoweredOnBlock:^(MBALCentralManager *manager) {
[self scanForMiBand];
}];
[centralManager setPoweredOffBlock:^(MBALCentralManager *manager) {
[manager stopScan];
}];
[centralManager setDisconnectedBlock:^(MBALPeripheral *peripheral, NSError *error) {
// null.
}];
}
- (void)scanForMiBand
{
// get peripheral after scan.
[centralManager scanForMiBandWithBlock:^(MBALPeripheral *miband, NSNumber *RSSI, NSError *error) {
[centralManager stopScan];
self->_myPeripheral = centralManager.peripherals[0];
[centralManager connectPeripheral:_myPeripheral withResultBlock:^(MBALPeripheral *peripheral, NSError *error) {
if (error) {
return NSLog(@"[MiBandAlertController] ERROR -> %@", [error localizedDescription]);
}
}];
}];
}
- (MBALPeripheral *)dkPeripheral
{
return _myPeripheral;
}
@end