I want to require entering a PIN when connecting two Android devices to ensure they are talking to each other.
There is a sample project in the Android SDK 17 called "WiFiDirectDemo". It establishes a Wi-Fi Direct connection but it lacks the PIN authentication.
I don't know how to specify WiFiP2pConfig.wps.setup in the class DeviceDetailFragment around line 71. Do I specify the action on the current device (I want to show a auto-generated four-digit PIN), i. e. config.wps.setup = WPSInfo.DISPLAY, or should I specify the action on the peer, i. e. config.wps.setup = WPSInfo.KEYPAD?
How can I detect the connection request on the other peer? I guess there is something I can listen to, e. g. in the BroadcastReceiver, but I cannot find it.
I'm an experienced iOS developer but Android is very new to me, maybe the the answer is obvious to you. Thanks for your help.
you need to specify the action on the device which sends the connect request ... and for pin i think this should work
p2pconfig.wps.setup = WpsInfo.DISPLAY;
p2pconfig.wps.pin = "0000";
On connect, PIN is displayed on local device and requests user to enter same PIN on remote device; once PIN is entered, connect completes successfully.
Check this https://groups.google.com/forum/#!topic/android-platform/hN5WfXRzXpI read the 5th post
p2pconfig.wps.setup=WpsInfo.KEYPAD(don't specify p2pconfig.wps.pin) works fine for me.It displays a dynamically generated key on the device from which we are trying to connect to be entered on the other device.
In this case you don't need to listen on any b'cast receiver to detect the connection.The wpa_supplicant takes care of it and prompts you to enter the pin.For a clear picture, look at the logcat(I use adb from ADT(sdk+eclipse) to see the logcat) of wpa_supplicant while connection is in progress.
Related
I am tring to setup WIFI P2P on 2 devices using
manager.addLocalService(channel, service, ActionListener)
and then connect both devices using
manager.connect(channel, config, ActionListener).
I would like to know which method is called before the popup to accept/reject connection is shown on the target device. All I was able to find was onConnectionInfoAvailable(WifiP2pInfo p2pInfo), but it is called after the connection is established.
I basically want to receive the "instance name" of the device trying to connect to me using WIFI P2P and then reject the connection request without showing system dialog(that allows the user to accept/reject connection).
I can't anything that can help me do this on docs or any other place. If anyone knows how to do it or can point me in the right direction then please let me know.
I solved it. I can put the instancename and devicename (of device I want to connect to) in Map that is passed when setting up service. From other device I can retrieve map of all devices available using this and find the instancename of one I need.
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));
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.
im testing the new technology wifi direct and im having some issues using
the wifi direct demo from the samples that come with the android-sdk.
So, I have two devices A and B, both with android 4.0.3.
First, from device A, I send a file to B. Nothing wrong here, B
receives the file.
Then A disconnects from B.
Now, from device B I try to send a file to A.
But the device that receives the file is B, instead of A.
To fix, i need to turn off and on both devices...
Also, sometimes when i click disconnect and try to
connect again, connection fails and i have to disable and
enable wifi direct...
Anyone else experiencing this?
Is it because the new technology is not mature yet or maybe
something wrong with my build/driver/etc or maybe this demoapp
doesnt support two-way sharing.
Any ideas and/or explanations would be apreciated.
When providing a WifiP2pConfig instance to the connect() function, you can set the groupOwnerIntent property of this configuration object as follows:
WifiP2pConfig config = new WifiP2pConfig();
config.deviceAddress = "..."; // insert ip here
config.groupOwnerIntent = 0;
config.wps.setup = WpsInfo.PBC;
manager.connect(..., config, ...);
From the android reference:
This (the groupOwnerIntent) is an integer value between 0 and 15 where
0 indicates the least inclination to be a group owner and 15 indicates
the highest inclination to be a group owner.
Furthermore, the demo probably repeatedly sends the file to the same device because there is always made a socket connection to the ip-address obtained from:
WifiP2pInfo.groupOwnerAddress
If you would like to support bidirectional communication, the first step in setting this up would be sending the ip-address of the non group owner to the group owner.
As far as the disconnect/reconnect problem goes, I seem to have the same inconsistencies with Android 4.0.2 devices.
I have been trying for a while to transfer files between two devices using wifi direct. I have use the Android SDK WifiDirectDemo as base. My experience:
GO address is always the same (at least in Samsung Nexus), but this is not really a problem, because you can use this to know who is the server (or client).
Another strange thing was that MAC address of devices were different when you got it from Android WifiManager and when you read it from "/proc/net/arp" file.
At the end I did it, and you can see the code here.
I hope it helps you!
I have been struggling with the same problem lately. I suppose this is an OS issue. To give you a brief background, I have installed Wi-Fi Direct application to both devices with different OS versions, one with OS 4.0.1 and one with OS 4.0.2. The connection fails from time to time when I disconnect and reconnect the devices. It goes same while searching for devices too. But the thing is, this only happens on the device with OS 4.0.2. Other device does not crash or disconnect.
While searching for that problem, I have found the links below. People discussed about that and they share the same idea. Apparently this is an OS 4.0.2 issue. I am not sure if it is the same for OS 4.0.3 but there is no problem with the previous version OS 4.0.1 for sure.
Here are the links:
http://code.google.com/p/android/issues/detail?id=24402
http://osdir.com/ml/android-platform/2012-01/msg00226.html
I have an app where I am programmatically controlling Bluetooth pairing and unpairing. I can pair before connection and unpair afterwards. The reason I need to do this is specific to my application and not in the scope of my question.
Basically what I am doing is:
Get a reference ib to IBluetooth object as described in this answer
Register a BroadcastReceiver for android.bluetooth.device.action.PAIRING_REQUEST
Call ib.createBond(address)
Wait for BroadcastReceiver to trigger
Convert user pin into bytes with convertPinToBytes()
Call ib.setPin(address, pinBytes) from within BroadcastReceiver
Anyways, this approach works great, except for the fact that when I do the pairing, I get a notification in the Status bar requesting that the user enter a PIN to complete the pairing. But this is in fact unnecessary, because by the time the user sees this, my app has already used setPin(). I'd really like for that notification to either a) not appear at all, or b) be dismissed automatically somehow.
I realize this may not even be possible, but I thought I would ask in case someone has a creative idea.
Try setting the confirmation first in the PAIRING_REQUEST
BluetoothDevice device = intent.getParcelableExtra("android.bluetooth.device.extra.DEVICE");
device.getClass().getMethod("setPairingConfirmation", boolean.class).invoke(device, true);
device.getClass().getMethod("cancelPairingUserInput").invoke(device);
This worked for me between two Android devices using RFCOMM but I'm not entering any PINs
Since Android API 19 Google switched these Methods to public Methods, so there is no need for Reflection any more. :)
Do this in the PAIRING_REQUEST notification event:
BluetoothDevice localBluetoothDevice = (BluetoothDevice)intent.getParcelableExtra("android.bluetooth.device.extra.DEVICE");
Class localClass = localBluetoothDevice.getClass();
Class[] arrayOfClass = new Class[0];
localClass.getMethod("cancelPairingUserInput", arrayOfClass).invoke(paramBluetoothDevice, null)).booleanValue();
But you gotta tell me how did you pair your remote device without the user to enter Passkey/PIN? off course, you know the PIN for the remote device which is trying to pair to your device but how did you provide that PIN to the remote device.