Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
DCloud
uni-api
提交
bbea88dd
U
uni-api
项目概览
DCloud
/
uni-api
通知
672
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看板
提交
bbea88dd
编写于
11月 08, 2022
作者:
杜庆泉
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
优化onConnectWifi 函数
上级
926786d9
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
161 addition
and
140 deletion
+161
-140
uni_modules/uni-wifi/utssdk/app-android/index.uts
uni_modules/uni-wifi/utssdk/app-android/index.uts
+161
-140
未找到文件。
uni_modules/uni-wifi/utssdk/app-android/index.uts
浏览文件 @
bbea88dd
import Context from "android.content.Context";
import Context from "android.content.Context";
import { getAppContext,getUniActivity } from "io.dcloud.uts.android";
import { getAppContext,
getUniActivity } from "io.dcloud.uts.android";
import WifiManager from "android.net.wifi.WifiManager";
import WifiManager from "android.net.wifi.WifiManager";
import WifiInfo from "android.net.wifi.WifiInfo";
import WifiInfo from "android.net.wifi.WifiInfo";
import ConnectivityManager from "android.net.ConnectivityManager";
import Manifest from "android.Manifest";
import Manifest from "android.Manifest";
import PackageManager from "android.content.pm.PackageManager";
import PackageManager from "android.content.pm.PackageManager";
import ScanResult from "android.net.wifi.ScanResult";
import ScanResult from "android.net.wifi.ScanResult";
...
@@ -24,49 +22,62 @@ type WifiOption = {
...
@@ -24,49 +22,62 @@ type WifiOption = {
complete?: (res: object) => void;
complete?: (res: object) => void;
};
};
/**
* Wifi 链接参数封装
*/
type WifiConnectOption = {
SSID:string;
BSSID:string;
password:string;
maunal:boolean;
partialInfo:boolean;
success?: (res: object) => void;
fail?: (res: object) => void;
complete?: (res: object) => void;
}
/**
/**
* Wifi信息统一数据结构
* Wifi信息统一数据结构
*/
*/
class UniWifiInfo {
class UniWifiInfo {
SSID:String = "";
SSID:
String = "";
BSSID:String = "";
BSSID:
String = "";
secure:boolean = false;
secure:
boolean = false;
signalStrength:Number = 0;
signalStrength:
Number = 0;
frequency:Number = 0;
frequency:
Number = 0;
constructor(scanResult?:
ScanResult)
{
constructor(scanResult?:
ScanResult)
{
if
(scanResult != null)
{
if
(scanResult != null)
{
// 如果是通过扫描列表得到的数据,进行封装
// 如果是通过扫描列表得到的数据,进行封装
this.BSSID = scanResult.BSSID;
this.BSSID = scanResult.BSSID;
this.SSID = scanResult.SSID;
this.SSID = scanResult.SSID;
this.signalStrength = scanResult.level;
this.signalStrength = scanResult.level;
this.frequency = scanResult.frequency;
this.frequency = scanResult.frequency;
// 是否安全,微信的标准是是否需要密码。 来源:https://developers.weixin.qq.com/community/develop/doc/00064cf1790458db19cddf9925ac00?highLine=WifiInfo
// 是否安全,微信的标准是是否需要密码。 来源:https://developers.weixin.qq.com/community/develop/doc/00064cf1790458db19cddf9925ac00?highLine=WifiInfo
this.secure = false;
this.secure = false;
let capabilities = scanResult.capabilities.trim();
let capabilities = scanResult.capabilities.trim();
if (capabilities != null && (capabilities.equals(Global.WIFI_AUTH_OPEN) || capabilities.equals(Global.WIFI_AUTH_ROAM))) {
if (capabilities != null && (capabilities.equals(Global.WIFI_AUTH_OPEN) || capabilities.equals(Global.WIFI_AUTH_ROAM))) {
this.secure = false;
this.secure = false;
}
else
{
}
else
{
this.secure = true;
this.secure = true;
}
}
}
}
}
}
/**
/**
* 根据connectInfo 链接信息对wifi数据结构进行初始化
* 根据connectInfo 链接信息对wifi数据结构进行初始化
*/
*/
wrapConnectInfo(connectInfo:
WifiInfo):void
{
wrapConnectInfo(connectInfo:
WifiInfo): void
{
console.log(connectInfo.getSSID());
console.log(connectInfo.getSSID());
console.log(connectInfo.getBSSID());
console.log(connectInfo.getBSSID());
if
(connectInfo.getBSSID() == null)
{
if
(connectInfo.getBSSID() == null)
{
return
return
}
}
if (connectInfo != null) {
if (connectInfo != null) {
if
(connectInfo.getSSID() != null)
{
if
(connectInfo.getSSID() != null)
{
let s = connectInfo.getSSID();
let s = connectInfo.getSSID();
// 微信不带,这里需要去掉引号
// 微信不带,这里需要去掉引号
if (s.length > 2 && s.charAt(0) == '"' && s.charAt(s.length - 1) == '"') {
if (s.length > 2 && s.charAt(0) == '"' && s.charAt(s.length - 1) == '"') {
...
@@ -74,26 +85,26 @@ class UniWifiInfo {
...
@@ -74,26 +85,26 @@ class UniWifiInfo {
}
}
this.SSID = s;
this.SSID = s;
}
}
this.BSSID = connectInfo.getBSSID();
this.BSSID = connectInfo.getBSSID();
this.signalStrength = connectInfo.getRssi()+100; //Android返回的值是-100~0,而微信API规范是0~100,值越大信号越好,需要+100拉齐
//Android返回的值是-100~0,而微信API规范是0~100,值越大信号越好,需要+100拉齐
this.signalStrength = connectInfo.getRssi() + 100;
this.frequency = connectInfo.getFrequency();
this.frequency = connectInfo.getFrequency();
// WifiInfo.macAddress = winfo.getMacAddress(); //注意此代码涉及隐私,首先需要配置权限,没有权限会返回"02:00:00:00:00:00";然后需要在隐私协议中声明用途。如不需要,可注释掉本行
}
}
}
}
}
}
/**
/**
* 获取当前链接的wifi信息
* 获取当前链接的wifi信息
*/
*/
type GetConnectedWifiOptions = {
type GetConnectedWifiOptions = {
partialInfo?:boolean
partialInfo?:
boolean
success?: (res: UTSJSONObject) => void
success?: (res: UTSJSONObject) => void
fail?: (res: UTSJSONObject) => void
fail?: (res: UTSJSONObject) => void
complete?: (res: UTSJSONObject) => void
complete?: (res: UTSJSONObject) => void
}
}
...
@@ -101,87 +112,93 @@ type GetConnectedWifiOptions = {
...
@@ -101,87 +112,93 @@ type GetConnectedWifiOptions = {
/**
/**
* 全局数据储存
* 全局数据储存
*/
*/
class Global{
class Global
{
static mReceiver?:CustomBroadcastReceiver = null;
static mReceiver?:
CustomBroadcastReceiver = null;
static scanList:
UniWifiInfo[] =
[]
static scanList:
UniWifiInfo[] =
[]
static WIFI_AUTH_OPEN:
String
= "";
static WIFI_AUTH_OPEN:
String
= "";
static WIFI_AUTH_ROAM:
String
= "[ESS]";
static WIFI_AUTH_ROAM:
String
= "[ESS]";
static getWifiListCallbackList:UTSCallback[] = []
static getWifiListCallbackList:
UTSCallback[] = []
static onWifiConnectCallbackList:UTSCallback[] = []
static onWifiConnectCallbackList:
UTSCallback[] = []
}
}
/**
/**
* 自定义wifi变化广播监听器
* 自定义wifi变化广播监听器
*/
*/
class CustomBroadcastReceiver extends BroadcastReceiver{
class CustomBroadcastReceiver extends BroadcastReceiver {
mWifiManager?:WifiManager = null;
constructor(wifiManager: WifiManager){
mWifiManager?: WifiManager = null;
constructor(wifiManager: WifiManager) {
super();
super();
this.mWifiManager = wifiManager;
this.mWifiManager = wifiManager;
}
}
override onReceive(
context: Context, intent: Intent) :
void {
override onReceive(
_context: Context, intent: Intent):
void {
if (intent.action == WifiManager.WIFI_STATE_CHANGED_ACTION) {
if (intent.action == WifiManager.WIFI_STATE_CHANGED_ACTION) {
let state =
let state =
intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN)
intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN)
if (state == WifiManager.WIFI_STATE_ENABLED) {
if (state == WifiManager.WIFI_STATE_ENABLED) {
// 获取当前的connectInfo 并且进行数据封装
// 获取当前的connectInfo 并且进行数据封装
let uniWifiInfo = new UniWifiInfo(null)
let uniWifiInfo = new UniWifiInfo(null)
let winfo = this.mWifiManager!.getConnectionInfo();
//做一些异步操作
while(winfo.bssid == null){
setTimeout(function() {
Thread.sleep(1000)
// BroadcastReceiver 中不能执行耗时任务,需要使用setTimeout
winfo = this.mWifiManager!!.getConnectionInfo();
console.log(Thread.currentThread().getName())
}
let winfo = this.mWifiManager!.getConnectionInfo();
while (winfo.bssid == null) {
console.log(winfo)
Thread.sleep(1000)
// 封装成数据对象
winfo = this.mWifiManager!!.getConnectionInfo();
uniWifiInfo.wrapConnectInfo(winfo)
}
let res = {
errMsg: 'onWifiConnected:ok',
// 封装成数据对象
errCode: 0,
uniWifiInfo.wrapConnectInfo(winfo)
wifi: uniWifiInfo
let res = {
}
errMsg: 'onWifiConnected:ok',
console.log("Global.onWifiConnectCallbackList == " + Global.onWifiConnectCallbackList.length);
errCode: 0,
// wifi状态可用了,分发当前的链接状态给已注册的监听集合
wifi: uniWifiInfo
for(let perCallback in Global.onWifiConnectCallbackList){
}
perCallback(res);
console.log("Global.onWifiConnectCallbackList == " + Global.onWifiConnectCallbackList.length);
}
// wifi状态可用了,分发当前的链接状态给已注册的监听集合
}
for (let perCallback in Global.onWifiConnectCallbackList) {
perCallback(res);
}
}, 100);
}
}
}
if (intent.action == WifiManager.SCAN_RESULTS_AVAILABLE_ACTION) {
if (intent.action == WifiManager.SCAN_RESULTS_AVAILABLE_ACTION) {
// wifi 扫描结果回调
// wifi 扫描结果回调
let results = this.mWifiManager!.scanResults;
let results = this.mWifiManager!.scanResults;
if (results != null) {
if (results != null) {
Global.scanList = []
Global.scanList = []
for
(let scanResult in results)
{
for
(let scanResult in results)
{
if
(scanResult.SSID == null)
{
if
(scanResult.SSID == null)
{
continue;
continue;
}
}
Global.scanList.push(new UniWifiInfo(scanResult));
Global.scanList.push(new UniWifiInfo(scanResult));
}
}
// 挨个通知,所有的监听器
// 挨个通知,所有的监听器
for
(let perCallback in Global.getWifiListCallbackList)
{
for
(let perCallback in Global.getWifiListCallbackList)
{
const data = new JSONObject();
const data = new JSONObject();
let mainJsonStr = Gson().toJson(Global.scanList);
let mainJsonStr = Gson().toJson(Global.scanList);
data["wifiList"] = mainJsonStr
data["wifiList"] = mainJsonStr
perCallback(data);
perCallback(data);
}
}
}
}
}
}
}
}
}
}
/************************* 下面是对外提供的函数 *************************/
/************************* 下面是对外提供的函数 *************************/
...
@@ -189,169 +206,173 @@ class CustomBroadcastReceiver extends BroadcastReceiver{
...
@@ -189,169 +206,173 @@ class CustomBroadcastReceiver extends BroadcastReceiver{
/**
/**
* 获取wifi列表
* 获取wifi列表
*/
*/
export function getWifiList(option:
WifiOption) {
export function getWifiList(option:
WifiOption) {
let wifiManager: WifiManager =
let wifiManager: WifiManager =
getAppContext()!.getSystemService(Context.WIFI_SERVICE) as WifiManager
getAppContext()!.getSystemService(Context.WIFI_SERVICE) as WifiManager
wifiManager.startScan()
wifiManager.startScan()
let ret = {
let ret = {
errNo:0,
errNo:
0,
errCode:0,
errCode:
0,
errMsg:"getWifiList:ok"
errMsg:
"getWifiList:ok"
}
}
option?.success?.(ret)
option?.success?.(ret)
option?.complete?.(ret)
option?.complete?.(ret)
}
}
/**
/**
* wifi 链接成功的回调注册
* wifi 链接成功的回调注册
*/
*/
export function onWifiConnected(callback: UTSCallback) {
export function onWifiConnected(callback: UTSCallback) {
Global.onWifiConnectCallbackList.push(callback)
Global.onWifiConnectCallbackList.push(callback)
}
}
/**
/**
* wifi 链接成功的回调取消注册
* wifi 链接成功的回调取消注册
*/
*/
export function offWifiConnected(callback: UTSCallback) {
export function offWifiConnected(callback: UTSCallback) {
let callbackIndex = Global.onWifiConnectCallbackList.indexOf(callback)
let callbackIndex = Global.onWifiConnectCallbackList.indexOf(callback)
if
(callbackIndex > 0)
{
if
(callbackIndex > 0)
{
Global.onWifiConnectCallbackList.splice(callbackIndex, 1);
Global.onWifiConnectCallbackList.splice(callbackIndex, 1);
}
}
}
}
/**
/**
* 注册Wifi列表的监听事件
* 注册Wifi列表的监听事件
*/
*/
export function onGetWifiList(callback: UTSCallback) {
export function onGetWifiList(callback: UTSCallback) {
Global.getWifiListCallbackList.push(callback)
Global.getWifiListCallbackList.push(callback)
}
}
/**
/**
* 取消注册Wifi列表的监听事件
* 取消注册Wifi列表的监听事件
*/
*/
export function offGetWifiList(callback: UTSCallback) {
export function offGetWifiList(callback: UTSCallback) {
let callbackIndex = Global.getWifiListCallbackList.indexOf(callback)
let callbackIndex = Global.getWifiListCallbackList.indexOf(callback)
if
(callbackIndex > 0)
{
if
(callbackIndex > 0)
{
Global.getWifiListCallbackList.splice(callbackIndex, 1);
Global.getWifiListCallbackList.splice(callbackIndex, 1);
}
}
}
}
export function connectWifi(option: WifiOption) {
}
/**
/**
* 开启wifi
* 开启wifi
*/
*/
export function startWifi(option:
WifiOption) {
export function startWifi(option:
WifiOption) {
// 需要先开启wifi,才能使用后续的功能
// 需要先开启wifi,才能使用后续的功能
let requestCode = 1001;
let requestCode = 1001;
let permissionWifi = arrayOf("android.permission.ACCESS_FINE_LOCATION");
let permissionWifi = arrayOf("android.permission.ACCESS_FINE_LOCATION");
// 检查权限
// 检查权限
if (ActivityCompat.checkSelfPermission(getUniActivity()!, Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.checkSelfPermission(getUniActivity()!, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(getUniActivity()!, permissionWifi, requestCode)
ActivityCompat.requestPermissions(getUniActivity()!, permissionWifi, requestCode)
// 尚不具备权限,返回错误
// 尚不具备权限,返回错误
var result = {
var result = {
errNo:0,
errNo:
0,
errCode:0,
errCode:
0,
errMsg:"startWifi:premission loss"
errMsg:
"startWifi:premission loss"
}
}
option.fail?.(result)
option.fail?.(result)
option.complete?.(result)
option.complete?.(result)
return;
return;
}
}
// 具备了权限,继续前进
// 具备了权限,继续前进
let wifiManager: WifiManager =
let wifiManager: WifiManager =
getAppContext()!.getSystemService(Context.WIFI_SERVICE) as WifiManager
getAppContext()!.getSystemService(Context.WIFI_SERVICE) as WifiManager
if (Global.mReceiver == null) {
if (Global.mReceiver == null) {
Global.mReceiver = new CustomBroadcastReceiver(wifiManager)
Global.mReceiver = new CustomBroadcastReceiver(wifiManager)
}
}
let filter = new IntentFilter()
let filter = new IntentFilter()
filter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)
filter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)
filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION)
filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION)
filter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
filter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
getUniActivity()!.registerReceiver(Global.mReceiver, filter)
getUniActivity()!.registerReceiver(Global.mReceiver, filter)
wifiManager.startScan()
wifiManager.startScan()
var result = {
var result = {
errNo:0,
errNo:
0,
errCode:0,
errCode:
0,
errMsg:"startWifi:ok"
errMsg:
"startWifi:ok"
}
}
option.success?.(result)
option.success?.(result)
option.complete?.(result)
option.complete?.(result)
}
}
/**
/**
* 关闭wifi
* 关闭wifi
*/
*/
export function stopWifi(option:WifiOption) {
export function stopWifi(option:
WifiOption) {
// 需要先开启wifi,才能使用后续的功能
// 需要先开启wifi,才能使用后续的功能
if (Global.mReceiver == null) {
if (Global.mReceiver == null) {
var result = {
var result = {
errNo:12000,
errNo:
12000,
errCode:12000,
errCode:
12000,
errMsg:"stopWifi:not init"
errMsg:
"stopWifi:not init"
}
}
option.fail?.(result)
option.fail?.(result)
option.complete?.(result)
option.complete?.(result)
return
return
}
}
getUniActivity()!.unregisterReceiver(Global.mReceiver)
getUniActivity()!.unregisterReceiver(Global.mReceiver)
var result = {
var result = {
errNo:0,
errNo:
0,
errCode:0,
errCode:
0,
errMsg:"stopWifi:ok"
errMsg:
"stopWifi:ok"
}
}
option.success?.(result)
option.success?.(result)
option.complete?.(result)
option.complete?.(result)
}
}
/**
/**
* 获取当前连接中的wifi信息
* 获取当前连接中的wifi信息
*/
*/
export function getConnectedWifi(option: GetConnectedWifiOptions) {
export function getConnectedWifi(option: GetConnectedWifiOptions) {
console.log(option);
console.log(option);
let WifiInfo = new UniWifiInfo(null)
let WifiInfo = new UniWifiInfo(null)
let res = {
let res = {
errMsg: 'getConnectedWifi:fail. please check permission about location or enable wifi or connect wifi',
errMsg: 'getConnectedWifi:fail. please check permission about location or enable wifi or connect wifi',
errCode: -1,
errCode: -1,
wifi: WifiInfo
wifi: WifiInfo
}
}
if (ActivityCompat.checkSelfPermission(getUniActivity()!, Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.checkSelfPermission(getUniActivity()!, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
// 尚不具备权限,返回错误
// 尚不具备权限,返回错误
option.fail?.(res)
option.fail?.(res)
option.complete?.(res)
option.complete?.(res)
return;
return;
}
}
// TODO 应该try catch一下,把系统的错误码给返回来,然后和微信的错误码拉齐
// TODO 应该try catch一下,把系统的错误码给返回来,然后和微信的错误码拉齐
// 需要先校验权限,没有位置权限无法获取wifi
// 需要先校验权限,没有位置权限无法获取wifi
const context = getAppContext();
const context = getAppContext();
if (context != null) {
if (context != null) {
const wm = context.getSystemService(
const wm = context.getSystemService(
Context.WIFI_SERVICE
Context.WIFI_SERVICE
) as WifiManager;
) as WifiManager;
// 测试android 12上可以使用
// 测试android 12上可以使用
const winfo = wm.getConnectionInfo();
const winfo = wm.getConnectionInfo();
// 封装成数据对象
// 封装成数据对象
WifiInfo.wrapConnectInfo(winfo);
WifiInfo.wrapConnectInfo(winfo);
res.errCode = 0
res.errCode = 0
res.errMsg = "getConnectedWifi:ok"
res.errMsg = "getConnectedWifi:ok"
res.wifi = WifiInfo;
res.wifi = WifiInfo;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录