-
Notifications
You must be signed in to change notification settings - Fork 44
/
BeaconBroadcast.m
136 lines (101 loc) · 4.03 KB
/
BeaconBroadcast.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
#import "BeaconBroadcast.h"
@interface BeaconBroadcast()
@property (nonatomic, strong) CLLocationManager *locationManager;
@property (nonatomic, strong) CLBeaconRegion *beaconRegion;
@property (nonatomic, strong) CBPeripheralManager *peripheralManager;
@end
@implementation BeaconBroadcast
RCT_EXPORT_MODULE()
RCT_EXPORT_METHOD(startSharedAdvertisingBeaconWithString:(NSString *)uuid major:(int)major minor:(int)minor identifier:(NSString *)identifier)
{
[[BeaconBroadcast sharedInstance] startAdvertisingBeaconWithString: uuid major: major minor: minor identifier: identifier];
}
RCT_EXPORT_METHOD(stopSharedAdvertisingBeacon)
{
[[BeaconBroadcast sharedInstance] stopAdvertisingBeacon];
}
#pragma mark - Common
+ (id)sharedInstance
{
// structure used to test whether the block has completed or not
static dispatch_once_t p = 0;
// initialize sharedObject as nil (first call only)
__strong static id _sharedObject = nil;
// executes a block object once and only once for the lifetime of an application
dispatch_once(&p, ^{
_sharedObject = [[self alloc] init];
});
// returns the same object each time
return _sharedObject;
}
- (void)startAdvertisingBeaconWithString:(NSString *)uuid major:(int)major minor:(int)minor identifier:(NSString *)identifier
{
NSLog(@"Turning on advertising...");
[self createBeaconRegionWithString:uuid major:major minor:minor identifier:identifier];
if (!self.peripheralManager)
self.peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self queue:nil options:nil];
[self turnOnAdvertising];
}
- (void)stopAdvertisingBeacon
{
if ([self.peripheralManager isAdvertising]) {
[self.peripheralManager stopAdvertising];
}
NSLog(@"Turned off advertising.");
}
- (void)createBeaconRegionWithString:(NSString *)uuid major:(int)major minor:(int)minor identifier:(NSString *)identifier
{
// if (self.beaconRegion)
// return;
NSUUID *proximityUUID = [[NSUUID alloc] initWithUUIDString:uuid];
self.beaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:proximityUUID major:major minor:minor identifier:identifier];
self.beaconRegion.notifyEntryStateOnDisplay = YES;
}
- (void)createLocationManager
{
if (!self.locationManager) {
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
}
}
#pragma mark - Beacon advertising
- (void)turnOnAdvertising
{
if (self.peripheralManager.state != CBPeripheralManagerStatePoweredOn) {
NSLog(@"Peripheral manager is off.");
return;
}
time_t t;
srand((unsigned) time(&t));
UInt16 major = [self.beaconRegion.major unsignedShortValue];
UInt16 minor = [self.beaconRegion.minor unsignedShortValue];
NSLog(@"Minor HHAHHAHAHHAHAHAHHAHAHHA: %d", minor);
CLBeaconRegion *region = [[CLBeaconRegion alloc] initWithProximityUUID:self.beaconRegion.proximityUUID
major: major
minor: minor
identifier:self.beaconRegion.identifier];
NSDictionary *beaconPeripheralData = [region peripheralDataWithMeasuredPower:nil];
[self.peripheralManager startAdvertising:beaconPeripheralData];
NSLog(@"Turning on advertising for region: %@.", region);
}
#pragma mark - Beacon advertising delegate methods
- (void)peripheralManagerDidStartAdvertising:(CBPeripheralManager *)peripheralManager error:(NSError *)error
{
if (error) {
NSLog(@"Couldn't turn on advertising: %@", error);
return;
}
if (peripheralManager.isAdvertising) {
NSLog(@"Turned on advertising.");
}
}
- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheralManager
{
if (peripheralManager.state != CBPeripheralManagerStatePoweredOn) {
NSLog(@"Peripheral manager is off.");
return;
}
NSLog(@"Peripheral manager is on.");
[self turnOnAdvertising];
}
@end