How to find Wifip2p groups in android - android

I'm trying to use WiFi-Direct for connecting multiple devices over wifi in a master-slave style (one to many)- one client creates a group using the "createGroup" function, and all the other clients should connect to the group (manually).
when a client press on a "Discover peers" button, i want to give him a list of all the master peers.
And here is the problem- I can't find a way to differentiate between slave peers and the master peer (the one who initiate the createGroup request).
is there any way to filter out all the slave peers and keep only the master peers?

You should be discovering services rather than peers, though the API does work better if you also do peer discovery, thus here's my proposal for your logic:
With slave:
Start peerDiscovery
When you get Peers changed event, start service discovery (for service_type defined by your master)
Add the discovered services into a selection list as they come (note that they come one by one, and I've seen max 5 seconds between discovered services)
With Master
createGroup
Add local service to advertise that you are the master
Start Peer discovery, and make sure by monitoring the Discovery state changes that it stays on (if it goes off, your service advertisement likely will not be seen by the slaves)

A simple way to achieve this is to do the following: You can set which device to be Group Owner (Master device) by setting the groupOwnerIntent to 15.
WifiP2pConfig config = new WifiP2pConfig();
config.groupOwnerIntent = 15; //Value between 0-15
You also need to change the master's device name to something like "Master"+itsCurrentName. (To change the WiFi Direct device name, check my answer on how to set interface device name of wifi direct)
Now, whenever a new device scans for peers, the results will show which devices are GroupOwners from their name that starts with the word "Master".
This is a simple way to filter out master from slave peers.

Related

TXT Record from a Wifi Direct Printer

I'm trying to get TXT Records from Wifi Direct Printers nearby. So far, I can discover peers and establish a connection. But no luck with TXT Records.
I tried Wifi Direct Service Discovery, and I believe I did everything properly since I compared lots of codes from different resources including sample projects from Google and some open source Wifi Direct Wrappers in GitHub. But I couldn't make it work.
I've also seen some weird issues while trying to achieve that. e.g in some devices, when I start the peer discovery, Wifi Connection started to be turned off and on constantly.
Can someone explain how this actually works ? are DnsSdServiceResponseListener and DnsSdTxtRecordListener made for Android devices rather than Printers ?
I've also tried to listen the MultiCast DNS IP Address (224.0.0.251) with a MulticastSocket after establishing the connection between Android and Wifi Direct Printer, but I couldn't receive any data as well.
Thanks.
I used "DnsSdServiceResponseListener" and "DnsSdTxtRecordListener" successfully in my current project. Both listeners are associated to discovering local services nearby.
To use them properly, you have to do the following:
Implement your listeners
WifiP2pManager.DnsSdServiceResponseListener dnsListener = new WifiP2pManager.DnsSdServiceResponseListener() {
#Override
public void onDnsSdServiceAvailable(String instanceName, String registrationType, WifiP2pDevice device) {
// instanceName is service name and device is the print information
}};
WifiP2pManager.DnsSdTxtRecordListener txtListener = new WifiP2pManager.DnsSdTxtRecordListener() {
#Override
public void onDnsSdTxtRecordAvailable(String fullDomain, Map record, WifiP2pDevice device) {
// here we get the service published information in the record map object
}};
Add the listeners to your WiFiManager object
wifiP2PManagerObj.setDnsSdResponseListeners(mChannel, dnsListener, txtListener);
Add service request
WifiP2pDnsSdServiceRequest serviceRequest = WifiP2pDnsSdServiceRequest.newInstance();
wifiP2PManagerObj.addServiceRequest(mChannel,serviceRequest, actionListener);
Finally, discover services
wifiP2PManagerObj.discoverServices(mChannel,actionListener);
After discover services is executed successfully, the listeners should receive the nearby services information.
Hope this helps.
Goodluck.
Update
Wifi direct supported printers don't have any published services by default. To use them you have to connect to them via wifi direct and print normally as its a printer connected to your network. Note that those listeners are meant to capture published services (i.e will not capture anything for any device unless its publishing a service).
I think you will need to run Bonjour discovery once the connection is established. You can see NSD and look for "_ipp._tcp" as the service type. By the way,
for "I've also seen some weird issues while trying to achieve that. e.g in some devices, when I start the peer discovery, Wifi Connection started to be turned off and on constantly." if you're testing on a 7.1 device you might be seeing this issue, for which a patch should be coming soon.

How many devices can i connect with Wi-Fi P2P?

I need to connect 20+ android devices in a client-server network. Each client Android device will be communicating with the server Android device and vice versa. The client devices do not need to communicate with each other.
The server device would need access to internet for a brief period while connected to the clients.
My question is, can Wi-Fi P2P support that many connections reliably? And if yes, how do I go about implementing them?
Or will I have to ensure that all devices are on the same WLAN?
From experience, in a real-world deployment of an Android Wi-Fi Direct application, 20 devices should not be an issue.
Theoretically, the maximum number of devices in a Wi-Fi P2P group, where the GO is an Android device, is 254. The group owner is assigned the IP, 192.168.49.1. Clients are assigned an IP from the range, 192.168.49.2 to 192.168.49.254.
The group owner address is defined by the following in WifiP2pServiceImpl.java:
/* Is chosen as a unique address to avoid conflict with
the ranges defined in Tethering.java */
private static final String SERVER_ADDRESS = "192.168.49.1";
Determining the range for the clients is done as follows:
In WifiP2pServiceImpl.java, the startDhcpServer(String intf) method will start the DHCP server for a given interface - not a surprise. This method is called when the group has started and the device is the group owner.
Taking a closer look at this code, we can see that on the InterfaceConfiguration object, the link address is set to 192.168.49.1 and the prefix length is 24 (prefix length is the number of bits set in a subnet mask, here equivalent to 255.255.255.0) - this implies the answer, but we can dig a little further.
ifcg = mNwService.getInterfaceConfig(intf);
ifcg.setLinkAddress(new LinkAddress(NetworkUtils.numericToInetAddress(
SERVER_ADDRESS), 24));
ifcg.setInterfaceUp();
mNwService.setInterfaceConfig(intf, ifcg);
Next, the following commands will restart tethering with the DHCP range specified by the String[], tetheringDhcpRanges. The calls of mNwService (Network Management Service) methods will execute the appropriate netd commands.
ConnectivityManager cm = (ConnectivityManager) mContext.getSystemService(
Context.CONNECTIVITY_SERVICE);
String[] tetheringDhcpRanges = cm.getTetheredDhcpRanges();
if (mNwService.isTetheringStarted()) {
if (DBG) logd("Stop existing tethering and restart it");
mNwService.stopTethering();
}
mNwService.tetherInterface(intf);
mNwService.startTethering(tetheringDhcpRanges);
And cm.getTetheredDhcpRanges() is ultimately a reference to the following (ConnectivityManager.getTetheredDhcpRanges() -> ConnectivityService.getTetheredDhcpRanges() -> Tethering.getTetheredDhcpRanges()):
// USB is 192.168.42.1 and 255.255.255.0
// Wifi is 192.168.43.1 and 255.255.255.0
// BT is limited to max default of 5 connections. 192.168.44.1 to 192.168.48.1
// with 255.255.255.0
// P2P is 192.168.49.1 and 255.255.255.0
private String[] mDhcpRange;
private static final String[] DHCP_DEFAULT_RANGE = {
"192.168.42.2", "192.168.42.254", "192.168.43.2", "192.168.43.254",
"192.168.44.2", "192.168.44.254", "192.168.45.2", "192.168.45.254",
"192.168.46.2", "192.168.46.254", "192.168.47.2", "192.168.47.254",
"192.168.48.2", "192.168.48.254", "192.168.49.2", "192.168.49.254",
}
and:
mDhcpRange = context.getResources().getStringArray(
com.android.internal.R.array.config_tether_dhcp_range);
if ((mDhcpRange.length == 0) || (mDhcpRange.length % 2 ==1)) {
mDhcpRange = DHCP_DEFAULT_RANGE;
}
in com.android.server.connectivity.Tethering.
Of course, it is possible for the device manufacturer to change this code, so this is also worth considering.
For those planning to deploy applications where there will be many users, a mechanism to allow a more than one device to be GO is required. If data needs to be synchronised between devices, it is simple to simulate "churn" and have GOs only be a GO for a time period before becoming a client to another GO and synchronising any data.
The max number as far as I know is not specified, so you would need to test that out to be certain. Also there could be differences between hardware.
Anyway, the basic implementation would be rather simple. The server would call GreateGroup, so it would be the Groupowner in all cases. And then start locals service advertising. Clients then would simply look for the advertisement and once they see it, they would start connection process to the server. One the server connection is made over Wifi direct you would simply start socket communications from the client to the server (server would have listening socket on all times).
Note that connection would require user to click on the dialog showed when client tries to connect to the group owner. And if you want to get rid of this. Then you could actually use the Accesspoint created by GreateGroup, and add the access point name as well as the password to the advertising. Then your clients could actually use the accesspoint to connect (like to any Wlan accesspoint)
Note though that the Wifi Direct way, would not interfere with Wifi connections, not would it require it. But the accesspoint way would mean that any existing Wifi connection from the client would be disconnected, and the device thinks that the connection made to the server would provide normal internet connectivity.
Remember that devices don't need to be connected to a network to connect to each other. Wi-Fi Direct allows them to connect directly.
Here is a list of Wi-Fi Direct resources that you may find useful: https://groups.google.com/forum/#!topic/wi-fi-direct/uWpuOzHY6y0
I'd recommend following Android's Service Discovery Demo and try implementing it yourself. And here is the source code for the demo.

Android- wifiDirect

I am developing a simple wifiDirect chat app.My app does peer searching and once peer's list is available one can select a particular device, the one to connect with. Then my code calls connect() which sends a notification to the other device (2nd phone) about connection (default). Once the second phone agrees to the connection then 1st phone becomes group owner while second phone running my app does nothing.
So my question is do I need to call connect() on both devices? Also when is this intent action WIFI_P2P_THIS_DEVICE_CHANGED_ACTION broadcast?
Need help, read android.developers.com and other official sites but couldn't figure out.
No, you only need to call connect() on one device. Note that this does not mean that the device that calls connect() will be the group owner. The system will decide the group owner itself. You can only suggest your preference through config.groupOwnerIntent = x; where x is from 0 to 15, 0 representing least intention to become owner while 15 represents the highest.
WIFI_P2P_THIS_DEVICE_CHANGED_ACTION event will be fired when the status of the device has changed.
Use the following code to fetch the updated details of the device.
(WifiP2pDevice)intent.getParcelableExtra(WifiP2pManager.EXTRA_WIFI_P2P_DEVICE));

