【问题标题】:Sending and recieving data over wifi connection in android programatically以编程方式在android中通过wifi连接发送和接收数据
【发布时间】:2015-08-26 19:32:36
【问题描述】:

我正在尝试制作一个与我的电脑中的热点通信的应用程序。我能够连接到热点,但我无法弄清楚如何在 android 中发送和接收数据。这是我现在在我的应用程序中的代码。

WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
// setup a wifi configuration
WifiConfiguration wc = new WifiConfiguration();
wc.SSID = "\"pigo\"";
wc.preSharedKey = "\"12345678\"";
wc.status = WifiConfiguration.Status.ENABLED;
wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
// connect to and enable the connection
int netId = wifiManager.addNetwork(wc);
wifiManager.enableNetwork(netId, true);
wifiManager.setWifiEnabled(true);
stepinfo.setText("Initiating Connection . . .");

我查看了其他类似的问题,但我的应用似乎没有任何效果。

【问题讨论】:

  • 电脑中的热点?请解释一下如何在电脑上制作热点?
  • 您的代码使用 wifi 管理器执行某些操作。好的。但是您到底想向什么发送数据?
  • @greenapps 在管理员模式下使用 cmd step1) netsh wlan set hostsnetwork mode=allow ssid=pigo key=12345678 step2) netsh wlan start hostingnetwork
  • 对不起,我不明白。

标签: java android android-wifi


【解决方案1】:

通过执行上述操作,您只能连接到计算机的热点(Windows PC - 假设您使用的是 netsh wlan)。这将允许您连接到您的 PC 并最大程度地共享其互联网。但要实现计算机和手机之间的通信,您需要在此层上构建一个应用程序。

这个应用程序会做什么。它将侦听您的 PC 和移动设备上的特定端口。因此,当通过热点建立连接时,您需要有一个Socket 并执行Socket.connect(new InetAddress(IP, PORT))。假设您的计算机监听PORT_0,那么您的手机需要监听Socket.connect(new InetAddress(IP_COMPUTER, PORT_0))

这样你现在就有了一个套接字,可以用来在你的电脑和你的手机之间进行通信。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-15
    • 1970-01-01
    • 2017-06-25
    • 1970-01-01
    相关资源
    最近更新 更多