admin管理员组文章数量:1388153
The app connects to a Wi-Fi network (ELM327) to collect car data, but this network has no internet access. Need to send data via mobile network simultaneously.
Problem: On Android/iOS, when Wi-Fi is active, all traffic uses it by default, even if there’s no internet. How to force HTTP requests to use mobile data without disconnecting Wi-Fi?
The app connects to a Wi-Fi network (ELM327) to collect car data, but this network has no internet access. Need to send data via mobile network simultaneously.
Problem: On Android/iOS, when Wi-Fi is active, all traffic uses it by default, even if there’s no internet. How to force HTTP requests to use mobile data without disconnecting Wi-Fi?
Share Improve this question asked Mar 18 at 8:00 PetrPetr 2542 silver badges16 bronze badges1 Answer
Reset to default 4Android Solution
Android allows developers to direct specific traffic through the mobile network, even while connected to Wi-Fi.
Connect to the Wi-Fi network (ELM327) for collecting car data.
Use the ConnectivityManager to access the mobile network interface.
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkRequest.Builder requestBuilder = new NetworkRequest.Builder();
requestBuilder.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR);
cm.requestNetwork(requestBuilder.build(), new ConnectivityManager.NetworkCallback() {
@Override
public void onAvailable(Network network) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
cm.bindProcessToNetwork(network);
} else {
ConnectivityManager.setProcessDefaultNetwork(network);
}
// Place your HTTP request code here, it will use mobile data.
}
});
After completing the requests, you can unbind the network by calling:
cm.bindProcessToNetwork(null);
本文标签:
版权声明:本文标题:How to use mobile data for sending requests when connected to a Wi-Fi without internet (AndroidiOS)? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744516749a2610209.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论