如何通过Android上的Wi-Fi热点以编程方式启动本地网络连接? [英] How to start a local network connection programmatically via Wi-Fi hotspot on Android?

查看:125
本文介绍了如何通过Android上的Wi-Fi热点以编程方式启动本地网络连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Android设备上的内置Wi-Fi热点启动本地网络连接. 有没有办法做到这一点? 以及如何在两个设备之间进行通信?

I want to start a local network connection using the built-in Wi-Fi hotspot on Android devices. Is there a way to accomplish it? And how to communicate between two devices?

我想以编程方式进行操作.然后,我可以在设备之间传输自己的数据.

I want to do it programmatically. Then I can transfer my own data between devices.

推荐答案

我以前没有做过,所以我的回答全是理论上的.

I haven't done this before, so my answer is all theoretical.

我认为您需要做的是将一台设备声明为服务器,并使用已知名称手动启动其热点.

I think what you need to do is declare one device the server and start its hotspot manually with a known name.

通过代码启动/停止内置的Wi-Fi/USB绑定?(根据此问题,您可以不会以编程方式启动热点).

Start / stop built-in Wi-Fi / USB tethering from code? (According to this question, you can't start hotspot programatically).

一旦设置了热点,就可以启动服务器应用程序并等待传入​​连接.服务器应用程序可以为您显示IP地址或主机名.

Once you have the hotspot set up, you start up the server app and wait for incoming connections. The server app can show you the ip address or hostname.

在客户端上,您可以进行简单的检查,查看热点名称是否在周围,并在找到热点名称时自动将其连接.请参阅以下链接以连接到网络:

On the clients, you can do a simple check and see if the hotspot name is around and automatically connect to it if you find it. See this link for connecting to a network:

使用WifiManager连接到网络

Using WifiManager to connect to a network

关于第二个问题,即如何通信-您需要在设备之间建立连接.为此,您需要服务器的IP地址.最简单的操作是在服务器UI上显示IP地址,然后在客户端上手动输入该IP地址,然后按connect.然后它将在已知端口上建立到服务器的连接,并且您可以在客户端和服务器之间发送消息.

As for your second question, how to communicate - you need to establish a connection between the devices. For this, you need the ip address of the server. The simplest thing to do would be to show the ip address on the server UI and then manually enter it on the clients and press connect. It would then establish a connection to the server on a known port and you can send messages between client and server.

作为参考,我找到了 NanoHttpd ,它是Android的网络服务器.它使用 ServerSocket 侦听传入的连接.

For reference, I found NanoHttpd, which is a webserver for Android. It uses ServerSocket to listen for incoming connections.

这篇关于如何通过Android上的Wi-Fi热点以编程方式启动本地网络连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