Skip to content

Commit

Permalink
chore(update): 1. 删除react-native-location库改为在ios端导出获取定位权限的方法供JS端使用; 2…
Browse files Browse the repository at this point in the history
…. android端新增获取设备状态方法与停止配网方法;
  • Loading branch information
zgatrying committed May 19, 2021
1 parent d7af983 commit 5231545
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,57 @@ public void decryptData(
private boolean isWifiConnected = false;
private boolean isPlatConnected = false;

@ReactMethod
public void stopConfigWifi() {
EZOpenSDK.getInstance().stopConfigWiFi();
}

@ReactMethod
public void probeDeviceInfo(
String deviceSerial,
String deviceType,
Promise promise
) {
final EZProbeDeviceInfoResult deviceInfoResult = EZOpenSDK.getInstance().probeDeviceInfo(deviceSerial, deviceType);
WritableNativeMap result = new WritableNativeMap();
if(deviceInfoResult.getBaseException() == null) {
result.putString("message", "设备已在线,可进行添加设备操作");
result.putBoolean("isAbleToAdd", true);
result.putBoolean("isNeedToConfigWifi", false);
} else {
switch (deviceInfoResult.getBaseException().getErrorCode()) {
case 120023:
case 120002:
case 120029:
result.putString("message", "设备不在线,需要配网");
result.putBoolean("isAbleToAdd", true);
result.putBoolean("isNeedToConfigWifi", true);
break;
case 120020:
result.putString("message", "设备在线,已经被自己添加");
result.putBoolean("isAbleToAdd", false);
result.putBoolean("isNeedToConfigWifi", false);
break;
case 120022:
result.putString("message", "设备在线,已经被别的用户添加");
result.putBoolean("isAbleToAdd", false);
result.putBoolean("isNeedToConfigWifi", false);
break;
case 120024:
result.putString("message", "设备不在线,已被别的用户添加");
result.putBoolean("isAbleToAdd", false);
result.putBoolean("isNeedToConfigWifi", false);
break;
default:
result.putString("message", "Request failed = " + deviceInfoResult.getBaseException().getErrorCode());
result.putBoolean("isAbleToAdd", false);
result.putBoolean("isNeedToConfigWifi", false);
break;
}
}
promise.resolve(result);
}

@ReactMethod
public void configWifi(
String deviceSerial,
Expand Down
13 changes: 2 additions & 11 deletions example/ios/EzvizviewExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@
13B07F8E1A680F5B00A75B9A /* Resources */,
00DD1BFF1BD5951E006B06BC /* Bundle React Native code and images */,
C1D60D28B925C94BD88E79D7 /* [CP] Copy Pods Resources */,
2A442285F60C5313A76B342D /* [CP] Embed Pods Frameworks */,
1C77A59DBCEE673D5609B7AE /* [CP] Embed Pods Frameworks */,
);
buildRules = (
);
Expand Down Expand Up @@ -425,12 +425,7 @@
shellPath = /bin/sh;
shellScript = "export NODE_BINARY=node\n../node_modules/react-native/scripts/react-native-xcode.sh";
};
<<<<<<< HEAD
<<<<<<< HEAD
=======
=======
>>>>>>> dev
2A442285F60C5313A76B342D /* [CP] Embed Pods Frameworks */ = {
1C77A59DBCEE673D5609B7AE /* [CP] Embed Pods Frameworks */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
Expand All @@ -448,10 +443,6 @@
shellScript = "\"${PODS_ROOT}/Target Support Files/Pods-EzvizviewExample/Pods-EzvizviewExample-frameworks.sh\"\n";
showEnvVarsInLog = 0;
};
<<<<<<< HEAD
>>>>>>> 547696b... chore(update): 1. 将Frameworks目录移到与ios目录同级; 2. 删除Libraries目录并调整react-native-ezvizview.podspec;
=======
>>>>>>> dev
2D02E4CB1E0B4B27006451C7 /* Bundle React Native Code And Images */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
Expand Down
10 changes: 0 additions & 10 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -433,19 +433,9 @@ SPEC CHECKSUMS:
React-jsi: 54245e1d5f4b690dec614a73a3795964eeef13a8
React-jsiexecutor: 8ca588cc921e70590820ce72b8789b02c67cce38
React-jsinspector: b14e62ebe7a66e9231e9581279909f2fc3db6606
<<<<<<< HEAD
<<<<<<< HEAD
react-native-ezvizview: 04ad9d01fcf435dcaac5556e447d662f5be14629
=======
react-native-camera: 9e55a09596b8c4e41eb7d4cce7c39826990c3cac
react-native-ezvizview: 935d48e900c1bfb8f82437912569da316c6da14a
react-native-location: 5a40ec1cc6abf2f6d94df979f98ec76c3a415681
>>>>>>> 547696b... chore(update): 1. 将Frameworks目录移到与ios目录同级; 2. 删除Libraries目录并调整react-native-ezvizview.podspec;
=======
react-native-camera: 9e55a09596b8c4e41eb7d4cce7c39826990c3cac
react-native-ezvizview: 935d48e900c1bfb8f82437912569da316c6da14a
react-native-location: 5a40ec1cc6abf2f6d94df979f98ec76c3a415681
>>>>>>> dev
react-native-safe-area-context: f0906bf8bc9835ac9a9d3f97e8bde2a997d8da79
React-RCTActionSheet: 910163b6b09685a35c4ebbc52b66d1bfbbe39fc5
React-RCTAnimation: 9a883bbe1e9d2e158d4fb53765ed64c8dc2200c6
Expand Down
1 change: 0 additions & 1 deletion example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
"react-native": "0.63.2",
"react-native-camera": "^3.43.6",
"react-native-gesture-handler": "^1.10.3",
"react-native-location": "^2.5.0",
"react-native-reanimated": "^2.0.1",
"react-native-safe-area-context": "^3.2.0",
"react-native-screens": "^2.18.1",
Expand Down
9 changes: 4 additions & 5 deletions example/src/page/AutoWifiConfig.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import { Alert, Button, Platform, Text, View } from 'react-native';
import {
configWifi,
probeDeviceInfo,
requestWhenInUseAuthorization,
stopConfigWifi,
} from 'react-native-ezvizview';
import { TextInput } from 'react-native-gesture-handler';
import { addDevice } from '../api';
import RNLocation from 'react-native-location';
import type { RootStackParamList } from '../App';
import useAsyncEffect from 'use-async-effect';

