除了文件共享之外,WiFi Direct 可以进行设备到设备的通信吗? [英] Can WiFi Direct do device to device communication other than file sharing?
问题描述
我正在三星 Galaxy S3 智能手机和 Galaxy 平板电脑上使用 WiFi Direct.我可以使用 WiFi Direct 在两者之间共享图片和文件,无论是通过 Android 内置的共享选项还是一些第三方应用程序.但我真正感兴趣的是在两者之间以编程方式进行一些实时通信.
I am playing with WiFi Direct on a Samsung Galaxy S3 smartphone and a Galaxy tablet. I can share pictures and files between the two using WiFi Direct, either from the Android built-in Share option or some third party apps. But what I am really interested is to do some real time communication programmatically between the two.
Android 应用程序是否可以执行任何 类型的普通 WiFi一旦我有可用的 WiFi Direct 连接,就进行通信?如果是,有关如何使用 WiFi Direct 建立正常连接(例如套接字连接)的任何参考资料(最好是一些示例代码)?我想了解底层过程,例如如何从应用程序内设置 WiFi Direct 连接或如何获取对等设备 IP.
Is it true that an Android app can do any kind of normal WiFi communication once I have a working WiFi Direct connection? If yes, any references(ideally some example code) on how to set up a normal connection (say a socket connection) using WiFi Direct? I want to understand the underlying process like how to set up a WiFi Direct connection from within the app or how to obtain the peer device IP.
推荐答案
是的,可以做到.您可以查看 android 开发人员站点以获取更多信息.以下是相同内容的简短摘要:
Yes, it can be done. you can check out the android developers site for more info. Here's a short synopsis of the same:
• 设置权限:
必须通过修改将以下权限添加到应用程序
The following permissions would have to be added to the app by modifying
清单文件:CHANGE_WIFI_STATE、ACCESS_WIFI_STATE、
the manifest file: CHANGE_WIFI_STATE, ACCESS_WIFI_STATE,
和互联网
• 创建广播接收器和 P2P 管理器
• Creating Broadcast Reciever and P2P Manager
这是通过实例化以下类来完成的:
This is done by instantiating the following classes:
♠ IntentFilter() :检查连接/对等点的变化.*任何
♠ IntentFilter() : To check for changes in the connection/peers. *Any
连接问题&应在此处检测对等列表中的更改
problem in connection & changes in the peer list should be detected here
♠ WifiP2pManager() : 用于管理 Wi-Fi 对等连接
♠ WifiP2pManager() : for managing Wi-Fi peer-to-peer connectivity
♠ BroadCastReciever():接收意图的代码的基类
♠ BroadCastReciever() :Base class for code that will receive intents
• 获取对等点列表:
通过调用discoverPeers()方法启动Peer发现
Initiate Peer discovery by giving a call to discoverPeers() method
实现 PeerListListener.使用 peerList.getDeviceList() 获取列表
Implement PeerListListener. Use peerList.getDeviceList() to obtain the list
同龄人;并将其传递给一个数组列表
of peers; and pass it to an arraylist
• 连接到对等点:
创建一个 WifiP2pDevice 对象,该对象将存储有关每个 p2p 的信息
Create a WifiP2pDevice objects, that will store information about each p2p
设备
实例化 WifiP2pConfig 对象;并将数据从
Instantiate WifiP2pConfig object; and copy data into it from the
wifip2p设备
可以在此处找到此过程的详细说明:http://developer.android.com/training/connect-devices-无线/wifi-direct.html
The detailed description of this process can be found out over here: http://developer.android.com/training/connect-devices-wirelessly/wifi-direct.html
我只是试图简化 &缩短它.
I've just tried to simplify & shorten it.
除此之外;您可以使用传统的套接字编程(UserDatagramSocket 或 DatagramSocket)进行通信.连接的设备之一承担组所有者的角色;并可作为主设备使用.
beyond this; you can use traditional socket programming (UserDatagramSocket or DatagramSocket) for communication. One of the connected devices assumes role of group owner; and can be used as the main device.
这篇关于除了文件共享之外,WiFi Direct 可以进行设备到设备的通信吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!