BlueToolManage.m
4.2 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
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
//
// 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;
//定义变量
CBCentralManager *manage;
}
RCT_EXPORT_MODULE(BlueToolManage);
- (instancetype)init
{
if(self = [super init]){
manage =[[CBCentralManager alloc]initWithDelegate:self queue:nil];
[self initLocationManager];
[self initBeaconRegion];
[self initDetectedBeaconsList];
[self startBeaconRanging];
}
return self;
}
- (void)centralManagerDidUpdateState:(CBCentralManager *)central {
switch (central.state) {
case CBManagerStateUnknown:
NSLog(@"CBCentralManagerStateUnknown");
break;
case CBManagerStateResetting:
NSLog(@"CBCentralManagerStateResetting");
break;
case CBManagerStateUnsupported:
NSLog(@"CBCentralManagerStateUnsupported");//不支持蓝牙
break;
case CBManagerStateUnauthorized:
NSLog(@"CBCentralManagerStateUnauthorized");
break;
case CBManagerStatePoweredOff:
{
NSLog(@"CBCentralManagerStatePoweredOff");//蓝牙未开启
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"未开启蓝牙" message:@"请关闭app,并在设置->打开蓝牙服务" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
[alert show];
}
break;
case CBManagerStatePoweredOn:
{
NSLog(@"CBCentralManagerStatePoweredOn");//蓝牙已开启
}
break;
default:
break;
}
}
- (NSArray<NSString *> *)supportedEvents {
return @[@"BlueToolData"]; //这里返回的将是你要发送的消息名的数组。
}
RCT_EXPORT_METHOD(startTimer:(NSString *)timeInterval)
{
dispatch_async(dispatch_get_main_queue(), ^{
timer = [NSTimer scheduledTimerWithTimeInterval:[timeInterval intValue] target:self selector:@selector(postCurrentLocation) userInfo:nil repeats:YES];
[timer fire];
});
}
RCT_EXPORT_METHOD(stopTimer)
{
dispatch_async(dispatch_get_main_queue(), ^{
[timer invalidate];
timer = nil;
});
}
- (void)postCurrentLocation{
[self sendEventWithName:@"BlueToolData" body:@{@"deviceCode": [NSString stringWithFormat:@"E2C56DB5-DFFB-48D2-B060-D0F5A71096E0|%@|%@",beacon_current.major?beacon_current.major:@"",beacon_current.minor?beacon_current.minor:@""],@"rss":beacon_current.rssi? [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