Expand All @@ -36,9 +36,7 @@ export default function AutoWifiConfigScreen({ route, navigation }: Props) {

useEffect(() => {
if (Platform.OS === 'ios') {
RNLocation.requestPermission({
ios: 'whenInUse',
}).then((granted) => {
requestWhenInUseAuthorization().then((granted) => {
if (granted) {
console.log('已获取定位权限');
} else {
Expand Down Expand Up @@ -109,7 +107,8 @@ export default function AutoWifiConfigScreen({ route, navigation }: Props) {
password
);
console.log('------->>正常接收到注册平台消息,添加设备');
handleAddDevice();
await handleAddDevice();
setIsOnConfig(false);
} catch (error) {
console.log('error', error.message);
}
Expand Down
5 changes: 0 additions & 5 deletions example/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3629,11 +3629,6 @@ react-native-iphone-x-helper@^1.3.0:
resolved "https://registry.npm.taobao.org/react-native-iphone-x-helper/download/react-native-iphone-x-helper-1.3.1.tgz#20c603e9a0e765fd6f97396638bdeb0e5a60b010"
integrity sha1-IMYD6aDnZf1vlzlmOL3rDlpgsBA=

react-native-location@^2.5.0:
version "2.5.0"
resolved "https://registry.yarnpkg.com/react-native-location/-/react-native-location-2.5.0.tgz#b6d2f7e9c3dbe13fbe87587f9533b8bf943b6a35"
integrity sha512-myT54tZuFmN6d5RruMVfoa8eY+voJWkSd73xUVhjzJDnQr5uu5MFoH7hMg3trHF1CAkaY/2gIneOspUdGxv59Q==

react-native-reanimated@^2.0.1:
version "2.1.0"
resolved "https://registry.npm.taobao.org/react-native-reanimated/download/react-native-reanimated-2.1.0.tgz#b9ad04aee490e1e030d0a6cdaa43a14895d9a54d"
Expand Down
60 changes: 58 additions & 2 deletions ios/EzvizModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,39 @@

#define EZOPENSDK [EZOpenSDK class]

@interface RNEzvizview : NSObject<RCTBridgeModule>
@interface RNEzvizview : NSObject<RCTBridgeModule, CLLocationManagerDelegate>

@property (nonatomic, strong) CLLocationManager *locationmanager;
@property (strong, nonatomic) CLLocationManager *locationManager;
@property (strong, nonatomic) RCTPromiseResolveBlock whenInUsePermissionResolver;

@end

@implementation RNEzvizview

RCT_EXPORT_MODULE();

#pragma mark - Initialization

+ (BOOL)requiresMainQueueSetup
{
return YES;
}

- (instancetype)init
{
if (self = [super init]) {
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
}

return self;
}

- (void)dealloc
{
self.locationManager = nil;
}

RCT_EXPORT_METHOD(setAccessToken: (NSString *) accessToken)
{
[EZOPENSDK setAccessToken:accessToken];
Expand Down Expand Up @@ -53,6 +76,38 @@ @implementation RNEzvizview
[EZOPENSDK stopConfigWifi];
}

RCT_REMAP_METHOD(requestWhenInUseAuthorization,
requestWhenInUseAuthorizationWithResolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)
{
// Get the current status
CLAuthorizationStatus status = [CLLocationManager authorizationStatus];

if (status == kCLAuthorizationStatusAuthorizedAlways || status == kCLAuthorizationStatusAuthorizedWhenInUse) {
// We already have the correct status so resolve with true
resolve(@(YES));
} else if (status == kCLAuthorizationStatusNotDetermined) {
// If we have not asked, or we have "when in use" permission, ask for always permission
[self.locationManager requestWhenInUseAuthorization];
// Save the resolver so we can return a result later on
self.whenInUsePermissionResolver = resolve;
} else {
// We are not in a state to ask for permission so resolve with false
resolve(@(NO));
}
}

#pragma mark - CLLocationManagerDelegate

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
// Handle the when in use permission resolver
if (self.whenInUsePermissionResolver != nil) {
self.whenInUsePermissionResolver(@(status == kCLAuthorizationStatusAuthorizedWhenInUse));
self.whenInUsePermissionResolver = nil;
}
}

RCT_EXPORT_METHOD(probeDeviceInfo: (NSString *) deviceSerial
deviceType: (NSString *) deviceType
resolver: (RCTPromiseResolveBlock) resolver
Expand Down Expand Up @@ -128,6 +183,7 @@ @implementation RNEzvizview
else if (status == DEVICE_PLATFORM_REGISTED)
{
resolver(@{@"message": @"设备注册平台成功, 可进行添加设备操作", @"isAbleToAdd": @YES});
[EZOPENSDK stopConfigWifi];
}
}];
}
Expand Down
11 changes: 10 additions & 1 deletion src/EzvizModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

import axios from 'axios';
import { NativeModules, NativeEventEmitter } from 'react-native';
import { NativeModules, NativeEventEmitter, Platform } from 'react-native';
import qs from 'qs';

export const RNEzvizview = NativeModules.RNEzvizview;
Expand Down Expand Up @@ -92,3 +92,12 @@ export async function probeDeviceInfo(
console.log('查询结果', res);
return res;
}

export async function requestWhenInUseAuthorization() {
if (Platform.OS === 'ios') {
let isAllowed: boolean = await RNEzvizview.requestWhenInUseAuthorization();
return isAllowed;
} else {
return false;
}
}

0 comments on commit 5231545

Please sign in to comment.