Android: wifi p2p disabling event (not disabled) - android

I register a receiver for the action
WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION
In onReceive() I get this action and compare the EXTRA_WIFI_STATE to this two cases
case WifiP2pManager.WIFI_P2P_STATE_ENABLED:
case WifiP2pManager.WIFI_P2P_STATE_DISABLED:
However I need to know when Wifi is going to be disabled but not disabled yet, in order to call some methods of the WifiP2pManager. I have to call
manager.clearLocalServices
before wifi is disabled, otherwise it fails and the service is not removed.
How can I get the "disabling" event?

I think there is a misunderstanding here. Most apps that use Wifi Direct search for peers and maintain a list of available peers.
However, you will often find that implementations do not remove peers when they are no longer available. When WifiP2pManager.WIFI_P2P_STATE_DISABLED, then by definition, the device is no longer available to other peers. So any peers that think that the device is still available probably just haven't updated the list of "available" peers, that is, they haven't checked which devices are still available.
To verify what the behavior truly is, you should try searching for the device AFTER the state has changed to WIFI_P2P_STATE_DISABLED. If peers are still able to find your device after this event, then there is a fundamental bug in the Android implementation, which I doubt.

Related

How to clear cache of discovered peers

In my Android application, I am using the WifiP2pManager API to discover peers with Wifi Direct. It works, and it discovers them as expected after calling
wifiP2pManager.DiscoverPeers(myChannel, myActionListener);
But sometimes, if I turn off a device after it has already been discovered, it still appears in the WifiP2pDeviceList when I discover peers again. I actually have to turn the wifi off and on from the device (that's discovering peers) so that it doesn't show the device (that's off) anymore. Is there a chache that I can clear instead?
I'm developing this app in Xamarin.Android, but an answer in Java/Kotlin would be fine.
EDIT
Just realized that if I wait for a minute, the turned off device doesn't appear in the list of discovered peers anymore. Still would like to know if there's a way to clear the cache sooner.
The device does not go away until "discoverPeers()" is called again by the system, which is currently set (in android framework) to be called after 60 sec.
This is normal behavior, probably nothing much you can do about it.
An answer to explain the behaviour: https://stackoverflow.com/a/25154034/3260008

Usage of createBond() with Bluetooth Low Energy

I am creating an app that communicates through Bluetooth Low Energy with a device, mostly to have the phone display the data from the device. I need to implement functionality where if the device and phone are moved into range, they will automatically identify as a pair and will connect without user interaction. I have been looking into Bonding to accomplish this.
I am currently trying to take the currently connected device and create a bond with it when the user clicks a ListItem. I call BluetoothDevice.createBond() on a reference to the connected BluetoothDevice variable. The Android Developer site says that for BluetoothDevice.createBond(), false will be returned if there is an error, and that I should register for ACTION_BOND_STATE_CHANGED to be informed when Bonding has started and is completed.
I have a few problems with createBond(), which is where my questions lie:
Most of the time createBond() returns false, indicating that it has failed to start bonding. What are some possible reasons for this? (My phone runs Android 4.4.2, API19 which is where createBond() is introduced - so it isn't that.)
Sometimes, bonding apparently succeeds because the code inside the ACTION_BOND_STATE_CHANGED receiver will be called. I may misunderstand what is involved in maintaining and using a bond, but the BLE device does not automatically connect to the phone when it is in range, so I am probably missing some code. After establishing a bond, what else do I need to do?
In the event that createBond() is no good for whatever reason (e.g. I have to make the app work for API18 when createBond() is introduced in API19), what alternatives are there?
Examples would be incredibly helpful. Finding information about bonding with BLE has been frustrating. Self-teaching this stuff has been difficult, especially with no tutorials lying around. On top of that, some people say it's impossible due to being "Just Works" when others say it's certainly possible. BLE is still a big cloud of confusion for me, so for now, some clarity with createBond() would be much appreciated.

Why do WiFi Direct peers vanish

The question is based on an observation I made while playing around with the WiFi Direct Demo Application rolled out by google.
Why does the shown list of Available peers tend to shorten even when the WifiDirect enabled devices are in close range? However, if I manually 'search' for available peers again, the list of peers grows. Well the latter seems a normal behavior, whereby mManager.discoverPeers() is called first which subsequently invokes a call to the callback function onPeersAvailable() to give a new set of peers if changed.
But why would onPeersAvailable() get called when the phones are just idle but still nearby and thus shorten the list of peers?
Thanks

Android Wifi Direct: Avoiding user acceptance when connecting to a known peer

While connecting to a peer using WiFi Direct, user's permission is required. I know that connection can't be made without user never giving the permission and there's a feature request for this kind of functionality:
https://code.google.com/p/android/issues/detail?id=30880
But I don't need this feature, asking for permission on the first connection is OK (also, more secure). Is there any method such that the permission is asked for the first time only? And when trying to connect to same peer later, the permission is not required.
From the P2P specification we can find something called "persistent P2P group" which is intended to re-establish a group without user-intervention.
In a persitent P2P group the Group Owner stores the credentials of the group so that the group can be re-established in the future by the request of any group member without the need of user intervention.
Such a group can be formed by specifying persistent while connecting using command line (see the P2P of wpa_supplicant and search for p2p_connect command https://android.googlesource.com/platform/external/wpa_supplicant_8/+/ics-plus-aosp/wpa_supplicant/README-P2P). But I don't know if the option is available with the current devices.
Edit:
See the answer to the question Can I change the group owner in a persistent group in Wi-Fi Direct?
Since I worked with Wi-Fi Direct on Ubuntu, I used wpa_cli (which comes along with wpa_supplicant) to create a persistent group as specified in the p2p_readme file.
The closest thing to have Wi-Fi Direct features without prompt I've managed is to use Service Discovery combined with legacy mode, i.e. when the connection is supposed to happen, you don't use WifiP2pManager.connect() but WifiManager.enableNetwork() instead.
I'm the author of a very simple P2P stack which automates all the magic, you can check out an example on https://github.com/croconaut/wifon-mini to see how well/bad it works (plus some documentation).
Unfortunately, not at this time. Perhaps in the future, or if you write your own ROM.
I am using two HTC One V's as well as an HTC One X+. I've been running the WifiDirectDemo given by Android. Apart from switching Wifi Direct on, no other setup is required. I simply press a search button in the application and peers are listed.
I did at first think I needed another devices permission as you've described, needing to reenable it every time I installed the application on my devices. But this was before I noticed the search button in the application, and realised this was not required.
Could you have made the same mistake as me?
Its possible through the reflection method.

Detect if data connection has been disabled

I am struggling with an issue relating to the licensing service and network connection.
The bottom line is I want the LVL to work if the user is in the middle of nowhere with no signal, but I don't want them to be able to 'cheat' and turn off the data connection.
I already have detected and intercepted airplane mode, but some phones or apps let you turn data connection on or off. (some don't)
My Epic 4g allows this, and I think the hidden API letting us control this is not working past 2.0 release. (iTelephony)
However is it possible to simply detect if the connection was disabled and not change that fact but simply react to it?
I can already detect whether or not there is a data connection or not- what I want is to tell the difference between a disabled antenna and simply not having signal. The market licensing check will fail and I want to give 'em a pass if they legitimately have no signal, but not if they forced it off.
Well, I don't need to check whether net has been disabled outside of airplane mode,
I found a different solution here:
http://code.google.com/p/androidbest/
What this code does is force the user to authenticate at least once. Then if it fails to connect to the LVL server, it uses a cached authentication. I had to change it a bit, notably it was launching the next activity twice, and now it solves my issue adequately.

Categories

Resources