Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
huangzhicong
/
SmartCanteen
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
16bc5455
authored
Jul 28, 2020
by
pye52
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
修复wifi无法链接的问题
parent
cbe693e4
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
57 additions
and
29 deletions
+57
-29
app/src/main/java/com/bgycc/smartcanteen/command/WifiConfigCommandWorker.java
+27
-15
app/src/main/java/com/bgycc/smartcanteen/utils/NetworkUtils.java
+30
-14
No files found.
app/src/main/java/com/bgycc/smartcanteen/command/WifiConfigCommandWorker.java
View file @
16bc5455
package
com
.
bgycc
.
smartcanteen
.
command
;
package
com
.
bgycc
.
smartcanteen
.
command
;
import
android.content.Context
;
import
android.content.Context
;
import
android.net.wifi.WifiConfiguration
;
import
android.net.wifi.WifiInfo
;
import
android.net.wifi.WifiInfo
;
import
android.util.Log
;
import
androidx.annotation.NonNull
;
import
androidx.annotation.NonNull
;
import
androidx.work.WorkerParameters
;
import
androidx.work.WorkerParameters
;
...
@@ -28,20 +30,29 @@ public class WifiConfigCommandWorker extends CommandWorker {
...
@@ -28,20 +30,29 @@ public class WifiConfigCommandWorker extends CommandWorker {
if
(
data
==
null
)
{
if
(
data
==
null
)
{
return
success
();
return
success
();
}
}
try
{
String
ssid
=
data
.
getSsid
();
String
identity
=
data
.
getIdentity
();
if
(
identity
==
null
)
{
identity
=
""
;
}
String
pwd
=
data
.
getPwd
();
String
type
=
data
.
getType
();
WifiConfiguration
currentConfiguration
=
NetworkUtils
.
getWifiConfiguration
(
ssid
);
if
(
currentConfiguration
!=
null
)
{
NetworkUtils
.
disconnect
(
currentConfiguration
.
networkId
);
}
if
(
NetworkUtils
.
enable
(
ssid
,
identity
,
pwd
,
type
))
{
NetworkUtils
.
reconnect
();
}
if
(!
NetworkUtils
.
isWifiEnabled
())
{
inProgress
(
"正在启动Wifi"
);
if
(!
NetworkUtils
.
setEnable
(
true
))
{
if
(!
NetworkUtils
.
setEnable
(
true
))
{
String
failedMessage
=
"无法启动Wifi"
;
String
failedMessage
=
"无法启动Wifi"
;
inProgress
(
failedMessage
);
inProgress
(
failedMessage
);
LogUtils
.
e
(
TAG
,
failedMessage
);
LogUtils
.
e
(
TAG
,
failedMessage
);
try
{
Thread
.
sleep
(
DEFAULT_DELAY
);
Thread
.
sleep
(
DEFAULT_DELAY
);
}
catch
(
Exception
ignored
)
{
}
return
success
(
failedMessage
);
return
success
(
failedMessage
);
}
}
}
// 断开当前在链接的wifi
// 断开当前在链接的wifi
WifiInfo
currentWifiInfo
=
NetworkUtils
.
getWifiInfo
();
WifiInfo
currentWifiInfo
=
NetworkUtils
.
getWifiInfo
();
...
@@ -50,27 +61,28 @@ public class WifiConfigCommandWorker extends CommandWorker {
...
@@ -50,27 +61,28 @@ public class WifiConfigCommandWorker extends CommandWorker {
LogUtils
.
d
(
TAG
,
"断开当前wifi: "
+
currentWifiInfo
.
toString
());
LogUtils
.
d
(
TAG
,
"断开当前wifi: "
+
currentWifiInfo
.
toString
());
}
}
String
ssid
=
data
.
getSsid
();
String
identity
=
data
.
getIdentity
();
String
pwd
=
data
.
getPwd
();
String
type
=
data
.
getType
();
inProgress
(
"正在配置Wifi"
);
inProgress
(
"正在配置Wifi"
);
WifiConfiguration
targetConfiguration
=
NetworkUtils
.
createWifiConfiguration
(
ssid
,
identity
,
pwd
,
type
);
NetworkUtils
.
disconnect
(
targetConfiguration
.
networkId
);
LogUtils
.
d
(
TAG
,
"开始配置wifi, ssid: "
+
ssid
+
", identity: "
+
identity
+
", pwd: "
+
pwd
+
", type: "
+
type
);
LogUtils
.
d
(
TAG
,
"开始配置wifi, ssid: "
+
ssid
+
", identity: "
+
identity
+
", pwd: "
+
pwd
+
", type: "
+
type
);
try
{
NetworkUtils
.
enable
(
ssid
,
identity
,
pwd
,
type
);
NetworkUtils
.
connect
(
ssid
,
identity
,
pwd
,
type
);
// 轮询检查wifi是否链接成功
// 轮询检查wifi是否链接成功
for
(
int
i
=
0
;
i
<
20
;
i
++)
{
for
(
int
i
=
0
;
i
<
20
;
i
++)
{
Thread
.
sleep
(
DEFAULT_DELAY
);
WifiInfo
info
=
NetworkUtils
.
getWifiInfo
();
WifiInfo
info
=
NetworkUtils
.
getWifiInfo
();
if
(
info
==
null
||
info
.
getIpAddress
()
==
0
)
{
if
(
info
==
null
)
{
continue
;
}
if
(
info
.
getIpAddress
()
==
0
)
{
inProgress
(
"当前链接wifi: "
+
info
.
getBSSID
());
continue
;
continue
;
}
}
String
message
=
"Wifi配置成功"
;
String
message
=
"Wifi配置成功"
;
inProgress
(
message
);
inProgress
(
message
);
LogUtils
.
d
(
TAG
,
message
);
LogUtils
.
d
(
TAG
,
message
);
Thread
.
sleep
(
DEFAULT_DELAY
);
return
success
(
message
);
return
success
(
message
);
}
}
Log
.
d
(
TAG
,
"轮询结束,仍然未能成功链接wifi: "
+
ssid
);
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
LogUtils
.
e
(
TAG
,
"链接wifi过程出错: "
+
e
.
getMessage
(),
e
);
LogUtils
.
e
(
TAG
,
"链接wifi过程出错: "
+
e
.
getMessage
(),
e
);
return
success
(
e
.
getMessage
());
return
success
(
e
.
getMessage
());
...
...
app/src/main/java/com/bgycc/smartcanteen/utils/NetworkUtils.java
View file @
16bc5455
...
@@ -22,6 +22,7 @@ import com.blankj.utilcode.util.LogUtils;
...
@@ -22,6 +22,7 @@ import com.blankj.utilcode.util.LogUtils;
import
java.net.InetAddress
;
import
java.net.InetAddress
;
import
java.net.NetworkInterface
;
import
java.net.NetworkInterface
;
import
java.util.Enumeration
;
import
java.util.Enumeration
;
import
java.util.List
;
import
static
com
.
bgycc
.
smartcanteen
.
utils
.
SmartCanteenUtils
.
TAG
;
import
static
com
.
bgycc
.
smartcanteen
.
utils
.
SmartCanteenUtils
.
TAG
;
...
@@ -234,33 +235,48 @@ public class NetworkUtils {
...
@@ -234,33 +235,48 @@ public class NetworkUtils {
connectivityManager
.
requestNetwork
(
request
,
callback
);
connectivityManager
.
requestNetwork
(
request
,
callback
);
}
}
public
static
boolean
connect
(
String
ssid
,
String
identity
,
String
pwd
,
String
type
)
{
public
static
boolean
enable
(
String
ssid
,
String
identity
,
String
pwd
,
String
type
)
{
return
connect
(
createWifiConfiguration
(
ssid
,
identity
,
pwd
,
type
));
return
enable
(
createWifiConfiguration
(
ssid
,
identity
,
pwd
,
type
));
}
}
public
static
boolean
connect
(
String
ssid
,
String
pwd
,
String
type
)
{
public
static
boolean
enable
(
String
ssid
,
String
pwd
,
String
type
)
{
return
connect
(
createWifiConfiguration
(
ssid
,
null
,
pwd
,
type
));
return
enable
(
createWifiConfiguration
(
ssid
,
null
,
pwd
,
type
));
}
}
public
static
boolean
connect
(
String
ssid
,
String
pwd
)
{
public
static
boolean
enable
(
String
ssid
,
String
pwd
)
{
return
connect
(
createWifiConfiguration
(
ssid
,
null
,
pwd
,
null
));
return
enable
(
createWifiConfiguration
(
ssid
,
null
,
pwd
,
null
));
}
public
static
boolean
reconnect
()
{
return
wifiManager
.
reconnect
();
}
}
@SuppressLint
(
"MissingPermission"
)
@SuppressLint
(
"MissingPermission"
)
public
static
boolean
connect
(
WifiConfiguration
config
)
{
public
static
boolean
enable
(
WifiConfiguration
config
)
{
wifiManager
.
addNetwork
(
config
);
int
netId
=
wifiManager
.
addNetwork
(
config
);
if
(!
wifiManager
.
enableNetwork
(
config
.
networkId
,
true
))
{
return
wifiManager
.
enableNetwork
(
netId
,
true
);
return
false
;
}
}
wifiManager
.
reconnect
();
return
false
;
@SuppressLint
(
"MissingPermission"
)
public
static
WifiConfiguration
getWifiConfiguration
(
String
ssid
)
{
try
{
List
<
WifiConfiguration
>
existingConfigs
=
wifiManager
.
getConfiguredNetworks
();
for
(
WifiConfiguration
existingConfig
:
existingConfigs
)
{
if
(
existingConfig
.
SSID
.
equals
(
"\""
+
ssid
+
"\""
))
{
return
existingConfig
;
}
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
return
null
;
}
}
public
static
boolean
disconnect
(
int
netId
)
{
public
static
boolean
disconnect
(
int
netId
)
{
return
wifiManager
.
removeNetwork
(
netId
);
return
wifiManager
.
removeNetwork
(
netId
);
}
}
p
rivate
static
WifiConfiguration
createWifiConfiguration
(
String
ssid
,
String
identity
,
String
pwd
,
String
type
)
{
p
ublic
static
WifiConfiguration
createWifiConfiguration
(
String
ssid
,
String
identity
,
String
pwd
,
String
type
)
{
if
(
ssid
==
null
||
ssid
.
isEmpty
())
if
(
ssid
==
null
||
ssid
.
isEmpty
())
return
null
;
return
null
;
if
(
pwd
!=
null
&&
!
pwd
.
isEmpty
())
{
if
(
pwd
!=
null
&&
!
pwd
.
isEmpty
())
{
...
@@ -278,7 +294,7 @@ public class NetworkUtils {
...
@@ -278,7 +294,7 @@ public class NetworkUtils {
config
.
allowedProtocols
.
clear
();
config
.
allowedProtocols
.
clear
();
config
.
SSID
=
"\""
+
ssid
+
"\""
;
config
.
SSID
=
"\""
+
ssid
+
"\""
;
if
(
type
==
null
)
{
if
(
type
==
null
||
type
.
isEmpty
()
)
{
config
.
allowedKeyManagement
.
set
(
WifiConfiguration
.
KeyMgmt
.
NONE
);
config
.
allowedKeyManagement
.
set
(
WifiConfiguration
.
KeyMgmt
.
NONE
);
}
else
if
(
"wep"
.
compareToIgnoreCase
(
type
)
==
0
)
{
}
else
if
(
"wep"
.
compareToIgnoreCase
(
type
)
==
0
)
{
config
.
hiddenSSID
=
true
;
config
.
hiddenSSID
=
true
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment