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
0b3cabc6
authored
Mar 08, 2020
by
pye52
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
优化Wifi配置流程
parent
fe388104
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
21 additions
and
42 deletions
+21
-42
app/src/main/java/com/bgycc/smartcanteen/command/WifiConfigCommandHandler.java
+21
-42
No files found.
app/src/main/java/com/bgycc/smartcanteen/command/WifiConfigCommandHandler.java
View file @
0b3cabc6
package
com
.
bgycc
.
smartcanteen
.
command
;
package
com
.
bgycc
.
smartcanteen
.
command
;
import
android.net.wifi.WifiConfiguration
;
import
android.net.wifi.WifiInfo
;
import
android.net.wifi.WifiInfo
;
import
com.bgycc.smartcanteen.entity.Command
;
import
com.bgycc.smartcanteen.entity.Command
;
import
com.bgycc.smartcanteen.entity.CommandResponse
;
import
com.bgycc.smartcanteen.entity.CommandResponse
;
import
com.bgycc.smartcanteen.entity.CommandWifiConfig
;
import
com.bgycc.smartcanteen.entity.CommandWifiConfig
;
import
com.bgycc.smartcanteen.utils.WifiHelper
;
import
com.bgycc.smartcanteen.utils.NetworkUtils
;
import
com.blankj.utilcode.util.LogUtils
;
import
com.google.gson.Gson
;
import
com.google.gson.Gson
;
import
static
com
.
bgycc
.
smartcanteen
.
utils
.
SmartCanteenUtils
.
TAG
;
public
class
WifiConfigCommandHandler
extends
CommandHandler
{
public
class
WifiConfigCommandHandler
extends
CommandHandler
{
private
static
final
long
DEFAULT_DELAY
=
3000
;
private
static
final
long
DEFAULT_DELAY
=
3000
;
private
static
final
long
CHECK_DELAY
=
80
00
;
private
static
final
long
POLLING_DELAY
=
1
00
;
private
CommandWifiConfig
wifiConfig
;
private
CommandWifiConfig
wifiConfig
;
public
WifiConfigCommandHandler
(
Command
command
,
Gson
gson
,
CommandProgressCallback
callback
)
{
public
WifiConfigCommandHandler
(
Command
command
,
Gson
gson
,
CommandProgressCallback
callback
)
{
...
@@ -27,15 +23,9 @@ public class WifiConfigCommandHandler extends CommandHandler {
...
@@ -27,15 +23,9 @@ public class WifiConfigCommandHandler extends CommandHandler {
CommandWifiConfig
.
CommandWifiConfigData
data
=
wifiConfig
.
getData
();
CommandWifiConfig
.
CommandWifiConfigData
data
=
wifiConfig
.
getData
();
if
(
data
==
null
)
return
null
;
if
(
data
==
null
)
return
null
;
String
ssid
=
data
.
getSsid
();
if
(!
NetworkUtils
.
isWifiEnabled
())
{
String
identity
=
data
.
getIdentity
();
String
pwd
=
data
.
getPwd
();
String
type
=
data
.
getType
();
WifiHelper
.
connect
(
ssid
,
identity
,
pwd
,
type
);
if
(!
WifiHelper
.
isWifiEnabled
())
{
progress
(
"正在启动Wifi"
,
5
);
progress
(
"正在启动Wifi"
,
5
);
if
(!
WifiHelper
.
setEnable
(
true
))
{
if
(!
NetworkUtils
.
setEnable
(
true
))
{
String
failedMessage
=
"无法启动Wifi"
;
String
failedMessage
=
"无法启动Wifi"
;
progress
(
failedMessage
,
5
);
progress
(
failedMessage
,
5
);
Thread
.
sleep
(
DEFAULT_DELAY
);
Thread
.
sleep
(
DEFAULT_DELAY
);
...
@@ -43,41 +33,30 @@ public class WifiConfigCommandHandler extends CommandHandler {
...
@@ -43,41 +33,30 @@ public class WifiConfigCommandHandler extends CommandHandler {
}
}
}
}
progress
(
"正在配置Wifi"
,
10
);
String
ssid
=
data
.
getSsid
();
WifiConfiguration
config
=
WifiHelper
.
createWifiConfiguration
(
ssid
,
identity
,
pwd
,
type
);
String
identity
=
data
.
getIdentity
();
if
(
config
==
null
)
{
String
pwd
=
data
.
getPwd
();
String
failedMessage
=
"Wifi参数有误"
;
String
type
=
data
.
getType
();
progress
(
failedMessage
,
10
);
Thread
.
sleep
(
DEFAULT_DELAY
);
return
failed
(
failedMessage
);
}
progress
(
"正在配置Wifi"
,
10
);
try
{
try
{
if
(!
WifiHelper
.
removeWifiConfiguration
(
ssid
))
{
NetworkUtils
.
connect
(
ssid
,
identity
,
pwd
,
type
);
return
failed
(
"无法修改Wifi配置"
);
// 轮训检查wifi是否链接成功
}
for
(
int
i
=
0
;
i
<
30
;
i
++)
{
int
netId
=
WifiHelper
.
addNetwork
(
config
);
Thread
.
sleep
(
POLLING_DELAY
);
if
(!
WifiHelper
.
enableNetwork
(
netId
))
{
WifiInfo
info
=
NetworkUtils
.
getWifiInfo
();
return
failed
(
"无法应用Wifi配置"
);
if
(
info
==
null
||
info
.
getIpAddress
()
==
0
)
{
continue
;
}
String
message
=
"Wifi配置成功"
;
progress
(
message
,
50
);
Thread
.
sleep
(
DEFAULT_DELAY
);
return
success
(
message
);
}
}
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
Thread
.
sleep
(
DEFAULT_DELAY
);
Thread
.
sleep
(
DEFAULT_DELAY
);
return
failed
(
e
.
getMessage
());
return
failed
(
e
.
getMessage
());
}
}
progress
(
"正在连接Wifi"
,
20
);
// 等待一段时间后检查是否连接成功
Thread
.
sleep
(
CHECK_DELAY
);
WifiInfo
info
=
WifiHelper
.
getWifiInfo
();
if
(
info
!=
null
)
{
LogUtils
.
d
(
TAG
,
"当前网络: "
+
info
.
getSSID
()
+
" 目标网络: "
+
ssid
);
if
(
info
.
getIpAddress
()
!=
0
)
{
String
successMessage
=
"Wifi已成功切换"
;
progress
(
successMessage
,
50
);
Thread
.
sleep
(
DEFAULT_DELAY
);
return
success
(
successMessage
);
}
}
return
failed
(
"无法连接Wifi"
);
return
failed
(
"无法连接Wifi"
);
}
}
}
}
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