Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
DCloud
uni-api
提交
926786d9
U
uni-api
项目概览
DCloud
/
uni-api
通知
670
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看板
提交
926786d9
编写于
11月 07, 2022
作者:
杜庆泉
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
wifi onConnnectWifi api功能实现
上级
9377f404
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
112 addition
and
36 deletion
+112
-36
pages/index/index.vue
pages/index/index.vue
+9
-0
uni_modules/uni-wifi/package.json
uni_modules/uni-wifi/package.json
+2
-2
uni_modules/uni-wifi/utssdk/app-android/index.uts
uni_modules/uni-wifi/utssdk/app-android/index.uts
+101
-34
未找到文件。
pages/index/index.vue
浏览文件 @
926786d9
...
...
@@ -37,6 +37,10 @@
partialInfo
:
false
,
complete
:(
res
)
=>
{
console
.
log
(
res
);
uni
.
showToast
({
icon
:
'
none
'
,
title
:
res
.
wifi
.
SSID
})
}
});
},
...
...
@@ -47,6 +51,11 @@
uni
.
onGetWifiList
(
function
(
res
){
console
.
log
(
res
);
});
uni
.
onWifiConnected
(
function
(
res
){
console
.
log
(
"
11199
"
);
console
.
log
(
"
onWifiConnected =
"
+
JSON
.
stringify
(
res
));
});
},
fail
:(
res
)
=>
{
console
.
log
(
"
fail:
"
+
JSON
.
stringify
(
res
));
},
complete
:(
res
)
=>
{
...
...
uni_modules/uni-wifi/package.json
浏览文件 @
926786d9
...
...
@@ -37,8 +37,8 @@
"startWifi"
:
"startWifi"
,
"stopWifi"
:
"stopWifi"
,
"getWifiList"
:
"getWifiList"
,
"onGetWifiList"
:
"onGetWifiList"
"onGetWifiList"
:
"onGetWifiList"
,
"onWifiConnected"
:
"onWifiConnected"
}
},
"dependencies"
:
[],
...
...
uni_modules/uni-wifi/utssdk/app-android/index.uts
浏览文件 @
926786d9
import Context from "android.content.Context";
import { getAppContext,getUniActivity } from "io.dcloud.uts.android";
import WifiManager from "android.net.wifi.WifiManager";
import WifiInfo from "android.net.wifi.WifiInfo";
import ConnectivityManager from "android.net.ConnectivityManager";
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";
import Thread from "java.lang.Thread";
/**
* Wifi 函数通用入参封装
...
...
@@ -33,7 +36,6 @@ class UniWifiInfo {
signalStrength:Number = 0;
frequency:Number = 0;
constructor(scanResult?:ScanResult){
if(scanResult != null){
// 如果是通过扫描列表得到的数据,进行封装
...
...
@@ -52,8 +54,36 @@ class UniWifiInfo {
this.secure = true;
}
}
}
/**
* 根据connectInfo 链接信息对wifi数据结构进行初始化
*/
wrapConnectInfo(connectInfo:WifiInfo):void{
console.log(connectInfo.getSSID());
console.log(connectInfo.getBSSID());
if(connectInfo.getBSSID() == null){
return
}
if (connectInfo != null) {
if(connectInfo.getSSID() != null){
let s = connectInfo.getSSID();
// 微信不带,这里需要去掉引号
if (s.length > 2 && s.charAt(0) == '"' && s.charAt(s.length - 1) == '"') {
s = s.substring(1, s.length - 1);
}
this.SSID = s;
}
this.BSSID = connectInfo.getBSSID();
this.signalStrength = connectInfo.getRssi()+100; //Android返回的值是-100~0,而微信API规范是0~100,值越大信号越好,需要+100拉齐
this.frequency = connectInfo.getFrequency();
// WifiInfo.macAddress = winfo.getMacAddress(); //注意此代码涉及隐私,首先需要配置权限,没有权限会返回"02:00:00:00:00:00";然后需要在隐私协议中声明用途。如不需要,可注释掉本行
}
}
}
/**
...
...
@@ -68,7 +98,6 @@ type GetConnectedWifiOptions = {
/**
* 全局数据储存
*/
...
...
@@ -78,6 +107,7 @@ class Global{
static WIFI_AUTH_OPEN:String = "";
static WIFI_AUTH_ROAM:String = "[ESS]";
static getWifiListCallbackList:UTSCallback[] = []
static onWifiConnectCallbackList:UTSCallback[] = []
}
...
...
@@ -96,21 +126,50 @@ class CustomBroadcastReceiver extends BroadcastReceiver{
override onReceive(context: Context, intent: Intent) :void {
if (intent.action == WifiManager.SCAN_RESULTS_AVAILABLE_ACTION) {
if (intent.action == WifiManager.WIFI_STATE_CHANGED_ACTION) {
let state =
intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN)
if (state == WifiManager.WIFI_STATE_ENABLED) {
// 获取当前的connectInfo 并且进行数据封装
let uniWifiInfo = new UniWifiInfo(null)
let winfo = this.mWifiManager!.getConnectionInfo();
while(winfo.bssid == null){
Thread.sleep(1000)
winfo = this.mWifiManager!!.getConnectionInfo();
}
let results = mWifiManager!.scanResults;
console.log(winfo)
// 封装成数据对象
uniWifiInfo.wrapConnectInfo(winfo)
let res = {
errMsg: 'onWifiConnected:ok',
errCode: 0,
wifi: uniWifiInfo
}
console.log("Global.onWifiConnectCallbackList == " + Global.onWifiConnectCallbackList.length);
// wifi状态可用了,分发当前的链接状态给已注册的监听集合
for(let perCallback in Global.onWifiConnectCallbackList){
perCallback(res);
}
}
}
if (intent.action == WifiManager.SCAN_RESULTS_AVAILABLE_ACTION) {
// wifi 扫描结果回调
let results = this.mWifiManager!.scanResults;
if (results != null) {
Global.scanList = []
for(scanResult in results){
for(
let
scanResult in results){
if(scanResult.SSID == null){
continue;
}
Global.scanList.
add
(new UniWifiInfo(scanResult));
Global.scanList.
push
(new UniWifiInfo(scanResult));
}
// 挨个通知,所有的监听器
for(perCallback in Global.getWifiListCallbackList){
for(
let
perCallback in Global.getWifiListCallbackList){
const data = new JSONObject();
let mainJsonStr = Gson().toJson(Global.scanList);
...
...
@@ -146,6 +205,23 @@ export function getWifiList(option:WifiOption) {
}
/**
* wifi 链接成功的回调注册
*/
export function onWifiConnected(callback: UTSCallback) {
Global.onWifiConnectCallbackList.push(callback)
}
/**
* wifi 链接成功的回调取消注册
*/
export function offWifiConnected(callback: UTSCallback) {
let callbackIndex = Global.onWifiConnectCallbackList.indexOf(callback)
if(callbackIndex > 0){
Global.onWifiConnectCallbackList.splice(callbackIndex, 1);
}
}
/**
* 注册Wifi列表的监听事件
*/
...
...
@@ -195,7 +271,11 @@ export function startWifi(option:WifiOption) {
Global.mReceiver = new CustomBroadcastReceiver(wifiManager)
}
let filter = new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)
let filter = new IntentFilter()
filter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)
filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION)
filter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
getUniActivity()!.registerReceiver(Global.mReceiver, filter)
wifiManager.startScan()
var result = {
...
...
@@ -255,15 +335,11 @@ export function getConnectedWifi(option: GetConnectedWifiOptions) {
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();
...
...
@@ -271,19 +347,11 @@ export function getConnectedWifi(option: GetConnectedWifiOptions) {
const wm = context.getSystemService(
Context.WIFI_SERVICE
) as WifiManager;
const winfo = wm.getConnectionInfo(); // TODO 这个方法在Android12标记为已废弃。替代方法还没找到
if (winfo != null) {
let s = winfo.getSSID();
// 测试android 12上可以使用
const winfo = wm.getConnectionInfo();
// 封装成数据对象
WifiInfo.wrapConnectInfo(winfo);
// 微信不带,这里需要去掉
if (s.length > 2 && s.charAt(0) == '"' && s.charAt(s.length - 1) == '"') {
s = s.substring(1, s.length - 1);
}
WifiInfo.SSID = s;
WifiInfo.BSSID = winfo.getBSSID();
WifiInfo.signalStrength = winfo.getRssi()+100; //Android返回的值是-100~0,而微信API规范是0~100,值越大信号越好,需要+100拉齐
WifiInfo.frequency = winfo.getFrequency();
// WifiInfo.macAddress = winfo.getMacAddress(); //注意此代码涉及隐私,首先需要配置权限,没有权限会返回"02:00:00:00:00:00";然后需要在隐私协议中声明用途。如不需要,可注释掉本行
res.errCode = 0
res.errMsg = "getConnectedWifi:ok"
res.wifi = WifiInfo;
...
...
@@ -291,7 +359,6 @@ export function getConnectedWifi(option: GetConnectedWifiOptions) {
option.complete?.(res)
return
}
}
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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录