BlueToolManage.m
2.91 KB
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
//
// BuleToolManage.m
// metroApp
//
// Created by 赵世强 on 2018/3/26.
// Copyright © 2018年 Facebook. All rights reserved.
//
#import "BlueToolManage.h"
@implementation BlueToolManage
{
CLLocationManager *_locationManager;
CLBeaconRegion *_region;
NSArray *_detectedBeacons;
CLBeacon *beacon_current;
NSTimer *timer;
}
RCT_EXPORT_MODULE(BlueToolManage);
- (instancetype)init
{
if(self = [super init]){
[self initLocationManager];
[self initBeaconRegion];
[self initDetectedBeaconsList];
[self startBeaconRanging];
}
return self;
}
- (NSArray<NSString *> *)supportedEvents {
return @[@"BlueToolData"]; //这里返回的将是你要发送的消息名的数组。
}
RCT_EXPORT_METHOD(startTimer:(NSString *)timeInterval)
{
timer = [NSTimer scheduledTimerWithTimeInterval:[timeInterval intValue] target:self selector:@selector(postCurrentLocation) userInfo:nil repeats:YES];
[timer fire];
}
RCT_EXPORT_METHOD(stopTimer)
{
[timer invalidate];
timer = nil;
}
- (void)postCurrentLocation{
[self sendEventWithName:@"BlueToolData" body:@{@"deviceCode": [NSString stringWithFormat:@"E2C56DB5-DFFB-48D2-B060-D0F5A71096E0|%@|%@",beacon_current.major,beacon_current.minor],@"rss": [NSString stringWithFormat:@"%ld",(long)beacon_current.rssi]}];
}
#pragma mark Init Beacons
- (void) initLocationManager{
if (!_locationManager) {
_locationManager = [[CLLocationManager alloc] init];
_locationManager.delegate = self;
[self checkLocationAccessForRanging];
}
}
- (void) initBeaconRegion{
if (_region)
return;
NSUUID *proximityUUID = [[NSUUID alloc] initWithUUIDString:@"E2C56DB5-DFFB-48D2-B060-D0F5A71096E0"];
_region = [[CLBeaconRegion alloc] initWithProximityUUID:proximityUUID identifier:@"cell"];
_region.notifyEntryStateOnDisplay = YES;
}
- (void) initDetectedBeaconsList{
if (!_detectedBeacons) {
_detectedBeacons = [[NSArray alloc] init];
}
}
#pragma mark Beacons Ranging
- (void) startBeaconRanging{
if (!_locationManager || !_region) {
return;
}
if (_locationManager.rangedRegions.count > 0) {
NSLog(@"Didn't turn on ranging: Ranging already on.");
return;
}
[_locationManager startRangingBeaconsInRegion:_region];
}
//Location manager delegate method
- (void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region{
if (beacons.count == 0) {
NSLog(@"No beacons found nearby.");
beacon_current = nil;
} else {
_detectedBeacons = beacons;
NSInteger currentrssi = -9999;
for (CLBeacon *beacon in _detectedBeacons) {
if (beacon.rssi > currentrssi) {
currentrssi = beacon.rssi;
beacon_current = beacon;
}
}
}
}
- (void)checkLocationAccessForRanging {
if ([_locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
[_locationManager requestWhenInUseAuthorization];
}
}
@end