How to create an autonomous GO for Wifi Direct in Android with dedicated ssid and passphrase?

I am trying to create a WI-FI Direct network with say 3 tablet PCs. I want to run WiFi-Direct as background service, with one device set as autonomous GO. Could someone please tell me how can this be done in Android? Also someone please tell me how we can set dedicated SSID and passphrase so that any time new devices are added to this network, they can search for a specific ssid and passphrase for connection establishment during the application initiation ?
I am using Android API Level 18 for my development ...
Thanks in advance ...
This is how an autonomous Group Owner is created i.e. using the following code you can deliberately set a device in Wifi direct Network as a Group Owner
manager.createGroup(channel,new WifiP2pManager.ActionListener()
{
#Override
public void onSuccess()
{
Toast.makeText(WiFiDirectActivity.this, "Group Created",Toast.LENGTH_SHORT).show();
}
#Override
public void onFailure(int reason)
{}
});
You can use this code on any event like Button click etc.
Bluemoon10 was almost right. I can't comment yet because I need 50 reputation :/. The config.groupOwnerIntent ranges from 1-15 with 15 being the highest intent to be group owner.
If 2 devices try to connect with both GO intents == 15, the connect call will fail. If there is a GO intent tie lower than 15, the devices agree on a tie breaker bit and will succeed. So if you want one device to be group owner, you have to make sure that it is the only one trying to be. You can do this with Service Discovery, i.e. if there is a service running set your GO intent to 15 on the device with the service and 1 on the connecting device. Only one device needs to call connect to initiate connection.
Link to Service Discovery tutorial: http://developer.android.com/training/connect-devices-wirelessly/nsd-wifi-direct.html
To create autonomous group you can just invoke createGroup() method from your manager. In order to set ssid and passPhrase you must invoke the hidden methods of WifiP2pGroup class setPassphrase and setNetworkName.
To achieve that, you use java reflection. Try the following example.
Let us assume WifiP2pGroup group your current object.
Method
setPassPhraseMethod=group.getClass().getMethod("setPassphrase", new Class[ {String.class});
and now you invoke the method:
setPassPhraseMethod.invoke(group, "yourNewPassPrhase");
hope it is helpful.
Normally the Group Owner is decided by the WiFi Direct protocol.
However you can force it's hand by, using the config class.
final WifiP2pConfig config = new WifiP2pConfig();
config.deviceAddress=myDeviceAddr;
config.wps.setup = WpsInfo.PBC;
config.groupOwnerIntent =15;
In this case I think I'm correct in saying that 15 mean least likely to become GO, you set this to zero, if you want that device to be the GO.
Also this might be where you can set pin etc take a look at WpsInfo.
this config info in then passed into your connect call.
mManager.connect(mChannel, config, new WifiP2pManager.ActionListener().
Hope this helps.

Connecting to a specific bluetooth port on a bluetooth device using Android

Is there any way for Android to connect to a Bluetooth device using a specific port instead of using service UUID?
I know this option is available in other platforms which provide Bluetooth support (Java ME for example by specifying a "btspp://" style URL).
Thanks!
Ok, it's been a while, but I found a solution to the problem. I actually intended to give up and use UUID, but I kept getting a Service Discovery Failed (IO)exception, and when I tried to find a solution to the service discovery issue, I found the solution to my original question... Ain't life something?:)
Anyways, this is the link I stumbled upon, though you should note there is a mistake in the answer (they actually simply connected to port 1, instead of using a service UUID).
And after this short history lesson, here is the solution:
Using reflection, it is possible to create the Rfcomm socket connecting to a port number instead of UUID:
int bt_port_to_connect = 5; // just an example, could be any port number you wish
BluetoothDevice device = ... ; // get the bluetooth device (e.g., using bt discovery)
BluetoothSocket deviceSocket = null;
...
// IMPORTANT: we create a reference to the 'createInsecureRfcommSocket' method
// and not(!) to the 'createInsecureRfcommSocketToServiceRecord' (which is what the
// android SDK documentation publishes
Method m = device.getClass().getMethod("createInsecureRfcommSocket", new Class[] {int.class});
deviceSocket = (BluetoothSocket) m.invoke(device,bt_port_to_connect);
A few things to notice:
since we're using Invoke, the first parameter is the object we're invoking the method on, the second parameter of invoke is actually the first function parameter)
There is also a secure version available ('createRfcommSocket'), which accepts a bluetooth channel number as a single parameter (again, since this is invoke style, you'll need to pass the object to invoke the method on, as mentioned in -1- )
I found what appears to be a link to these functions' prototypes
Good luck to all.
Bluetooth Android connections are exclusively done via UUID. Each Bluetooth device has a UUID for every service it runs (see Bluetooth SDP).
You just give Android the UUID to watch for and, in client mode, it will find a socket to connect to automatically (including port). In server mode, it will wait for the specified device to initiate a connection using the specified UUID.
The BluetoothSocket object is also valid when connection is established (use getInput/Output Stream)
See Server Socket documentation and Client Socket documentation.
If you really want to check everything, you can see what Android decodes from the other device's SDP and the UUID you provided.
Use this tutorial to get the Bluetooth interface (very easy to do).
Then the code should look something like this:
IBluetooth ib =getIBluetooth();
Int otherDevicePort = ib.getRemoteServiceChannel(otherDeviceAddress, UUID);
I'm using bluecove which allow me to do so with the function Connector.open().
I use the following url:
btspp://" + phoneID + ":" + phonePort
N.b.: Some options can be added (e.g.: authenticate=false; or encrypt=false;).
With phoneID being the the being the Bluetooth address and phonePort the port number.
How to find the Bluetooth address?
From this link:
From the Home screen, open the app drawer, then open “Settings“.
Select “System“. (Skip this step on some models)
Scroll down to the bottom and tap “About Phone“, “About device“, or “About tablet“.
Scroll down to the bottom and tap “Status“.
Scroll down and the “Bluetooth address” will be shown in the list.
How to find the port number?
I haven't been able to find which port is supposed to be used yet...
I used 5 and it works but I need to research why and if I want to change the phone I will need to know if I also need to change the port.

Categories

Resources