Enabling Wifi direct in Android 4.0 - android

I have bought a new android 4.0.3 tablet- Micromax Funbook P300 to learn developing android applications.
I started with Wifi Direct, so that the tablet could be used as a remote for robotic platform.
To my disappointment the stock OS doesn't provide this function in the settings menu.
Is it possible to check if we can programatically start wifi direct feature?
If not can someone direct to some tutorials which addresses this?
Thanks.

The Android SDK contains a sample application for using the DirectWiFi mode:
WiFiDirectDemo - Wi-Fi Direct Demo
The used API itself is located in android.net.wifi.p2p

WiFi Direct should be supported by Android 4.0.3 (it has been around since Android API 14, or Android 4.0). It is possible that your tablet does not support WiFi Direct due to a hardware limitation, but I doubt it. You probably aren't seeing it in the settings because there is a custom Android skin running on the tablet that prevents you from seeing it, or maybe the WiFi Direct settings interface wasn't implemented until Android 4.1 or something.
Whatever the case, you can test it easily in the code.
First, put the proper permissions in your manifest.xml
http://developer.android.com/training/connect-devices-wirelessly/wifi-direct.html#permissions
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.nsdchat"
...
<uses-permission
android:required="true"
android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission
android:required="true"
android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission
android:required="true"
android:name="android.permission.INTERNET"/>
...
Next, try initializing the Android WiFiP2pManager class to see if it is supported.
http://developer.android.com/reference/android/net/wifi/p2p/WifiP2pManager.html
public void onCreate(Bundle savedInstanceState) {
WifiP2pManager manager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
WifiP2pManager.Channel channel = manager.initialize(this, getMainLooper(), null);
}
Where I put null, you could pass a callback to check for failure. If it works, follow the rest of the Wi-Fi Direct guide to create your app.

He's not asking for a coded program example, so much as a way to programatically switch on the technology (ie one line of code). As far as I am aware there is no way to do this. It all has to be done from within your devices settings. While some devices allow Wi-Fi Direct to be switched on, other devices treat it the same as having regular Wi-Fi enabled - have you tried this? And have you checked if the P300 even supports Wi-Fi Direct?

Related

Why do I get "requesting permission to use WLAN. Allow?" prompt window in real mobile phone?

I use the Code A to set the enabled status of WiFi, it works well when I use Android Emulator with both API 26 and API 23 in Android Studio 3.1.3 , and none of prompt window display!
But I get a prompt window "An App is requesting permission to use WLAN. Allow?" in real mobile phone with Android 5.1 when I run the code A.
How can I make the prompt window not to display in real mobile phone? Thanks!
BTW,the real mobile phone is Samsung SM-J5008 with Android 5.1
Prompt Window
I have set the permissions
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
Code A
fun setWiFi(aWiFiDef: WiFiDef): Unit{
val wifiManager =mContext.applicationContext.getSystemService(WIFI_SERVICE) as WifiManager
wifiManager.isWifiEnabled=aWiFiDef.status
}
Android 5.1 does not have such runtime permission, so I assume, that this is a wrongly implemented permission request rational, which is shown even though the app does not need to. You wrote, that you tested with API level 26 and 23, which is Android 6.0 and higher, so these versions already have runtime permissions. Maybe you haven't seen this issue, because you haven't tested on devices below Android 6.0...
Please check if you somewhere call AppCompat.shouldShowRequestPermissionRationale() and if it is surrounded by SDK level checks or at least with a checkSelfPermission as described here: https://developer.android.com/training/permissions/requesting. Test your code in an emulator with Android 5.1 and Android 6.0. Then you should be able to reproduce that issue also on real phones.
You also need to add this to your manifest:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
According to the website below: *App on Marshmallow needs ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission and Location services should be enabled to get wifi scan results. Google probably sees scanning for wifi as a security concern, and wanted to add the location permissions as well.
I would recommend following the blog post below if you are having trouble after adding the ACCESS_COARSE_LOCATION permission to your manifest.
https://www.intentfilter.com/2016/08/programatically-connecting-to-wifi.html
EDIT
For the people down voting, please include a comment explaining why. Was my answer flat out wrong? Did I overlook somthing?
Some more information on google's geolocation service:
Google maintains a huge database for its geolocation service ("Access coarse location" permission). It consists of:
Wifi access point mappings: Wifi MAC address resolve to a geolocation
Cell tower mapping: Cell tower ID's resolve to a geolocation
So it is very possible that this user didn't include this permission and is getting this dialog box.

How can I check that whether tablet supports sim card or not.?

