Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
DCloud
uni-api
提交
d3df18a3
U
uni-api
项目概览
DCloud
/
uni-api
通知
671
Star
23
Fork
12
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
3
列表
看板
标记
里程碑
合并请求
1
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
U
uni-api
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
3
Issue
3
列表
看板
标记
里程碑
合并请求
1
合并请求
1
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
d3df18a3
编写于
11月 04, 2022
作者:
杜庆泉
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
wifi 插件实现中
上级
cc5fa837
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
312 addition
and
28 deletion
+312
-28
pages/index/index.vue
pages/index/index.vue
+51
-11
uni_modules/uni-memorywarning/utssdk/app-android/index.uts
uni_modules/uni-memorywarning/utssdk/app-android/index.uts
+3
-2
uni_modules/uni-wifi/package.json
uni_modules/uni-wifi/package.json
+5
-1
uni_modules/uni-wifi/utssdk/app-android/index.uts
uni_modules/uni-wifi/utssdk/app-android/index.uts
+253
-14
未找到文件。
pages/index/index.vue
浏览文件 @
d3df18a3
...
...
@@ -9,7 +9,10 @@
<button
@
tap=
"testGetBatteryInfo"
>
获取电池电量
</button>
<button
@
tap=
"testonMemoryWarning"
>
开启内存不足告警监听
</button>
<button
@
tap=
"testoffMemoryWarning"
>
关闭内存不足告警监听
</button>
<button
@
tap=
"testGetConnectedWifi"
>
获取当前wifi信息
</button>
<button
@
tap=
"testStartWifi"
>
开启wifi功能
</button>
<button
@
tap=
"testStopWifi"
>
关闭wifi功能
</button>
<button
@
tap=
"testGetWifiList"
>
获取当前wifi列表
</button>
<button
@
tap=
"testGetConnnectWifi"
>
获取当前连接的wifi
</button>
</view>
</
template
>
...
...
@@ -18,29 +21,66 @@
export
default
{
data
()
{
return
{
title
:
'
Hello
'
title
:
'
Hello
'
,
memListener
:
null
,
}
},
onLoad
()
{
},
methods
:
{
testGetConnectedWifi
()
{
onMemoryWarning
:
function
(
res
){
console
.
log
(
res
);
},
testGetConnnectWifi
(){
uni
.
getConnectedWifi
({
success
(
res
)
{
console
.
log
(
res
);
partialInfo
:
false
});
},
testStartWifi
(){
uni
.
startWifi
({
success
:(
res
)
=>
{
console
.
log
(
"
success:
"
+
JSON
.
stringify
(
res
));
uni
.
onGetWifiList
(
function
(
res
){
console
.
log
(
res
);
});
},
fail
:(
res
)
=>
{
console
.
log
(
"
fail:
"
+
JSON
.
stringify
(
res
));
},
complete
:(
res
)
=>
{
console
.
log
(
"
complete:
"
+
JSON
.
stringify
(
res
));
}
})
},
testonMemoryWarning
()
{
uni
.
onMemoryWarning
(
function
(
res
)
{
console
.
log
(
res
);
testStopWifi
()
{
uni
.
stopWifi
({
success
:(
res
)
=>
{
console
.
log
(
"
success:
"
+
JSON
.
stringify
(
res
));
},
fail
:(
res
)
=>
{
console
.
log
(
"
fail:
"
+
JSON
.
stringify
(
res
));
},
complete
:(
res
)
=>
{
console
.
log
(
"
complete:
"
+
JSON
.
stringify
(
res
));
}
})
},
testoffMemoryWarning
(){
uni
.
offMemoryWarning
(
function
(
res
)
{
console
.
log
(
res
);
testGetWifiList
()
{
uni
.
getWifiList
({
success
:(
res
)
=>
{
console
.
log
(
"
success:
"
+
JSON
.
stringify
(
res
));
},
fail
:(
res
)
=>
{
console
.
log
(
"
fail:
"
+
JSON
.
stringify
(
res
));
},
complete
:(
res
)
=>
{
console
.
log
(
"
complete:
"
+
JSON
.
stringify
(
res
));
}
})
},
testonMemoryWarning
()
{
uni
.
onMemoryWarning
(
this
.
onMemoryWarning
)
},
testoffMemoryWarning
(){
// uni.offMemoryWarning(this.onMemoryWarning)
uni
.
offMemoryWarning
()
},
testScreenShotListen
()
{
var
that
=
this
;
...
...
uni_modules/uni-memorywarning/utssdk/app-android/index.uts
浏览文件 @
d3df18a3
...
...
@@ -10,7 +10,6 @@ const onAppTrimMemoryListener = (res: number) => {
}
export function onMemoryWarning(callback: (res: number) => void) {
if (listeners.length === 0) {
// 仅首次执行底层的实际监听
onAppTrimMemory(onAppTrimMemoryListener)
...
...
@@ -22,13 +21,15 @@ export function onMemoryWarning(callback: (res: number) => void) {
listeners.push(callback)
}
export function offMemoryWarning(callback:
((res: number) => void) |
null) {
export function offMemoryWarning(callback:
UTSCallback | null =
null) {
if(callback == null){
// 清除全部回调
listeners = []
offAppTrimMemory(null);
return
}
// 清除指定回调
const index = listeners.indexOf(callback)
if (index > -1) {
...
...
uni_modules/uni-wifi/package.json
浏览文件 @
d3df18a3
...
...
@@ -34,7 +34,11 @@
"uni-ext-api"
:{
"uni"
:
{
"getConnectedWifi"
:
"getConnectedWifi"
,
"startWifi"
:
"startWifi"
"startWifi"
:
"startWifi"
,
"stopWifi"
:
"stopWifi"
,
"getWifiList"
:
"getWifiList"
,
"onGetWifiList"
:
"onGetWifiList"
}
},
"dependencies"
:
[],
...
...
uni_modules/uni-wifi/utssdk/app-android/index.uts
浏览文件 @
d3df18a3
import Context from "android.content.Context";
import { getAppContext } from "io.dcloud.uts.android";
import { getAppContext
,getUniActivity
} from "io.dcloud.uts.android";
import WifiManager from "android.net.wifi.WifiManager";
import Manifest from "android.Manifest";
import PackageManager from "android.content.pm.PackageManager";
import ScanResult from "android.net.wifi.ScanResult";
import BroadcastReceiver from "android.content.BroadcastReceiver";
import ActivityCompat from "androidx.core.app.ActivityCompat";
import Toast from "android.widget.Toast";
import IntentFilter from "android.content.IntentFilter";
import Gson from "com.google.gson.Gson";
import JSONObject from "com.alibaba.fastjson.JSONObject";
import Intent from "android.content.Intent";
/**
* Wifi 函数通用入参封装
*/
type WifiOption = {
success?: (res: object) => void;
fail?: (res: object) => void;
complete?: (res: object) => void;
};
/**
* Wifi信息统一数据结构
*/
class UniWifiInfo {
SSID:String = "";
BSSID:String = "";
secure:boolean = false;
signalStrength:Number = 0;
frequency:Number = 0;
constructor(scanResult?:ScanResult){
if(scanResult != null){
// 如果是通过扫描列表得到的数据,进行封装
this.BSSID = scanResult.BSSID;
this.SSID = scanResult.SSID;
this.signalStrength = scanResult.level;
this.frequency = scanResult.frequency;
// 是否安全,微信的标准是是否需要密码。 来源:https://developers.weixin.qq.com/community/develop/doc/00064cf1790458db19cddf9925ac00?highLine=WifiInfo
this.secure = false;
let capabilities = scanResult.capabilities.trim();
if (capabilities != null && (capabilities.equals(Global.WIFI_AUTH_OPEN) || capabilities.equals(Global.WIFI_AUTH_ROAM))) {
this.secure = false;
}else{
this.secure = true;
}
}
}
}
/**
* 获取当前链接的wifi信息
*/
type GetConnectedWifiOptions = {
partialInfo?:boolean
success?: (res: UTSJSONObject) => void
fail?: (res: UTSJSONObject) => void
complete?: (res: UTSJSONObject) => void
}
export function startWifi() { //TODO
/**
* 全局数据储存
*/
class Global{
static mReceiver?:CustomBroadcastReceiver = null;
static scanList:UniWifiInfo[] = []
static WIFI_AUTH_OPEN:String = "";
static WIFI_AUTH_ROAM:String = "[ESS]";
static getWifiListCallbackList:UTSCallback[] = []
}
/**
* 自定义wifi变化广播监听器
*/
class CustomBroadcastReceiver extends BroadcastReceiver{
mWifiManager?:WifiManager = null;
constructor(wifiManager: WifiManager){
super();
this.mWifiManager = wifiManager;
}
override onReceive(context: Context, intent: Intent) :void {
if (intent.action == WifiManager.SCAN_RESULTS_AVAILABLE_ACTION) {
let results = mWifiManager!.scanResults;
if (results != null) {
Global.scanList = []
for(scanResult in results){
if(scanResult.SSID == null){
continue;
}
Global.scanList.add(new UniWifiInfo(scanResult));
}
// 挨个通知,所有的监听器
for(perCallback in Global.getWifiListCallbackList){
const data = new JSONObject();
let mainJsonStr = Gson().toJson(Global.scanList);
data["wifiList"] = mainJsonStr
perCallback(data);
}
}
}
}
}
/************************* 下面是对外提供的函数 *************************/
/**
* 获取wifi列表
*/
export function getWifiList(option:WifiOption) {
let wifiManager: WifiManager =
getAppContext()!.getSystemService(Context.WIFI_SERVICE) as WifiManager
wifiManager.startScan()
let ret = {
errNo:0,
errCode:0,
errMsg:"getWifiList:ok"
}
option?.success?.(ret)
option?.complete?.(ret)
}
/**
* 注册Wifi列表的监听事件
*/
export function onGetWifiList(callback: UTSCallback) {
Global.getWifiListCallbackList.push(callback)
}
/**
* 取消注册Wifi列表的监听事件
*/
export function offGetWifiList(callback: UTSCallback) {
let callbackIndex = Global.getWifiListCallbackList.indexOf(callback)
if(callbackIndex > 0){
Global.getWifiListCallbackList.splice(callbackIndex, 1);
}
}
/**
* 开启wifi
*/
export function startWifi(option:WifiOption) {
// 需要先开启wifi,才能使用后续的功能
let requestCode = 1001;
let permissionWifi = arrayOf("android.permission.ACCESS_FINE_LOCATION");
// 检查权限
if (ActivityCompat.checkSelfPermission(getUniActivity()!, Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(getUniActivity()!, permissionWifi, requestCode)
// 尚不具备权限,返回错误
var result = {
errNo:0,
errCode:0,
errMsg:"startWifi:premission loss"
}
option.fail?.(result)
option.complete?.(result)
return;
}
// 具备了权限,继续前进
let wifiManager: WifiManager =
getAppContext()!.getSystemService(Context.WIFI_SERVICE) as WifiManager
if (Global.mReceiver == null) {
Global.mReceiver = new CustomBroadcastReceiver(wifiManager)
}
let filter = new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)
getUniActivity()!.registerReceiver(Global.mReceiver, filter)
wifiManager.startScan()
var result = {
errNo:0,
errCode:0,
errMsg:"startWifi:ok"
}
option.success?.(result)
option.complete?.(result)
}
export function getConnectedWifi(options: GetConnectedWifiOptions) {
let WifiInfo = {
SSID : "",
BSSID : "",
// secure : Boolean, // TODO 此值是微信的规范,Android原生并没有。不确定其的逻辑,需要在微信环境做各种测试才能验证
signalStrength : 0,
frequency : 0,
// macAddress : ""
/**
* 关闭wifi
*/
export function stopWifi(option:WifiOption) {
// 需要先开启wifi,才能使用后续的功能
if (Global.mReceiver == null) {
var result = {
errNo:12000,
errCode:12000,
errMsg:"stopWifi:not init"
}
option.fail?.(result)
option.complete?.(result)
return
}
getUniActivity()!.unregisterReceiver(Global.mReceiver)
var result = {
errNo:0,
errCode:0,
errMsg:"stopWifi:ok"
}
option.success?.(result)
option.complete?.(result)
}
/**
* 获取当前连接中的wifi信息
*/
export function getConnectedWifi(option: GetConnectedWifiOptions) {
console.log(option);
let WifiInfo = new UniWifiInfo(null)
let res = {
errMsg: 'getConnectedWifi:fail. please check permission about location or enable wifi or connect wifi',
errCode: -1,
WifiInfo: WifiInfo
}
if (ActivityCompat.checkSelfPermission(getUniActivity()!, Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED) {
// 尚不具备权限,返回错误
option.fail?.(res)
option.complete?.(res)
return;
}
// TODO 应该try catch一下,把系统的错误码给返回来,然后和微信的错误码拉齐
// 需要先校验权限,没有位置权限无法获取wifi
const context = getAppContext();
...
...
@@ -34,6 +272,7 @@ export function getConnectedWifi(options: GetConnectedWifiOptions) {
Context.WIFI_SERVICE
) as WifiManager;
const winfo = wm.getConnectionInfo(); // TODO 这个方法在Android12标记为已废弃。替代方法还没找到
console.log(winfo);
if (winfo != null) {
let s = winfo.getSSID();
console.log(s); // TODO 注意此值带着双引号。需要验证微信的值是否带双引号,如微信不带,这里需要去掉
...
...
@@ -48,11 +287,11 @@ export function getConnectedWifi(options: GetConnectedWifiOptions) {
// WifiInfo.macAddress = winfo.getMacAddress(); //注意此代码涉及隐私,首先需要配置权限,没有权限会返回"02:00:00:00:00:00";然后需要在隐私协议中声明用途。如不需要,可注释掉本行
res.errCode = 0
res.errMsg = "getConnectedWifi:ok"
option
s
.success?.(res)
option
s
.complete?.(res)
option.success?.(res)
option.complete?.(res)
return
}
}
option
s
.fail?.(res)
option
s
.complete?.(res)
option.fail?.(res)
option.complete?.(res)
}
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录