我正在开发一个本机IoT应用程序,该应用程序要求我通过热点连接到本地设备并为其提供WiFi凭据。
我正在3个手机中进行尝试:Vevo1601(android 6),华硕Zenfone Z00LD(android 6.1)和Redmi Note 5(android 8.1)
无缝连接到热点,但是TCP套接字在Vevo中抛出8/10次错误,在Asus中抛出2/10次错误,在Redmi中抛出5/10次错误。
我使用的是剥离技术的本机TCP库:https://www.npmjs.com/package/react-native-tcp
let ip = "";
let port = "1884";
ip = this.generateDeviceIP(sortedWifiDevice.BSSID);
wifi.forceWifiUsage(true); //method in self created android library to bind socket to wifi network
var net = require("react-native-tcp");
let tcpClient = net.createConnection({ host: ip, port: port }
Android方法forceWifiUsage
public void forceWifiUsage(boolean useWifi) {
boolean canWriteFlag = false;
if (useWifi) {
Log.d("> Wifi Logs >> ", "usewifi is true : ");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Log.d("> Wifi Logs >> ", "build >M line 1327 ");
canWriteFlag = Settings.System.canWrite(context);
if (!canWriteFlag) {
Log.d("> Wifi Logs >> ", "build >M .... !canwrite 1331 ");
Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
intent.setData(Uri.parse("package:" + context.getPackageName()));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
}
if (((Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) && canWriteFlag) ||
((Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) &&
!(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M))) {
Log.d("> Wifi Logs >> ", "build >M .... canwrite 1342 ");
final ConnectivityManager manager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkRequest.Builder builder;
builder = new NetworkRequest.Builder();
//set the transport type do WIFI
builder.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
manager.requestNetwork(builder.build(), new ConnectivityManager.NetworkCallback() {
@Override
public void onAvailable(Network network) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
manager.bindProcessToNetwork(network);
Log.d("> Wifi Logs >> ", "bindprocess 1356");
} else {
//This method was deprecated in API level 23
ConnectivityManager.setProcessDefaultNetwork(network);
Log.d("> Wifi Logs >> ", "setprocess default network 1360");
}
try {
} catch (Exception e) {
Log.d("> Wifi Logs >> ", "exception 1364");
e.printStackTrace();
}
manager.unregisterNetworkCallback(this);
Log.d("> Wifi Logs >> ", "unregisternetwork 1368");
}
});
}
}
} else {
Log.d("> Wifi Logs >> ", "usewifi false");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Log.d("> Wifi Logs >> ", "usewifi false..... greater than marshmallow... 1376");
ConnectivityManager manager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
manager.bindProcessToNetwork(null);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Log.d("> Wifi Logs >> ", "usewifi false..... greater than lollipop....1381");
ConnectivityManager.setProcessDefaultNetwork(null);
}
}
}
我已经尝试了从android官方文档到此门户网站上所有内容的所有操作,但是大多数情况下,它只会抛出ETIMEDOUT或Host无法访问,但有时它可以正常工作。
我什至没有Vevo手机中的SIM卡,只能通过WiFi连接工作。