socketException:连接失败(OS错误:网络无法实现,ERRNO = 101) - flutter(Android Platform)

发布于 2025-01-21 04:15:01 字数 2050 浏览 4 评论 0原文

我有一个需要在Android和iOS设备上运行的颤音项目。

我正在尝试编写本机Java代码,以连接到上面的Android 10上的特定Wi-Fi连接。

注意:连接是与没有Internet的设备的连接

,我遵循Android官方文档并实现这样的连接:

private void connectWiFi(String ssid, String bssid){
    final NetworkSpecifier specifier =
            new WifiNetworkSpecifier.Builder()
                    .setSsid(ssid)
                    .setBssid(MacAddress.fromString(bssid))
                    .build();

    final NetworkRequest request =
            new NetworkRequest.Builder()
                    .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
                    .removeCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
                    .setNetworkSpecifier(specifier)
                    .build();

    final ConnectivityManager connectivityManager = (ConnectivityManager)
            getApplicationContext().getSystemService(getApplicationContext().CONNECTIVITY_SERVICE);

    final ConnectivityManager.NetworkCallback networkCallback = new ConnectivityManager.NetworkCallback() {
        @Override
        public void onAvailable(@NonNull Network network) {
            super.onAvailable(network);
        }

        @Override
        public void onUnavailable() {
            super.onUnavailable();
        }
    };
    connectivityManager.requestNetwork(request, networkCallback);
}

在真实设备上运行代码后,我已经获得了“成功连接”的OS弹出窗口,

现在我正在尝试执行一个简单的使用弹力“ HTTP”软件包获取请求。

以下是我的Get请求的代码:

Response response = await get(urlStartRecord).catchError((onError) async {
  myLog("Error in _sendStartRecord urlStartRecord", "error",
      exception: "Cannot start record: ${onError.toString()}");
}

urlstartrecord 定义时:

    final urlStartRecord = Uri.parse('http://192.168.0.1:12913/startRecord');

运行Get请求后,我将收到以下错误消息:

无法启动记录:socketException:连接失败(OS错误:网络无法到达,errno = 101),地址= 192.168.0.1,端口= 12913

我更困惑的是,如果我手动连接到同一网络,它可以很好地工作。

我已经在网络上搜索了,但没有解决问题。

I have a Flutter project that needs to run on Android and IOS devices.

I am trying to write native Java code in order to connect to a specific Wi-Fi connection on Android 10 above.

Note: The connection is to a device without internet

I followed Android official documentation and implement the connection like that:

private void connectWiFi(String ssid, String bssid){
    final NetworkSpecifier specifier =
            new WifiNetworkSpecifier.Builder()
                    .setSsid(ssid)
                    .setBssid(MacAddress.fromString(bssid))
                    .build();

    final NetworkRequest request =
            new NetworkRequest.Builder()
                    .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
                    .removeCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
                    .setNetworkSpecifier(specifier)
                    .build();

    final ConnectivityManager connectivityManager = (ConnectivityManager)
            getApplicationContext().getSystemService(getApplicationContext().CONNECTIVITY_SERVICE);

    final ConnectivityManager.NetworkCallback networkCallback = new ConnectivityManager.NetworkCallback() {
        @Override
        public void onAvailable(@NonNull Network network) {
            super.onAvailable(network);
        }

        @Override
        public void onUnavailable() {
            super.onUnavailable();
        }
    };
    connectivityManager.requestNetwork(request, networkCallback);
}

After running the code on a real device, I've got a "Successfully connected" OS popup

Now I am trying to perform a simple GET request using the Flutter "http" package.

Here is my code for the GET request:

Response response = await get(urlStartRecord).catchError((onError) async {
  myLog("Error in _sendStartRecord urlStartRecord", "error",
      exception: "Cannot start record: ${onError.toString()}");
}

When urlStartRecord is defined like that:

    final urlStartRecord = Uri.parse('http://192.168.0.1:12913/startRecord');

After running the GET request I am receiving the following error message:

Cannot start record: SocketException: Connection failed (OS Error: Network is unreachable, errno = 101), address = 192.168.0.1, port = 12913

What confuses me more is that if I connected manually to the same network, it works well.

I already searched on the web but nothing solve the problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文