From 5231545202e1a1bb4c41c7dcf8fb648002746af6 Mon Sep 17 00:00:00 2001 From: zgatrying <279501822@qq.com> Date: Wed, 19 May 2021 15:00:58 +0800 Subject: [PATCH] =?UTF-8?q?chore(update):=201.=20=E5=88=A0=E9=99=A4react-n?= =?UTF-8?q?ative-location=E5=BA=93=E6=94=B9=E4=B8=BA=E5=9C=A8ios=E7=AB=AF?= =?UTF-8?q?=E5=AF=BC=E5=87=BA=E8=8E=B7=E5=8F=96=E5=AE=9A=E4=BD=8D=E6=9D=83?= =?UTF-8?q?=E9=99=90=E7=9A=84=E6=96=B9=E6=B3=95=E4=BE=9BJS=E7=AB=AF?= =?UTF-8?q?=E4=BD=BF=E7=94=A8;=202.=20android=E7=AB=AF=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E8=AE=BE=E5=A4=87=E7=8A=B6=E6=80=81=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E4=B8=8E=E5=81=9C=E6=AD=A2=E9=85=8D=E7=BD=91=E6=96=B9?= =?UTF-8?q?=E6=B3=95;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../reactnativeezvizview/EzvizviewModule.java | 51 ++++++++++++++++ .../project.pbxproj | 13 +--- example/ios/Podfile.lock | 10 ---- example/package.json | 1 - example/src/page/AutoWifiConfig.tsx | 9 ++- example/yarn.lock | 5 -- ios/EzvizModule.m | 60 ++++++++++++++++++- src/EzvizModule.ts | 11 +++- 8 files changed, 125 insertions(+), 35 deletions(-) diff --git a/android/src/main/java/com/reactnativeezvizview/EzvizviewModule.java b/android/src/main/java/com/reactnativeezvizview/EzvizviewModule.java index db2f74c..7bc031f 100644 --- a/android/src/main/java/com/reactnativeezvizview/EzvizviewModule.java +++ b/android/src/main/java/com/reactnativeezvizview/EzvizviewModule.java @@ -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, diff --git a/example/ios/EzvizviewExample.xcodeproj/project.pbxproj b/example/ios/EzvizviewExample.xcodeproj/project.pbxproj index 7819d31..606331d 100644 --- a/example/ios/EzvizviewExample.xcodeproj/project.pbxproj +++ b/example/ios/EzvizviewExample.xcodeproj/project.pbxproj @@ -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 = ( ); @@ -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 = ( @@ -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; diff --git a/example/ios/Podfile.lock b/example/ios/Podfile.lock index 890a04f..67caa81 100644 --- a/example/ios/Podfile.lock +++ b/example/ios/Podfile.lock @@ -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 diff --git a/example/package.json b/example/package.json index 6d9cca9..6abc2d6 100644 --- a/example/package.json +++ b/example/package.json @@ -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", diff --git a/example/src/page/AutoWifiConfig.tsx b/example/src/page/AutoWifiConfig.tsx index a6e2e1f..4b0946d 100644 --- a/example/src/page/AutoWifiConfig.tsx +++ b/example/src/page/AutoWifiConfig.tsx @@ -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'; @@ -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 { @@ -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); } diff --git a/example/yarn.lock b/example/yarn.lock index 81984ee..7699427 100644 --- a/example/yarn.lock +++ b/example/yarn.lock @@ -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" diff --git a/ios/EzvizModule.m b/ios/EzvizModule.m index a0eef39..6b61f25 100644 --- a/ios/EzvizModule.m +++ b/ios/EzvizModule.m @@ -14,9 +14,10 @@ #define EZOPENSDK [EZOpenSDK class] -@interface RNEzvizview : NSObject +@interface RNEzvizview : NSObject -@property (nonatomic, strong) CLLocationManager *locationmanager; +@property (strong, nonatomic) CLLocationManager *locationManager; +@property (strong, nonatomic) RCTPromiseResolveBlock whenInUsePermissionResolver; @end @@ -24,6 +25,28 @@ @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]; @@ -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 @@ -128,6 +183,7 @@ @implementation RNEzvizview else if (status == DEVICE_PLATFORM_REGISTED) { resolver(@{@"message": @"设备注册平台成功, 可进行添加设备操作", @"isAbleToAdd": @YES}); + [EZOPENSDK stopConfigWifi]; } }]; } diff --git a/src/EzvizModule.ts b/src/EzvizModule.ts index a3fb35d..e38bca3 100644 --- a/src/EzvizModule.ts +++ b/src/EzvizModule.ts @@ -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; @@ -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; + } +}