I am creating an application which sync the data of call-Logs,SMS,Calendars etc. to the webserver.
But some of tablet has not support the Sim card so I could not find call-Logs and SMS from that tablet.
So I want to disable the facility from that tablet which has not Sim card.
So how can I find that the Selected Tablet has Sim card or not..
First of all get Clear that, Not all Android tablet supports telephony manager. but some do,
Actually telephony is an "umbrella feature", where the tablet may support some sub-features.
EDIT:
Specify a uses-feature node for every API feature used by your app. This forces you to think about what your app uses, allowing you to:
Decide which features are necessary for your app to be useful and mark those featured with the attribute required=true. This lets Market hide your app from any device that doesn’t support the hardware features your app requires.
Something like,
<uses-feature android:name="android.hardware.telephony"
android:required="true"/>
For more info look at this Android - blog and Here
TelephonyManager telephonyManager1 = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
if(telephonyManager1.getPhoneType()==TelephonyManager.PHONE_TYPE_NONE)
{
//coming here if Tablet
}
else{
//coming here if phone
}

How to detect MTP/PTP device in Honeycomb?

I want to detect the MTP/PTP device when it plugs in the android device. I know there is one method using Intent: android.hardware.usb.action.USB_DEVICE_ATTACHED and then defining meta-data which using resource like <usb-device class="6" subclass="1" protocol="1">, but it doesn't work. The system doesn't send this intent to the registered application.
Thus, does anyone know how to do it?
Have you set the machine to act as USB host by adding into
AndroidManifest.xml:
<uses-feature android:name="android.hardware.usb.host" />
could you please specify what is the device you are using for testing?
It is very likely that your device has no PTP/MTP specific values in class, subclass and protocol for the USB device descriptor and/or interface descriptor.
See this: http://events.linuxfoundation.org/sites/events/files/slides/Media%20Transfer%20Protocol.pdf
Some devices advertise themselves as Mass Storage devices or Vendor Specific.
If that is the case the only way is to try matching the vendor id and product id of the devices you would like to support.
Here is also a list of MTP devices with their vendor id and product id: http://sourceforge.net/p/libmtp/code/ci/HEAD/tree/src/music-players.h.
Hope this helps.

Alternative to enabling data connection

On android 2.3 the permission <uses-permission android:name="android.permission.MODIFY_PHONE_STATE" /> is not allowed anymore for non-system applications. Is there an alternative to enable/disable data connection, besides that one where you modify APN name? I found Data Enabler Widget on Android Market that does that, but I can't seem to understand how. Can anyone help me?
Thanks! - Alex Ady
I don't know how to change data connection (3g, 2g, etc), but you can enable/disable wifi connection through this:
WifiManager wifiManager = (WifiManager)this.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(boolean enabled);
I found a solution to my problem, so I'm closing this question. The alternative is to simply display the mobile settings activity if a level 10 API or higher is detected, or continue with direct enable from code otherwise. I keep the android.permission.MODIFY_PHONE_STATE, but only use it the API is under level 10.

How to receive Multicast packets on Android

I'm trying to receive data from a multicast address, but the call to MulticastSocket.receive() blocks until a timeout takes place.
I did some network sniffing and found out that my device (and the emulator) never send a MulticastSocket.joinGroup request.
I tried running the same Java code from my PC as a standalone application and it worked well. Could it be that the Android platform blocks IGMP join requests?
Has anyone succeeded with Multicast on Android before?
My manifest file contains the following permission:
I am running my application on 2.1 (Both emulator & device).
Any ideas anyone?
Thanks,
Lukas gives the best explanation and examples that I've seen on his blog: http://codeisland.org/2012/udp-multicast-on-android
In summary:
1. You need the permissions:
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
2. You need a lock:
WifiManager wifi = (WifiManager)getSystemService(Context.WIFI_SERVICE);
if (wifi != null){
WifiManager.MulticastLock lock = wifi.createMulticastLock("mylock");
lock.acquire();
}
3. You have to have a device that supports receiving multicast. Or you can follow his work around for rooted devices.
As it seems, there is no proper multicast support in the emulator.
Here's a bug report and related thread. It is being fixed for froyo.
You need to do something like this
WifiManager wifi = (WifiManager)getSystemService( Context.WIFI_SERVICE );
if(wifi != null)
{
MulticastLock mcLock = wifi.createMulticastLock("mylock");
mcLock.acquire();
}
Reference:
http://developer.android.com/reference/android/net/wifi/WifiManager.MulticastLock.html
I read all 2.1 devices not supporting IGMP stack.
IGMP was missing on different HTC, Samsung, and Motorola devices of all android version from 2.1 up to 3.2.
Link in which i read http://www.programmingmobile.com/2012/01/multicast-and-android-big-headache.html

Categories

Resources