BlueToolManage.m 5.81 KB
//
//  BuleToolManage.m
//  metroApp
//
//  Created by 赵世强 on 2018/3/26.
//  Copyright © 2018年 Facebook. All rights reserved.
//

#import "BlueToolManage.h"
#import "AppDelegate.h"


@implementation BlueToolManage

{
  CLLocationManager *_locationManager;
  
  CLBeaconRegion *_region1;
  
  CLBeaconRegion *_region2;
  
  NSMutableArray *_detectedBeacons;
  
  CLBeacon *beacon_current;
  
  NSTimer *timer;
  
  //定义变量
  CBCentralManager *manage;
  
  BOOL blueOpen;
  
}

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;
}

- (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;
  });
}

RCT_EXPORT_METHOD(getPhoneNumber
                  :(RCTPromiseResolveBlock)resolve
                  rejecter:(RCTPromiseRejectBlock)reject)
{
  if ([AppDelegate sharedInstance].str_phone) {
    resolve([AppDelegate sharedInstance].str_phone?[AppDelegate sharedInstance].str_phone:@"");
  }
  
}

RCT_EXPORT_METHOD(getCurrentStatus
                  :(RCTPromiseResolveBlock)resolve
                  rejecter:(RCTPromiseRejectBlock)reject)
{
  resolve([NSString stringWithFormat:@"%hhd",[AppDelegate sharedInstance].isJump]);
}

- (void)postCurrentLocation{
  
  NSInteger currentrssi = -9999;
  
  if (_detectedBeacons.count>20) {
    [_detectedBeacons removeObjectsInRange:NSMakeRange(0, _detectedBeacons.count-20)];
  }
  
  for (CLBeacon *beacon in _detectedBeacons) {
    if (beacon.rssi <0) {
      if (beacon.rssi > currentrssi) {
        currentrssi = beacon.rssi;
        beacon_current = beacon;
      }
    }
  }
  
  if (!blueOpen) {
    beacon_current = nil;
  }
  
  NSLog(@"beacons found nearby.---%@ ----%@ -----%@",[beacon_current.proximityUUID UUIDString],beacon_current.major,beacon_current.minor);
  
  [self sendEventWithName:@"BlueToolData" body:@{@"deviceCode": [NSString stringWithFormat:@"%@|%@|%@",[beacon_current.proximityUUID UUIDString],beacon_current.major?beacon_current.major:@"",beacon_current.minor?beacon_current.minor:@""],@"rss":beacon_current.rssi? [NSString stringWithFormat:@"%ld",(long)beacon_current.rssi]:@""}];
  
  [_detectedBeacons removeAllObjects];
}

#pragma mark Init Beacons
- (void) initLocationManager{
  if (!_locationManager) {
    _locationManager = [[CLLocationManager alloc] init];
    _locationManager.delegate = self;
    [self checkLocationAccessForRanging];
  }
}

- (void) initBeaconRegion{
  if (_region1 && _region2)
    return;
  
  NSUUID *proximityUUID1 = [[NSUUID alloc] initWithUUIDString:@"E2C56DB5-DFFB-48D2-B060-D0F5A71096E0"];
  _region1 = [[CLBeaconRegion alloc] initWithProximityUUID:proximityUUID1 identifier:@"cell1"];
  _region1.notifyEntryStateOnDisplay = YES;
  
  NSUUID *proximityUUID2 = [[NSUUID alloc] initWithUUIDString:@"FDA50693-A4E2-4FB1-AFCF-C6EB07647825"];
  _region2 = [[CLBeaconRegion alloc] initWithProximityUUID:proximityUUID2 identifier:@"cell2"];
  _region2.notifyEntryStateOnDisplay = YES;
  
  
}

- (void) initDetectedBeaconsList{
  if (!_detectedBeacons) {
    _detectedBeacons = [[NSMutableArray alloc] init];
  }
}

#pragma mark Beacons Ranging
- (void) startBeaconRanging{
  if (!_locationManager || !_region1 || !_region2) {
    return;
  }
  if (_locationManager.rangedRegions.count > 0) {
    NSLog(@"Didn't turn on ranging: Ranging already on.");
    return;
  }
  
  [_locationManager startRangingBeaconsInRegion:_region1];
  [_locationManager startRangingBeaconsInRegion:_region2];
  
}


//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 {

    NSInteger currentrssi = -9999;
    for (CLBeacon *beacon in beacons) {
      [_detectedBeacons addObject:beacon];
//      if (beacon.rssi > currentrssi) {
//        currentrssi = beacon.rssi;
//        beacon_current = beacon;
//      }
    }
    
  }
}

- (void)checkLocationAccessForRanging {
  if ([_locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
    [_locationManager requestWhenInUseAuthorization];
  }
}

- (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];
      
      blueOpen = NO;
    }
      break;
    case CBManagerStatePoweredOn:
    {
      blueOpen = YES;
      NSLog(@"CBCentralManagerStatePoweredOn");//蓝牙已开启
      
    }
      break;
    default:
      break;
  }
}

@end