I'm working on a react-native IoT application which requires me to connect to local device over hotspot and provide it with WiFi credentials.
I'm trying it in 3 phones: Vevo1601(android 6), Asus Zenfone Z00LD(android 6.1) and Redmi Note 5 (android 8.1)
Connecting to hotspot works seamless but TCP socket throws errors 8/10 times in Vevo, 2/10 times in Asus and 5/10 times in Redmi.
I'm using react-native TCP library by peel technologies : 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 method 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);
}
}
}
I have tried everything from the android official docs to stuff on this portal but it just throwing ETIMEDOUT or Host unreachable most of the times but then sometimes it works.
I don't even have a sim card in Vevo phone and only a working over WiFi connection.