WifiP2pManager null - android

I'm getting mad, i'm following this tutorial on Wifi peer to peer :
https://developer.android.com/guide/topics/connectivity/wifip2p.html
To find the error, I tried many things in onCreate() in my activity and my last test was just that :
WifiP2pManager manager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
manager.toString();
I found out that my manager is always null. I don't understand why, my API's level is correct and I don't find any problems about WifiP2pManager and getSystemService on Internet. What I missed ?
AndroidManifest.xml
<uses-sdk android:minSdkVersion="14"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>

I had the same problem. Each time I ran the code through the emulator I received an error message that the object was null, but when I ran the code in my Nexus (real device) there was no error and the object is not null. In my opinion the reason is that the emulator does not support p2p and the real device yes. Hope I helped.

Related

onPhyUpdate is triggered with status 6 = GATT_REQUEST_NOT_SUPPORTED

I'm developing an Android app which communicates via BLE with an external module, and I am trying to set the BLE connection PHY.
I've tried calling the method gatt.setPreferredPhy(txPhy, rxPhy, phyOptions) with all the different phy type integer arguments (for both tx and rx), and added the appropriate integer argument for phyOptions in case I was requesting a coded phy.
Every time the onPhyUpdate callback is invoked with a status 6 = GATT_REQUEST_NOT_SUPPORTED.
All other GATT callbacks are invoked with a status 0 = GATT_SUCCESS.
My permissions:
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
The Android device spec, according to nrf connect:
The datasheet for the bluetooth module I'm communicating with:
https://www.renesas.com/us/en/document/dst/da14531-datasheet?language=en&r=1564826
Well, the error says that your peripheral (the Renesas) doesn't support this particular GATT request. I quickly scanned the Renesas doc and I couldn't find any confirmation that it support 2M phy....

BLE scanner on Android 12 not emit devices in Foreground service after phone reboot

I have Android 10 and Android 12 phones. BLE scanner works fine after rebooting the first one, but doesn't work on the second one.
App has all required permissions requested in runtime:
<uses-permission android:name="android.permission.BLUETOOTH" android:maxSdkVersion="30"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" android:maxSdkVersion="30"/>
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" android:maxSdkVersion="23"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
GPS and Bluetooth are enabled on the phone.
After rebooting, Foreground service is started from the broadcast receiver (Intent.ACTION_BOOT_COMPLETED).
This service then starts the BLE scanner with the scan filter:
bleAdapter.bluetoothLeScanner.startScan(filters, settings, callback)
It's successfully registered in system (BluetoothLeScanner: onScannerRegistered() - status=0 scannerId=7 mScannerId=0), but there are no devices in the scanner callback on Android 12.
What is the reason and how to solve this problem?
Your app is missing the BLUETOOTH_SCAN and BLUETOOTH_CONNECT runtime permissions for API 31 and above. The BLUETOOTH_SCAN is for accessing the ble scan results and the latter is for accessing and communicating with remote devices, hence makes use of the APIs like BluetoothDevice. You need to implement this permissions in your code where you access to these APIs. Also have a look at Target Android 12 or higher.
BLE scanner in service started after phone reboot requires android.permission.ACCESS_BACKGROUND_LOCATION permission at runtime - Allow all the time in location permission settings page (not Allow only while using the app). It's not enough to only put it in AndroidManifest.
More about permission here:
https://developer.android.com/training/location/permissions#request-background-location

Android Oreo 8.0+ Disable Network Permissions Problems

I'm building a Cordova app that needs to access WiFi on Android and disable the currently connected network in order to prevent automatic disconnection from the network i'm having it connect to (since there is no internet).
The problem is that when testing on Android Oreo 8.0+ I am getting this error in adb logcat and I can't figure out what I need to do to fix this:
E/WifiConfigManager: UID 10315 does not have permission to update configuration "Test SSID"WPA_PSK
E/WifiStateMachine: Failed to disable network
These are the perms listed in manifest:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.OVERRIDE_WIFI_CONFIG" />
<uses-permission android:name="android.permission.INTERNET" />
The OVERRIDE_WIFI_CONFIG perm I found on this post, but that doesn't seem to help in my specific situation: Changing Android hotspot settings
I found this specific error located in this file:
https://android.googlesource.com/platform/frameworks/opt/net/wifi/+/master/service/java/com/android/server/wifi/WifiConfigManager.java#984
Which calls canModifyNetwork which I found here:
https://android.googlesource.com/platform/frameworks/opt/net/wifi/+/master/service/java/com/android/server/wifi/WifiConfigManager.java#651
Can anybody more experienced with Android help me to resolve this issue, and what needs to be done in order to allow my app to disable networks?
Does this mean that apps are not allowed to disable a network if it wasn't created by the app?? Please help I don't know where to go from here!
I did find this post as well, which references 6.0, but is this true that we're basically completely locked out of disabling networks we didn't create? Android 6.0 Cannot add WifiConfiguration if there is already another WifiConfiguration for that SSID
I am not able to answer your question per se, but I can answer this
Does this mean that apps are not allowed to disable a network if it
wasn't created by the app?
That's correct, as according the documentation of the method disableNetwork:
Disable a configured network. The specified network will not be a
candidate for associating. This may result in the asynchronous
delivery of state change events. Applications are not allowed to
disable networks created by other applications.
So if the user has already connected to this network using the Android system, in Oreo you won't be able to disable the network.
Though the method disableNetwork returns true or false in the case of success or failure

How to get mobile bluetooth MAC address programmatically in oreo

I am trying to get mobile blue-tooth mac id programmatically.i saw some link and i got this coce.
String macAddress = android.provider.Settings.Secure.getString(context.getContentResolver(), "bluetooth_address");
upto nougat this code is working fine.But i am getting null in OREO(8.0.0) and i gave permission in manifest file.
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
So is there a way to get the bluetooth mac address in OREO?
The blue-tooth mac address is only available to system apps in Android Oreo. Please check this answer for details. Moreover you can also read this article for more information.

What does "mWifiServiceMessenger == null" exception in LogCat imply?

When getting a WifiManager System Service like this
WifiManager mainWifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
I have the following permissions in the Manifest file:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Eclipse LogCat logs an error
"mWifiServiceMessenger == null" [Tag: WifiManager]
Everything seems to work fine nevertheless. I do get an instance of WifiManager back. But the error is logged, also regardless of the Wifi state (enabled or disabled.
I would like to understand why this error is logged and what it does imply.
This error occurs when the device has the WiFi turned off. If the device can't get the WIFI_SERVICE.
Anyway not all the devices throw this error.
In your case it is posible that you're trying to get the WiFi service and if not you're using the cellular data.
It is thrown by WifiManager:
E/WifiManager: mWifiServiceMessenger == null
I had the same problem and I found the solution. You have to add CHANGE_WIFI_STATE permission in your manifest.
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>

Categories

Resources