提交 c4d182b7 编写于 作者: 杜庆泉's avatar 杜庆泉

手动合并dev 到 alpha

......@@ -2,7 +2,10 @@
/.hbuilderx
/.vite
.DS_Store
<<<<<<< HEAD
=======
>>>>>>> dev
env.js
jest.config.js
autotest.reporter.js
......
......@@ -19,7 +19,7 @@
<button @tap="testGetBatteryInfoSync">同步获取电池电量</button>
<button @tap="testonMemoryWarning">开启内存不足告警监听</button>
<button @tap="testoffMemoryWarning">关闭内存不足告警监听</button>
<button @tap="getLocationTest" style="width: 100%;">获取定位</button>
</view>
</template>
......@@ -32,7 +32,8 @@
memListener:null,
setUserCaptureScreenFlag: false,
setUserCaptureScreenText: '禁止截屏',
permissionGranted: false
permissionGranted: false,
id:0
}
},
onLoad() {
......@@ -45,7 +46,19 @@
fn:function(res){
console.log(res)
},
getLocationTest() {
console.log(" ------- getLocationTest: ");
uni.getLocation({
type: 'gcj02 ',
success (res) {
console.log(" success ",res);
},fail (res) {
console.log(" fail ",res);
}
})
},
onGetWifiList2_assert0() {
console.log(" ------- onGetWifiList2_assert0: ",this.id);
const fn = res => console.log('onGetWifiList res', res)
uni.startWifi({success(){
uni.onGetWifiList(fn)
......@@ -62,6 +75,7 @@
}
})
}})
this.id++
},
testConnnectWifi(){
......
......@@ -4,7 +4,7 @@ export interface Uni {
*
* @tutorial http://uniapp.dcloud.io/api/location/location?id=getlocation
*/
getLocation: GetLocation;
getLocation(options: GetLocationOptions) : void;
}
export type GetLocation = (options: GetLocationOptions) => void;
......
## 1.3.1(2023-09-15)
app端适配使用UniError
## 1.3.0(2023-05-30)
新增 同步获取电量api
## 1.2.0(2022-10-17)
实现百度小程序/支付宝小程序/QQ小程序获取电量
## 1.1.0(2022-10-17)
实现ios平台获取电量
......
{
"id": "uni-getbatteryinfo",
"displayName": "uni-getbatteryinfo",
"version": "1.2.0",
"version": "1.3.1",
"description": "使用uts开发,实现在多个平台获取电池电量功能",
"keywords": [
"battery"
],
"repository": "",
"engines": {
"HBuilderX": "^3.6.0"
"HBuilderX": "^3.9.0"
},
"dcloudext": {
"type": "uts",
......@@ -32,7 +32,12 @@
},
"uni_modules": {
"uni-ext-api": {
"uni": ["getBatteryInfo", "getBatteryInfoSync"]
"uni": {
"getBatteryInfo": "getBatteryInfo",
"getBatteryInfoSync": {
"web": false
}
}
},
"dependencies": [],
"encrypt": [],
......
import Context from "android.content.Context";
import BatteryManager from "android.os.BatteryManager";
import { UTSAndroid } from "io.dcloud.uts";
import { GetBatteryInfo, GetBatteryInfoSuccess, GetBatteryInfoFail, GetBatteryInfoResult,GetBatteryInfoSync } from '../interface.uts'
import { GetBatteryInfo, GetBatteryInfoOptions, GetBatteryInfoSuccess, GetBatteryInfoResult,GetBatteryInfoSync } from '../interface.uts'
import IntentFilter from 'android.content.IntentFilter';
import Intent from 'android.content.Intent';
/**
* 异步获取电量
* @param {Object} options
*/
export const getBatteryInfo : GetBatteryInfo = function (options) {
export const getBatteryInfo : GetBatteryInfo = function (options : GetBatteryInfoOptions) {
const context = UTSAndroid.getAppContext();
if (context != null) {
const manager = context.getSystemService(
......@@ -16,20 +20,21 @@ export const getBatteryInfo : GetBatteryInfo = function (options) {
const level = manager.getIntProperty(
BatteryManager.BATTERY_PROPERTY_CAPACITY
);
let ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
let batteryStatus = context.registerReceiver(null, ifilter);
let status = batteryStatus?.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
let isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL;
const res : GetBatteryInfoSuccess = {
errMsg: 'getBatteryInfo:ok',
level,
isCharging: manager.isCharging()
isCharging: isCharging
}
options.success?.(res)
options.complete?.(res)
} else {
const res : GetBatteryInfoFail = {
errSubject: "uni-getBatteryInfo",
errCode: 1001,
errMsg: 'getBatteryInfo:fail getAppContext is null',
cause: null
}
const res = new UniError("uni-getBatteryInfo", 1001, "getBatteryInfo:fail getAppContext is null")
options.fail?.(res)
options.complete?.(res)
}
......@@ -42,6 +47,8 @@ export const getBatteryInfoSync : GetBatteryInfoSync = function (): GetBatteryIn
const context = UTSAndroid.getAppContext();
if (context != null) {
const manager = context.getSystemService(
Context.BATTERY_SERVICE
) as BatteryManager;
......@@ -49,12 +56,19 @@ export const getBatteryInfoSync : GetBatteryInfoSync = function (): GetBatteryIn
BatteryManager.BATTERY_PROPERTY_CAPACITY
);
let ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
let batteryStatus = context.registerReceiver(null, ifilter);
let status = batteryStatus?.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
let isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL;
const res : GetBatteryInfoResult = {
level: level,
isCharging: manager.isCharging()
isCharging: isCharging
};
return res;
} else {
}
else {
/**
* 无有效上下文
*/
......
{
"deploymentTarget": "9"
}
\ No newline at end of file
......@@ -2,7 +2,7 @@
import { UIDevice } from "UIKit";
import { Int } from 'Swift';
import { GetBatteryInfo, GetBatteryInfoSuccess } from '../interface.uts';
import { GetBatteryInfo, GetBatteryInfoSuccess, GetBatteryInfoResult, GetBatteryInfoSync } from '../interface.uts';
/**
* 导出 获取电量方法
......
......@@ -34,4 +34,9 @@ declare interface Uni {
* 文档: [https://uniapp.dcloud.net.cn/api/system/batteryInfo.html](https://uniapp.dcloud.net.cn/api/system/batteryInfo.html)
*/
getBatteryInfo(option?: UniNamespace.GetBatteryInfoOption): void;
/**
* 同步获取电池电量信息
*/
getBatteryInfoSync(): UniNamespace.GetBatteryInfoSuccessCallbackResult;
}
......@@ -10,24 +10,24 @@ export type GetBatteryInfoSuccess = {
isCharging : boolean
}
export type GetBatteryInfoFail = {
/**
* 错误码
*/
errCode : number,
/**
* 调用API的名称
*/
errSubject : string,
/**
* 错误的详细信息
*/
errMsg : string,
/**
* 错误来源
*/
cause : any | null
}
// export type GetBatteryInfoFail = {
// /**
// * 错误码
// */
// errCode : number,
// /**
// * 调用API的名称
// */
// errSubject : string,
// /**
// * 错误的详细信息
// */
// errMsg : string,
// /**
// * 错误来源
// */
// cause : any | null
// }
......@@ -39,7 +39,7 @@ export type GetBatteryInfoOptions = {
/**
* 接口调用失败的回调函数
*/
fail ?: (res : GetBatteryInfoFail) => void
fail ?: (res : UniError) => void
/**
* 接口调用成功的回调
*/
......@@ -107,7 +107,7 @@ interface Uni {
* @uniVueVersion 2,3 //支持的vue版本
*
*/
getBatteryInfo : GetBatteryInfo,
getBatteryInfo (options : GetBatteryInfoOptions) : void,
/**
* 同步获取电池电量信息
* @description 获取电池电量信息
......@@ -134,6 +134,6 @@ interface Uni {
* @uniVueVersion 2,3 //支持的vue版本
*
*/
getBatteryInfoSync: GetBatteryInfoSync
getBatteryInfoSync():GetBatteryInfoResult
}
export function getBatteryInfo(options) {
return my.getBatteryInfo(options)
}
export function getBatteryInfoSync(options) {
return my.getBatteryInfoSync(options)
}
export function getBatteryInfo(options) {
return swan.getBatteryInfo(options)
}
export function getBatteryInfoSync(options) {
return swan.getBatteryInfoSync(options)
}
export function getBatteryInfo(options) {
return qq.getBatteryInfo(options)
}
export function getBatteryInfoSync(options) {
return qq.getBatteryInfoSync(options)
}
export function getBatteryInfo(options) {
return wx.getBatteryInfo(options)
}
export function getBatteryInfoSync(options) {
return wx.getBatteryInfoSync(options)
}
......@@ -5,7 +5,7 @@
"description": "UTS 实现内存警告监听",
"keywords": [
"onMemoryWarning"
],
],
"repository": "",
"engines": {
"HBuilderX": "^3.6.11"
......@@ -31,17 +31,15 @@
"npmurl": ""
},
"uni_modules": {
"uni-ext-api":{
"uni-ext-api": {
"uni": {
"onMemoryWarning": "onMemoryWarning",
"offMemoryWarning":"offMemoryWarning"
"onMemoryWarning": {
"web": false
},
"mp-weixin":false,
"mp-alipay":false,
"mp-baidu":false,
"mp-qq":false,
"mp-kuaishou":false,
"mp-jd":false
"offMemoryWarning": {
"web": false
}
}
},
"dependencies": [],
"encrypt": [],
......
......@@ -46,6 +46,7 @@ class MemoryWarningTool {
}
}
// 开启监听内存警告
export const onMemoryWarning : OnMemoryWarning = function (callback: UTSCallback) {
MemoryWarningTool.listenMemoryWarning(callback)
......
......@@ -25,8 +25,9 @@ export interface Uni {
* }
* @uniVersion 3.7.7
* @uniVueVersion 2,3 //支持的vue版本
* @autotest { expectCallback: true }
*/
onMemoryWarning : OnMemoryWarning,
onMemoryWarning(callback: UTSCallback) : void,
/**
* 取消监听内存不足告警事件
*
......@@ -48,6 +49,7 @@ export interface Uni {
* }
* @uniVersion 3.7.7
* @uniVueVersion 2,3 //支持的vue版本
* @autotest { expectCallback: true }
*/
offMemoryWarning : OffMemoryWarning
offMemoryWarning(callback : UTSCallback | null) : void
}
\ No newline at end of file
......@@ -33,9 +33,16 @@
"uni_modules": {
"uni-ext-api":{
"uni": {
"onUserCaptureScreen": "onUserCaptureScreen",
"offUserCaptureScreen": "offUserCaptureScreen",
"setUserCaptureScreen": "setUserCaptureScreen"
"onUserCaptureScreen": {
"web": false
},
"offUserCaptureScreen": {
"web": false
},
"setUserCaptureScreen": {
"web": false,
"mp-weixin": false
}
}
},
"dependencies": [],
......
......@@ -9,6 +9,7 @@ import Environment from "android.os.Environment";
import System from 'java.lang.System';
import WindowManager from 'android.view.WindowManager';
import { OnUserCaptureScreenCallbackResult, UserCaptureScreenCallback, OnUserCaptureScreen, OffUserCaptureScreen, SetUserCaptureScreenSuccess, SetUserCaptureScreenOptions, SetUserCaptureScreen } from "../interface.uts";
import string from 'android.R.string';
/**
......@@ -34,9 +35,9 @@ class ScreenFileObserver extends FileObserver {
*/
private screenFile : File;
constructor(screenFile : File) {
super(screenFile);
this.screenFile = screenFile;
constructor(screenFileStr : string) {
super(screenFileStr);
this.screenFile = new File(screenFileStr);
}
override onEvent(event : Int, path : string | null) : void {
......@@ -83,7 +84,7 @@ export const onUserCaptureScreen : OnUserCaptureScreen = function (callback : Us
}
// 先结束监听 再开启监听
observer?.stopWatching();
observer = new ScreenFileObserver(directory_screenshot);
observer = new ScreenFileObserver(directory_screenshot.getPath());
observer?.startWatching();
......@@ -92,7 +93,6 @@ export const onUserCaptureScreen : OnUserCaptureScreen = function (callback : Us
observer = null
})
}
/**
......
......@@ -3,7 +3,7 @@ import { CGRect } from "CoreFoundation";
import { UIApplication, UIView, UITextField, UIScreen, UIDevice } from "UIKit"
import { UTSiOS } from "DCloudUTSFoundation"
import { DispatchQueue } from 'Dispatch';
import { SetUserCaptureScreenOptions, OnUserCaptureScreenCallbackResult, OnUserCaptureScreen, OffUserCaptureScreen, SetUserCaptureScreen, UserCaptureScreenCallback, SetUserCaptureScreenSuccess, SetUserCaptureScreenFail } from "../interface.uts"
import { SetUserCaptureScreenOptions, OnUserCaptureScreenCallbackResult, OnUserCaptureScreen, OffUserCaptureScreen, SetUserCaptureScreen, UserCaptureScreenCallback, SetUserCaptureScreenSuccess } from "../interface.uts"
/**
* 定义监听截屏事件工具类
......@@ -119,20 +119,12 @@ export const offUserCaptureScreen : OffUserCaptureScreen = function (callback :
*/
export const setUserCaptureScreen : SetUserCaptureScreen = function (options : SetUserCaptureScreenOptions) {
if (UIDevice.current.systemVersion < "13.0") {
let res: SetUserCaptureScreenFail = {
errCode: 12001,
errSubject: "uni-usercapturescreen",
errMsg: "setUserCaptureScreen:system not support"
}
let res = new UniError("uni-usercapturescreen", 12001, "setUserCaptureScreen:system not support")
options.fail?.(res);
options.complete?.(res);
} else if (UIDevice.current.systemVersion == "15.1") {
let res: SetUserCaptureScreenFail = {
errCode: 12010,
errSubject: "uni-usercapturescreen",
errMsg: "setUserCaptureScreen:system internal error"
}
let res = new UniError("uni-usercapturescreen", 12010, "setUserCaptureScreen:system internal error")
options.fail?.(res);
options.complete?.(res);
} else {
......
......@@ -23,25 +23,6 @@ export type OffUserCaptureScreen = (callback : UserCaptureScreenCallback | null)
export type SetUserCaptureScreenSuccess = {
}
/**
* uni.setUserCaptureScreen失败回调参数
*/
export type SetUserCaptureScreenFail = {
/**
* 错误码
* 12001:system not support
* 12010:system internal error
*/
errCode : number,
/**
* 调用API的名称
*/
errSubject : string,
/**
* 错误的详细信息
*/
errMsg : string,
}
/**
* uni.setUserCaptureScreen成功回调函数定义
......@@ -51,7 +32,7 @@ export type SetUserCaptureScreenSuccessCallback = (res : SetUserCaptureScreenSuc
/**
* uni.setUserCaptureScreen失败回调函数定义
*/
export type SetUserCaptureScreenFailCallback = (res : SetUserCaptureScreenFail) => void
export type SetUserCaptureScreenFailCallback = (res : UniError) => void
/**
* uni.setUserCaptureScreen完成回调函数定义
......@@ -108,8 +89,9 @@ export interface Uni {
* }
* @uniVersion 3.7.7
* @uniVueVersion 2,3 //支持的vue版本
* @autotest { expectCallback: true }
*/
onUserCaptureScreen : OnUserCaptureScreen,
onUserCaptureScreen(callback : UserCaptureScreenCallback | null) : void,
/**
* 关闭截屏监听
*
......@@ -131,8 +113,9 @@ export interface Uni {
* }
* @uniVersion 3.7.7
* @uniVueVersion 2,3 //支持的vue版本
* @autotest { expectCallback: true }
*/
offUserCaptureScreen : OffUserCaptureScreen,
offUserCaptureScreen(callback : UserCaptureScreenCallback | null) : void,
/**
* 设置防截屏
*
......@@ -155,5 +138,5 @@ export interface Uni {
* @uniVersion 3.7.7
* @uniVueVersion 2,3 //支持的vue版本
*/
setUserCaptureScreen : SetUserCaptureScreen
setUserCaptureScreen(options : SetUserCaptureScreenOptions) : void
}
\ No newline at end of file
......@@ -5,7 +5,7 @@
"description": "wifi管理",
"keywords": [
"wifi"
],
],
"repository": "",
"engines": {
"HBuilderX": "^3.6.11"
......@@ -31,19 +31,41 @@
"npmurl": ""
},
"uni_modules": {
"uni-ext-api":{
"uni-ext-api": {
"uni": {
"startWifi": "startWifi",
"stopWifi": "stopWifi",
"connectWifi":"connectWifi",
"getConnectedWifi": "getConnectedWifi",
"getWifiList":"getWifiList",
"onGetWifiList":"onGetWifiList",
"offGetWifiList":"offGetWifiList",
"onWifiConnected":"onWifiConnected",
"offWifiConnected":"offWifiConnected",
"onWifiConnectedWithPartialInfo":"onWifiConnectedWithPartialInfo",
"offWifiConnectedWithPartialInfo":"offWifiConnectedWithPartialInfo"
"startWifi": {
"web": false
},
"stopWifi": {
"web": false
},
"connectWifi": {
"web": false
},
"getConnectedWifi": {
"web": false
},
"getWifiList": {
"web": false
},
"onGetWifiList": {
"web": false
},
"offGetWifiList": {
"web": false
},
"onWifiConnected": {
"web": false
},
"offWifiConnected": {
"web": false
},
"onWifiConnectedWithPartialInfo": {
"web": false
},
"offWifiConnectedWithPartialInfo": {
"web": false
}
}
},
"dependencies": [],
......
{
"minSdkVersion": "19"
}
\ No newline at end of file
......@@ -306,17 +306,8 @@ class CustomBroadcastReceiver extends BroadcastReceiver {
if (intent.action == WifiManager.SCAN_RESULTS_AVAILABLE_ACTION) {
startWifiScaning = false;
let ret : UniWifiResult = {
errCode: 0,
errSubject: "uni-getWifiList",
errMsg: "getWifiList:ok"
}
if(Global.supendGetWifiSuccess != null){
Global.supendGetWifiSuccess?.(ret)
}
if(Global.supendGetWifiComplete != null){
Global.supendGetWifiComplete?.(ret)
}
// wifi 扫描结果回调
let results = this.mWifiManager!.scanResults;
......@@ -336,15 +327,27 @@ class CustomBroadcastReceiver extends BroadcastReceiver {
const data = new JSONObject();
data["wifiList"] = Global.scanList
Global.onGetWifiListCallback?.(data);
/**
* 确保onGetWifiList 只会被执行一次
*/
Global.onGetWifiListCallback = null
}
// for (let perCallback in Global.onGetWifiListCallback) {
// const data = new JSONObject();
// data["wifiList"] = Global.scanList
// perCallback(data);
// }
}
let ret : UniWifiResult = {
errCode: 0,
errSubject: "uni-getWifiList",
errMsg: "getWifiList:ok"
}
if(Global.supendGetWifiSuccess != null){
Global.supendGetWifiSuccess?.(ret)
Global.supendGetWifiSuccess = null
}
if(Global.supendGetWifiComplete != null){
Global.supendGetWifiComplete?.(ret)
Global.supendGetWifiComplete = null
}
}
}
......@@ -367,18 +370,21 @@ export function startWifi(option : WifiOption) {
// 需要先开启wifi,才能使用后续的功能
let requestCode = 1001;
let permissionWifi = arrayOf("android.permission.ACCESS_FINE_LOCATION");
var result : UniWifiResult = {
let result : UniWifiResult = {
errCode: 12001,
errMsg: "startWifi:premission loss",
errSubject: "uni-startWifi"
}
// 检查权限
if (ActivityCompat.checkSelfPermission(UTSAndroid.getUniActivity()!, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(UTSAndroid.getUniActivity()!, permissionWifi, requestCode.toInt())
// 尚不具备权限,返回错误
option.fail?.(result)
option.complete?.(result)
let err = new UniError("uni-startWifi",12001,"startWifi:premission loss");
option.fail?.(err)
option.complete?.(err)
return;
}
......@@ -389,11 +395,9 @@ export function startWifi(option : WifiOption) {
// 用户没有开启wifi 总开关
if (!wifiManager.isWifiEnabled()) {
// wifi 没开启
result.errCode = 12005;
result.errMsg = "wifi not turned on";
option.fail?.(result);
option.complete?.(result);
let err = new UniError("uni-startWifi",12005,"wifi not turned on");
option.fail?.(err);
option.complete?.(err);
return;
}
// 初始化wifi 状态广播监听,后续所有的api,均基于此
......@@ -453,16 +457,13 @@ export function startWifi(option : WifiOption) {
@Suppress("DEPRECATION")
export function getWifiList(option : WifiOption) {
var result : UniWifiResult = {
errCode: 12000,
errMsg: "getWifiList:fail:not invoke startWifi",
errSubject: "uni-getWifiList"
}
if (Global.mReceiver == null) {
// 还没调用startWifi 提示报错
option.fail?.(result)
option.complete?.(result)
let err = new UniError("uni-getWifiList",12000,"getWifiList:fail:not invoke startWifi");
option.fail?.(err)
option.complete?.(err)
return
}
......@@ -547,11 +548,13 @@ export function offGetWifiList(callback? : UTSCallback) {
/**
* 真正执行wifi链接逻辑
*/
function realWifiConnect(option : WifiConnectOption,result : UniWifiResult){
function realWifiConnect(option : WifiConnectOption){
if (Global.mReceiver == null || Global.scanList.length < 1) {
option.fail?.(result)
option.complete?.(result)
let err = new UniError("uni-connectWifi",12000,"connectWifi:fail:not invoke startWifi");
option.fail?.(err)
option.complete?.(err)
return
}
......@@ -566,8 +569,9 @@ function realWifiConnect(option : WifiConnectOption,result : UniWifiResult){
if (scanWifiInfo == null) {
// 不在扫描列表中返回错误
option.fail?.(result)
option.complete?.(result)
let err = new UniError("uni-connectWifi",12000,"connectWifi:fail:not invoke startWifi");
option.fail?.(err)
option.complete?.(err)
return
}
......@@ -593,16 +597,16 @@ function realWifiConnect(option : WifiConnectOption,result : UniWifiResult){
// add since 2023-03-28,如果当前系统大于等于android10, 则明确当前系统不支持
if(Build.VERSION.SDK_INT > 28){
// 系统大于android 9
result.errCode = 12001
result.errMsg = "connectWifi:system not support"
let err = new UniError("uni-connectWifi",12001,"connectWifi:system not support");
option.fail?.(err)
option.complete?.(err)
}else{
// 移除之前的配置失败了,返回错误,需要用户手动取消保存一下
result.errCode = 12013
result.errMsg = "connectWifi:wifi config may be expired"
let err = new UniError("uni-connectWifi",12013,"connectWifi:wifi config may be expired");
option.fail?.(err)
option.complete?.(err)
}
option.fail?.(result)
option.complete?.(result)
return
}
......@@ -623,11 +627,11 @@ function realWifiConnect(option : WifiConnectOption,result : UniWifiResult){
let netID = wifiManager.addNetwork(wifiConfigration);
// 如果-1 说明没添加上,报错即可
if (netID < 0) {
result.errCode = 12002
result.errMsg = "connectWifi:password error Wi-Fi"
option.fail?.(result)
option.complete?.(result)
let err = new UniError("uni-connectWifi",12002,"connectWifi:password error Wi-Fi");
option.fail?.(err)
option.complete?.(err)
return
}
......@@ -636,11 +640,10 @@ function realWifiConnect(option : WifiConnectOption,result : UniWifiResult){
let enabled = wifiManager.enableNetwork(netID, true);
if (!enabled) {
result.errCode = 12007
result.errMsg = "connectWifi:user denied"
let err = new UniError("uni-connectWifi",12007,"connectWifi:user denied");
option.fail?.(result)
option.complete?.(result)
option.fail?.(err)
option.complete?.(err)
return
}
......@@ -654,14 +657,18 @@ function realWifiConnect(option : WifiConnectOption,result : UniWifiResult){
if (!connected) {
// 出错了,返回错误
// 兜底的报错
result.errCode = 12010
result.errMsg = "connectWifi:fail:unknown error"
option.fail?.(result)
option.complete?.(result)
let err = new UniError("uni-connectWifi",12010,"connectWifi:fail:unknown error");
option.fail?.(err)
option.complete?.(err)
return
}
let result : UniWifiResult = {
errCode: 0,
errMsg: "connectWifi:ok",
errSubject: "uni-connectWifi",
}
wifiManager.saveConfiguration()
//scanWifiInfo 根据 partialInfo 填充给返回字段
if (option.partialInfo != null && option.partialInfo == true) {
......@@ -673,9 +680,6 @@ function realWifiConnect(option : WifiConnectOption,result : UniWifiResult){
result.wifi = wrapUniWifiInfoFromAndroid(scanWifiInfo)
}
// result.wifi = scanWifiInfo.toUTSJSON(option.partialInfo)
result.errCode = 0
result.errMsg = "connectWifi:ok"
option.success?.(result)
option.complete?.(result)
......@@ -687,20 +691,16 @@ function realWifiConnect(option : WifiConnectOption,result : UniWifiResult){
@Suppress("UNUSED_PARAMETER", "DEPRECATION")
export function connectWifi(option : WifiConnectOption) {
var result : UniWifiResult = {
errCode: 12000,
errMsg: "connectWifi:fail:not invoke startWifi",
errSubject: "uni-connectWifi",
}
if (option.maunal == true) {
// 指定了手动模式
let manunalIntent = new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS);
UTSAndroid.getUniActivity()!.startActivity(manunalIntent);
result.errCode = 0
result.errMsg = "connectWifi:ok"
let result : UniWifiResult = {
errCode: 0,
errMsg: "connectWifi:ok",
errSubject: "uni-connectWifi",
}
option.success?.(result)
option.complete?.(result)
......@@ -719,7 +719,7 @@ export function connectWifi(option : WifiConnectOption) {
if(taskCount >= 5 || startWifiScaning == false){
// 超过10s了。或者扫描过程结束了
clearInterval(taskId)
realWifiConnect(option,result)
realWifiConnect(option)
}
},2000)
......@@ -727,7 +727,7 @@ export function connectWifi(option : WifiConnectOption) {
clearInterval(taskId)
});
}else{
realWifiConnect(option,result)
realWifiConnect(option)
}
......@@ -741,13 +741,9 @@ export function connectWifi(option : WifiConnectOption) {
export function stopWifi(option : WifiOption) {
// 需要先开启wifi,才能使用后续的功能
if (Global.mReceiver == null) {
var result : UniWifiResult = {
errCode: 12000,
errSubject: "uni-stopWifi",
errMsg: "stopWifi:not init"
}
option.fail?.(result)
option.complete?.(result)
let err = new UniError("uni-stopWifi",12000,"stopWifi:not init");
option.fail?.(err)
option.complete?.(err)
return
}
......@@ -768,6 +764,7 @@ export function stopWifi(option : WifiOption) {
errSubject: "uni-stopWifi",
errMsg: "stopWifi:ok"
}
option.success?.(result)
option.complete?.(result)
......@@ -783,16 +780,12 @@ export function getConnectedWifi(option : GetConnectedWifiOptions) {
SSID: ""
}
var res : UniWifiResult = {
errCode: 12000,
errMsg: "getConnectedWifi:fail:not invoke startWifi",
errSubject: "uni-getConnectedWifi",
}
if (Global.mReceiver == null) {
// 还没调用startWifi 提示报错
option.fail?.(res)
option.complete?.(res)
let err = new UniError("uni-getConnectedWifi",12000,"getConnectedWifi:fail:not invoke startWifi");
option.fail?.(err)
option.complete?.(err)
return
}
......@@ -800,14 +793,14 @@ export function getConnectedWifi(option : GetConnectedWifiOptions) {
// 需要先校验权限,没有位置权限无法获取wifi
if (ActivityCompat.checkSelfPermission(UTSAndroid.getUniActivity()!, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// 尚不具备权限,返回错误
res.errCode = 12001
res.errMsg = "getConnectedWifi:permission loss"
option.fail?.(res)
option.complete?.(res)
let err = new UniError("uni-getConnectedWifi",12001,"getConnectedWifi:permission loss");
option.fail?.(err)
option.complete?.(err)
return;
}
const context = UTSAndroid.getAppContext();
if (context != null) {
const wm = context.getSystemService(
......@@ -818,31 +811,37 @@ export function getConnectedWifi(option : GetConnectedWifiOptions) {
const winfo = wm.getConnectionInfo();
wifiInfo = wrapUniWifiInfoFromConnectInfo(winfo);
let res : UniWifiResult = {
errCode: 0,
errMsg: "getConnectedWifi:ok",
errSubject: "uni-getConnectedWifi",
}
// 判断一下是否wifi 关闭了
if (option.partialInfo != null && option.partialInfo == true) {
if (option.partialInfo!= null) {
let ret : UniWifiInfo = {
SSID: wifiInfo.SSID
}
res.wifi = ret;
} else {
if (wifiInfo.BSSID == null || zeroCountNum(wifiInfo.BSSID) > 3) {
res.errCode = 12005
res.errMsg = "getConnectedWifi:fail:wifi is disable"
option.fail?.(res)
option.complete?.(res)
let err = new UniError("uni-getConnectedWifi",12005,"getConnectedWifi:fail:wifi is disable");
option.fail?.(err)
option.complete?.(err)
return
}
res.wifi = wifiInfo;
}
res.errCode = 0
res.errMsg = "getConnectedWifi:ok"
option.success?.(res)
option.complete?.(res)
return
}
option.fail?.(res)
option.complete?.(res)
let err = new UniError("uni-getConnectedWifi",12000,"getConnectedWifi:fail:not invoke startWifi");
option.fail?.(err)
option.complete?.(err)
}
......@@ -114,24 +114,14 @@ function fetchConnectedWifiWithLocationPromise(option: GetConnectedWifiOptions)
option.success?.(res)
option.complete?.(res)
}else {
const res: UniWifiResult = {
errSubject: "uni-getConnectedWifi",
errCode: 12010,
errMsg: "getConnectedWifi:system internal error",
wifi: null
}
option.fail?.(res)
option.complete?.(res)
let err = new UniError("uni-getConnectedWifi",12010,"getConnectedWifi:system internal error");
option.fail?.(err)
option.complete?.(err)
}
}else {
const res: UniWifiResult = {
errSubject: "uni-getConnectedWifi",
errCode: 12010,
errMsg: "getConnectedWifi:system internal error",
wifi: null
}
option.fail?.(res)
option.complete?.(res)
let err = new UniError("uni-getConnectedWifi",12010,"getConnectedWifi:system internal error");
option.fail?.(err)
option.complete?.(err)
}
}
......@@ -183,14 +173,9 @@ export const stopWifi: StopWifi = function (option: WifiOption) {
* 获取wifi列表, 在调用之前需要引导用户跳转到系统设置-WIFI设置页面,系统搜索周边wifi后app才能接收到回调
*/
export const getWifiList: GetWifiList = function (option: WifiOption) {
let res: UniWifiResult = {
errSubject: "uni-getWifiList",
errCode: 12001,
errMsg: "getWifiList:system not support",
wifi: null
}
option.fail?.(res)
option.complete?.(res)
let err = new UniError("uni-getWifiList",12001,"getWifiList:system not support");
option.fail?.(err)
option.complete?.(err)
}
/* 获取wifi列表的回调
......@@ -213,28 +198,18 @@ export const offGetWifiList: OffGetWifiList = function (callback: UniWifiCallbac
*/
export const getConnectedWifi: GetConnectedWifi = function (option: GetConnectedWifiOptions) {
if (UniWiFiModuleGloabInfo.alreadyStartWifi == false) {
let res: UniWifiResult = {
errSubject: "uni-getConnectedWifi",
errCode: 12000,
errMsg: "getConnectedWifi:not init",
wifi: null
}
option.fail?.(res)
option.complete?.(res)
let err = new UniError("uni-getConnectedWifi",12000,"getConnectedWifi:not init");
option.fail?.(err)
option.complete?.(err)
} else{
if (UIDevice.current.systemVersion >= "13.0") {
requestLocationPromise((success) => {
if (success == true) {
fetchConnectedWifiWithLocationPromise(option)
}else {
let res: UniWifiResult = {
errSubject: "uni-getConnectedWifi",
errCode: 12007,
errMsg: "getConnectedWifi:user denied",
wifi: null
}
option.fail?.(res)
option.complete?.(res)
let err = new UniError("uni-getConnectedWifi",12007,"getConnectedWifi:user denied");
option.fail?.(err)
option.complete?.(err)
}
})
} else{
......@@ -247,14 +222,10 @@ export const getConnectedWifi: GetConnectedWifi = function (option: GetConnected
* 连接wifi
*/
export const connectWifi: ConnectWifi = function (option: WifiConnectOption) {
let res: UniWifiResult = {
errSubject: "uni-connectWifi",
errCode: 12001,
errMsg: "connectWifi:system not support",
wifi: null
}
option.fail?.(res)
option.complete?.(res)
let err = new UniError("uni-connectWifi",12001,"connectWifi:system not support");
option.fail?.(err)
option.complete?.(err)
}
......@@ -290,12 +261,7 @@ export const onOffWifiConnectedWithPartialInfo: OnOffWifiConnectedWithPartialInf
* 设置 wifiList 中 AP 的相关信息。在 onGetWifiList 回调后调用,iOS特有接口。
*/
export const setWifiList: SetWifiList = function (option: WifiOption) {
let res: UniWifiResult = {
errSubject: "uni-setWifiList",
errCode: 12001,
errMsg: "setWifiList:system not support",
wifi: null
}
option.fail?.(res)
option.complete?.(res)
let err = new UniError("uni-setWifiList",12001,"setWifiList:system not support");
option.fail?.(err)
option.complete?.(err)
}
\ No newline at end of file
/**
* Wifi 函数通用入参封装
*/
export type WifiOption = {
success?: (res: UniWifiResult) => void;
fail?: (res: UniWifiResult) => void;
complete?: (res: UniWifiResult) => void;
export type WifiOption = {
success ?: (res : UniWifiResult) => void;
fail ?: (res : UniError) => void;
complete ?: (res : any) => void;
};
/**
* Wifi 链接参数封装
*/
export type WifiConnectOption = {
SSID?: string;
BSSID?: string;
password?: string;
maunal?: boolean;
partialInfo?: boolean; //ios不生效
success?: (res: UniWifiResult) => void;
fail?: (res: UniWifiResult) => void;
complete?: (res: UniWifiResult) => void;
SSID ?: string;
BSSID ?: string;
password ?: string;
maunal ?: boolean;
partialInfo ?: boolean; //ios不生效
success ?: (res : UniWifiResult) => void;
fail ?: (res : UniError) => void;
complete ?: (res : any) => void;
}
/**
* 获取当前链接的wifi信息
*/
export type GetConnectedWifiOptions = {
partialInfo?: boolean
success?: (res: UniWifiResult) => void
fail?: (res: UniWifiResult) => void
complete?: (res: UniWifiResult) => void
partialInfo ?: boolean
success ?: (res : UniWifiResult) => void
fail ?: (res : UniError) => void
complete ?: (res : any) => void
}
/*
* 对外暴露的wifi信息
*/
export type UniWifiInfo = {
SSID: string;
BSSID?: string;
secure?: boolean;
signalStrength?: number;
frequency?: number;
SSID : string;
BSSID ?: string;
secure ?: boolean;
signalStrength ?: number;
frequency ?: number;
}
......@@ -47,35 +47,35 @@ export type UniWifiResult = {
errCode : number,
errSubject : string,
errMsg : string,
wifi: UniWifiInfo | null
wifi : UniWifiInfo | null
}
export type UniWifiCallback = () => void
export type StartWifi = (option: WifiOption) => void
export type StartWifi = (option : WifiOption) => void
export type StopWifi = (option: WifiOption) => void
export type StopWifi = (option : WifiOption) => void
export type GetWifiList = (option: WifiOption) => void
export type GetWifiList = (option : WifiOption) => void
export type OnGetWifiList = (callback: UniWifiCallback) => void
export type OnGetWifiList = (callback : UniWifiCallback) => void
export type OffGetWifiList = (callback: UniWifiCallback) => void
export type OffGetWifiList = (callback : UniWifiCallback) => void
export type GetConnectedWifi = (option: GetConnectedWifiOptions) => void
export type GetConnectedWifi = (option : GetConnectedWifiOptions) => void
export type ConnectWifi = (option: WifiConnectOption) => void
export type ConnectWifi = (option : WifiConnectOption) => void
export type OnWifiConnected = (callback: UniWifiCallback) => void
export type OnWifiConnected = (callback : UniWifiCallback) => void
export type OnWifiConnectedWithPartialInfo = (callback: UniWifiCallback) => void
export type OnWifiConnectedWithPartialInfo = (callback : UniWifiCallback) => void
export type OffWifiConnected = (callback: UniWifiCallback | null) => void
export type OffWifiConnected = (callback : UniWifiCallback | null) => void
export type OnOffWifiConnectedWithPartialInfo = (callback: UniWifiCallback | null) => void
export type OnOffWifiConnectedWithPartialInfo = (callback : UniWifiCallback | null) => void
export type SetWifiList = (option: WifiOption) => void
export type SetWifiList = (option : WifiOption) => void
interface Uni {
/**
......@@ -99,8 +99,9 @@ interface Uni {
* }
* @uniVersion 3.7.7
* @uniVueVersion 2,3 //支持的vue版本
* @autotest { after: 'stopWifi' }
*/
startWifi : StartWifi,
startWifi(option : WifiOption): void,
/**
* 关闭 Wi-Fi 模块
*
......@@ -122,15 +123,16 @@ interface Uni {
* }
* @uniVersion 3.7.7
* @uniVueVersion 2,3 //支持的vue版本
* @autotest { before: 'startWifi' }
*/
stopWifi : StopWifi,
stopWifi(option : WifiOption) : void,
/**
* @param {WifiConnectOption} option
* @tutorial https://uniapp.dcloud.net.cn/api/system/wifi.html#connectWifi
* @uniPlatform {
* "app": {
* "android": {
* "osVer": "4.4.4",
* "osVer": ">=4.4 && <10.0",
* "uniVer": "3.7.0",
* "unixVer": "3.9.0"
* },
......@@ -163,7 +165,7 @@ interface Uni {
]
}
*/
connectWifi : ConnectWifi,
connectWifi(option : WifiConnectOption) : void,
/**
* 请求获取 Wi-Fi 列表。wifiList 数据会在 onGetWifiList 注册的回调中返回。
* @param {WifiOption} option
......@@ -184,17 +186,9 @@ interface Uni {
* }
* @uniVersion 3.7.7
* @uniVueVersion 2,3 //支持的vue版本
* @autotest {
generated: false,
cases:[
{
before: 'startWifi',
after: 'stopWifi'
}
]
}
* @autotest { before: 'startWifi', after: 'stopWifi' }
*/
getWifiList : GetWifiList,
getWifiList(option : WifiOption) : void,
/**
* 监听获取到 Wi-Fi 列表数据事件。
*
......@@ -216,8 +210,25 @@ interface Uni {
* }
* @uniVersion 3.7.7
* @uniVueVersion 2,3 //支持的vue版本
* @autotest { expectCallback: true }
* @autotest {
generated: false,
pollution: false,
expectCallback: true,
before: 'startWifi',
after: 'onGetWifiListAfter',
cases: [
{
output: {
value: 0,
returnKey: '.wifiList.length',
jestExpectSyntax: 'toBeGreaterThan'
},
}
]
}
*/
onGetWifiList : OnGetWifiList,
onGetWifiList(callback : UniWifiCallback) : void,
/**
* 移除获取到 Wi-Fi 列表数据事件的监听函数。
*
......@@ -239,8 +250,9 @@ interface Uni {
* }
* @uniVersion 3.7.7
* @uniVueVersion 2,3 //支持的vue版本
* @autotest { expectCallback: true }
*/
offGetWifiList : OffGetWifiList,
offGetWifiList(callback : UniWifiCallback) : void,
/**
* 获取已连接的 Wi-Fi 信息
*
......@@ -262,8 +274,9 @@ interface Uni {
* }
* @uniVersion 3.7.7
* @uniVueVersion 2,3 //支持的vue版本
* @autotest { before: 'startWifi', after: 'stopWifi' }
*/
getConnectedWifi : GetConnectedWifi,
getConnectedWifi(option : GetConnectedWifiOptions) : void,
/**
* 监听连接上 Wi-Fi 的事件
*
......@@ -285,8 +298,9 @@ interface Uni {
* }
* @uniVersion 3.7.7
* @uniVueVersion 2,3 //支持的vue版本
* @autotest { expectCallback: true }
*/
onWifiConnected : OnWifiConnected,
onWifiConnected(callback : UniWifiCallback) : void,
/**
* 监听连接上 Wi-Fi 的事件。
*
......@@ -308,8 +322,9 @@ interface Uni {
* }
* @uniVersion 3.7.7
* @uniVueVersion 2,3 //支持的vue版本
* @autotest { expectCallback: true }
*/
onWifiConnectedWithPartialInfo : OnWifiConnectedWithPartialInfo,
onWifiConnectedWithPartialInfo(callback : UniWifiCallback) : void,
/**
* 移除连接上 Wi-Fi 的事件的监听函数。
*
......@@ -331,8 +346,9 @@ interface Uni {
* }
* @uniVersion 3.7.7
* @uniVueVersion 2,3 //支持的vue版本
* @autotest { expectCallback: true }
*/
offWifiConnected : OffWifiConnected,
offWifiConnected(callback : UniWifiCallback | null) : void,
/**
* 移除连接上 Wi-Fi 的事件的监听函数。
*
......@@ -341,20 +357,39 @@ interface Uni {
* @uniPlatform {
* "app": {
* "android": {
* "osVer": "4.4.4",
* "uniVer": "3.7.0",
* "unixVer": "3.9.0"
* "osVer": "x",
* "uniVer": "x",
* "unixVer": "x"
* },
* "ios": {
* "osVer": "9.0",
* "uniVer": "3.7.7",
* "unixVer": "3.9.0"
* "osVer": "x",
* "uniVer": "x",
* "unixVer": "x"
* }
* }
* }
* @uniVersion 3.7.7
* @uniVueVersion 2,3 //支持的vue版本
* @autotest { expectCallback: true }
*/
onOffWifiConnectedWithPartialInfo(callback : UniWifiCallback | null) : void,
/**
* SetWifiList 暂未实现
*
* @uniPlatform {
* "app": {
* "android": {
* "osVer": "x",
* "uniVer": "x",
* "unixVer": "x"
* },
* "ios": {
* "osVer": "x",
* "uniVer": "x",
* "unixVer": "x"
* }
* }
* }
*/
onOffWifiConnectedWithPartialInfo : OnOffWifiConnectedWithPartialInfo,
setWifiList : SetWifiList,
setWifiList(option : WifiOption) : void,
}
\ No newline at end of file
export function startWifi() {
return new Promise((resolve, reject) => {
uni.startWifi({
success: () => {
console.log('startWifi success');
resolve()
},
complete: () => {
console.log('startWifi fail');
resolve()
}
})
})
}
export function stopWifi() {
return new Promise((resolve, reject) => {
uni.stopWifi({
complete: resolve
})
})
}
export function onGetWifiListAfter() {
return new Promise((resolve, reject) => {
uni.getWifiList({
success() {
setTimeout(resolve, 500)
},
complete() {
stopWifi().finally(resolve)
}
})
})
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册