Following the documentation here:
http://developer.android.com/training/connect-devices-wirelessly/nsd-wifi-direct.html
It appears to be possible to broadcast data to all nearby devices that have WiFi Direct without establishing a connection by just putting the data in a WifiP2pDnsSdServiceInfo object. This works for my purposes as I'm attempting to establish a simple P2P messaging app amongst multiple nearby devices. However it seems like a misuse of the API as this is intended to carry information to setup a connection and advertise a service. My questions are:
- What are the pros and cons of sending data this way (please ignore security)?
- Will there be delays in messages being sent/received.
- What will the impact on battery life be like?
The key/value pairs you pass into
WifiP2pDnsSdServiceInfo
object are going to be encapsulated into DNS multicast messages, as specified in DNS-Based Service Discovery.Multicast traffic on WiFi is especially expensive, so it is not recommended to use this for messaging. Only for critical things like service discovery.
Depending on implementation (android), the under framework may pool all service discovery records/responses, before it calls your callbacks. This can add significant (even unpredictable) delay for your simple P2P messaging.
This is basically a DNS messaging, so your "messages" may be cached locally. DNS queries could also try mid-level optimizations like pooling, retries, query filtering, prioritization etc.
There is no reliability guarantee so your messages may be lost.
Multicast traffic, which is used here, can result in significant battery drain on android, see here
来源:https://stackoverflow.com/questions/26367191/sending-data-in-android-wifi-direct-service-discovery-instead-of-connecting