I am trying to connect to my device from another device and send a simple string via wifi direct.
I was able to get the list of peers available however how do I know that this is my device that I need to send the info to?
The documention at https://developer.android.com/reference/android/net/wifi/p2p/WifiP2pManager.html outlines this scenerio.
With peer discovery using discoverPeers(WifiP2pManager.Channel, WifiP2pManager.ActionListener), an application discovers the neighboring peers, but has no good way to figure out which peer to establish a connection with. For example, if a game application is interested in finding all the neighboring peers that are also running the same game, it has no way to find out until after the connection is setup. Pre-association service discovery is meant to address this issue of filtering the peers based on the running services.
Try using addLocalService(), so you peer devices can discover your "simple string" p2p service.
Related
So there's a requirement for an app that will communicate between devices if and only if its connected to the same local network.
E.G: Device A and Device B are connected to Wi-Fi network Z. The app will allow both the Devices to communicate between eachother (send messages/videos)
I've been researching the past couple of days but unable to get clarity with what I've found so far. (I'm confused between NSDManager, WifiP2pManager, etc. as to which one suits my requirement).
I just need confirmation between which concept/methodology to follow. Please help.
From my personal experience, WiFiP2PManager should do what you want. I have used it to connect two devices over the same WiFi network. It also allows your device to view all the other devices on the network and enables you to connect to them. Once you have connected to the device, you can then send messages across the WiFi network. This documentation does a good job of explaining how to use WiFi P2P and how to get started.
It also explains how to view devices on the network, how to connect with them, and how to transfer data. This should suit the requirements that you requested.
You can use NSD, but according to the documentation that is mainly used for giving your app access to services provided from other devices on the local network. It would allow your app to do file sharing with the other device by requesting a service.
After searching it up, I have concluded that WiFi Direct is the same as WiFi P2P. They both use the WiFiP2PManager.Here is a link for information about it.
You can use Network Service Discovery to find and connect to instances of your app. You can then open a socket connection to facilitate communication.
I created and example app on Android and on iOS that does this which I link to and explain in great detail here: http://brendaninnis.ca/connect-nearby-devices-part-2.html
I have 2 devices and both support Wifi Direct. I want to turn on Wifi direct in both devices and connect to other device from 1st device by getting it's MAC address programmatically.
However, I'm able to list down nearby Wifi Direct devices in my application and make a connection using WifiP2PDevice object which Android provides in callback of peers list using WifiP2P APIs, but here my approach is to construct WifiP2PDevice object by using remote device MAC address directly and trying to connect it with no luck.
Can anyone help me in resolving this issue?
To connect to any device via wifi-direct, each device need to discover nearby peers or services before attempting to connect to make sure that wifi-direct is enabled and discoverable.
From my tests, even if you have the correct MAC address, trying to connect will not work unless peers or services discovery is executed and the device that you are looking for is discoverable / visible.
If the device is discoverable, you can create new WifiP2PDevice or reuse what's returning from the peers list and connect to it.
I hope I got your question correctly. If not, I'm happy to help if you explain further.
Goodluck.
I am able to discover local devices via WiFi P2P that advertise an Android service that I am interested in binding to. Once those services are discovered, how do I bind a client to them?
In other words, I'd like to have a client send a Message object to a bound service running on a different device, have that device handle the Message and potentially respond.
The Android documentation has a training exercise on Wifi P2P here and specifically has code to connect to a peer here
If I wanted to connect to another device via bluetooth how would I do it?
Nothing I've done seems to work. The phone I'm trying to connect to is non android and has bluetooth on and discoverable.
Bluetooth API is realy poor in this case... let's have look at java-doc:
Hint: If you are connecting to a
Bluetooth serial board then try using
the well-known SPP UUID
00001101-0000-1000-8000-00805F9B34FB.
However if you are connecting to an
Android peer then please generate your
own unique UUID.
from: link text
According to this, your peer should be visible under the 00001101-0000-1000-8000-00805F9B34FB service record.
Now, if you want to create an app which need to connect to Bluetooth devices, I advice you to use Cordova platform and install a plugin that let you connect via bluetooth, list devices, send data, disconnect etc...
In Bluetooth, there is a concept of service profile for an application layer. An application on a peer 1 willing to talk to same application on peer 2 must be discoverable and connectable, the master initiating the connection.
The application is given a unique service/application identifier so that peers can discover each other. If you write a custom application yourself, you are expected to create a new UUID so that the corresponding SDP protocol can perform the necessary discovery.
Instead of connecting with brute force using the BT_ADDR, you would essentially initiate connection via graceful service discovery and then connection follows once the underlying UUID is identified. This, of course, assumes that you would have done the pairing process early, or that your lower layer stack will include pairing process as well.
Can someone explain a couple of very simple concepts to me - I'm interested in mobile devices running android and how they are identified over networks. Some scenarios:
Device is connected over WiFi - presumably the device has a standard IP address as with any host and can communicate with any other android host over TCP/IP (assuming it knows the participating device's IP?
Device is connected over bluetooth - how are devices identified in this case?
Device is connected over mobile operator's network - this is the one I'm interested in and confused by - is there anyway for two or more devices to discover each other and communicate via the mobile operator's network? How does a device communicate with a backend server in this scenario? In other words, how do apps and devices communicate when not connected to a WiFi network?
Thanks for any advice..
I'm only sure about the bluetooth thing, so i only answer this part:
The Bluetooth interface on your device has an MAC adresse. So while communicationg over Bluetooth you can assume that this MAC adresse is a unique identifier for a specific device. You can also reach other devices by establishing a connection over this MAC adress- However, to get this mac adress in the first place, you have to know it from somwhere, or you have to search for other bluetooth devices in the reachabla area before.
WiFi and 3g both attach the [mobile] device to the internet so it can make internet connections. 3G assigns a publicly addressable IP to the device, so one could, presumably open a server socket and listen for connections. The client would have to know the mobile IP, which may change quite frequently.
Bluetooth is more geared toward close-proximity. Devices in the vicinity can be connected to, after you have paired with them, which requires the cooperation of both devices which are to communicate.
If the goal is to produce an application which connects to nearby devices, I can think of the following ideas:
3g: all devices running the client register their position with a central database server. If the server detects that two clients are in close proximity, let them know so they can connect through the internet or through the server
WiFi: you could use the same idea as 3g, or use broadcast/multicast packets to broadcast your presence. Other apps can listen for those broadcasts and discover which other devices are near.
Bluetooth: A little trickier, as a device must be placed into discoverable mode in order for others to "see it". Discoverable mode is a temporary state and only lasts about 30 seconds (at a time).