MQTT with Android - java

I'm trying to publish a message via MQTT. So I created a blank Android app and added the Paho MQTT library to it.
This is the line of code I execute first:
MqttClient client = new MqttClient("tcp://192.168.178.34", "Foo");
This throws an "MqttException" which contains no message, no error code, no nothing. So currently I have no idea what's going wrong.
I also added the following permissions to my app:
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
Can anyone give me a hint what I could be doing wrong here?

In the code, The activated class is the TCPNetworkModule class which is using connect method of the socket class directly(see the start method of the class).
I think the exception might be the NetworkOnMainThreadException
attempting to use blocking IO.
That's why we use MqttAndroidClient instead.

Related

Unable to read device phone numbers despite meeting two of the listed requirements in Android 10 and above

I am trying to use the TelephonyManager class to read the line1Number and the IMEI of the Sim card in the respective slots. I have also tried using TelecomManager class to achieve the same with zero success. I headed over to the official documentation of the c;class on Google documentation and did find out that the methods TelephonyManager.GetLine1Number() or TelephonyManager.GetImei() can only be used if one of the following conditions is met.
If the calling app has been granted the READ_PRIVILEGED_PHONE_STATE permission; this is a privileged permission that can only be granted to apps preloaded on the device.
If the calling app is the device owner of a fully-managed device, a profile owner of an organization-owned device, or their delegates (see DevicePolicyManager.getEnrollmentSpecificId()).
If the calling app has been granted the Manifest.permission#USE_ICC_AUTH_WITH_DEVICE_IDENTIFIER permission.
If the calling app is the default SMS role holder (see RoleManager.isRoleHeld(java.lang.String)).
If the calling app has been granted the READ_PRIVILEGED_PHONE_STATE permission; this is a privileged permission that can only be granted to apps preloaded on the device.
I am aware that as from Android 10 accessing device identifiers is kind of technical so because they said if one of the requirements above is met then a method like TelephonyManager.GetImei() should work when invoked. I did try to use a method from an app with device administration privileges like mentioned in the second requirement from the list above, the exception user 10711 does not meet the requirements to read device identifiers is thrown. I also tried the other requirement to declare the permission <uses-permission android:name="android.permission.USE_ICC_AUTH_WITH_DEVICE_IDENTIFIER"/> in my manifest and then tried to request for the permission at runtime from the user like below
if (ContextCompat.CheckSelfPermission(this, Manifest.Permission.UseIccAuthWithDeviceIdentifier) !=
Permission.Granted)
{
RequestPermissions(new string[]{Manifest.Permission.UseIccAuthWithDeviceIdentifier},682);
}
Meeting that permission requirement also does not make the invocation of the TelephonyManager.GetImei() work. How do I write code that will make devices running Android 10 and above return the IMEI and lineNumber without the exception does not meet the requirements to access device identifiers being thrown?
My manifest file looks like this
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:versionCode="1"
android:versionName="1.0"
package="LockScreenAPI.LockScreenAPI">
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="31" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
<uses-permission android:name="android.permission.BIND_DEVICE_ADMIN"/>
<uses-permission android:name="android.permission.PREVENT_POWER_KEY" />
<uses-permission android:name="android.permission.READ_PHONE_NUMBERS"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.USE_ICC_AUTH_WITH_DEVICE_IDENTIFIER"/>
<application android:allowBackup="true" android:label="#string/app_name" android:supportsRtl="true" android:theme="#style/AppTheme">
</application>
</manifest>
An answer in Android Java is accepted because I use it as the blueprint to implement in a Xamarin Android app, Thank You.

Allow Background Activity for this App in Foreground Service Android

I am running a foreground service in android 10 and 11. But I have issue in my Oppo android 11. Issue is that the execution of any process is stopped after some time because the Allow Background Activity is not enabled for this app in setting. If I allow it then the foreground service works fine. I don't know how to check that if the Allow background activity is enabled or not and how to move to that specific intent. I have search alot but didn't found any solution.
The picture of the app setting is here.
The picture of the app setting is here
The Android Manifest file permissions are here.
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />

Google Play rejects my RN app because of location

I've been facing an issue for a few days now. Everytime I submit my release to Google Play Store, I get a message saying my app has been rejected over and over again. I tried multiple things.
First I tried removing the permissions from my AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" package="com.lachariottenative">
<uses-permission tools:node="remove" android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission tools:node="remove" android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission tools:node="remove" android:name="android.permission.ACCESS_BACKGROUND_LOCATION"/>
</manifest>
Then I tried editing my app.json file and set permissions to an empty array but each time I get the same error when uploading my app to the Google Play Store. It says that I'm using background location although I am not.
Can you help please?
Thanks in advance
EDIT : I do not wish to use location in my app for the moment. But somehow it still asks for permissions...

Error required permissions android.permission.CONNECTIVITY_INTERNAL android.permission.MAINLINE_NETWORK_STACK

im writing a simple app that want to connect to a specific WiFi Network on Android 10.
I gave all permissions that I thought I might need in the manifest.xml.
<uses-permission-sdk-23 android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission-sdk-23 android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission-sdk-23 android:name="android.permission.INTERNET"/>
<uses-permission-sdk-23 android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission-sdk-23 android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission-sdk-23 android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission-sdk-23 android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission-sdk-23 android:name="android.permission.CONNECTIVITY_INTERNAL" />
<uses-permission-sdk-23 android:name="android.permission.MAINLINE_NETWORK_STACK" />
But when I use the fuction "connectivitymanager.requestNetwork" I get the Error:
java.lang.SecurityException: Requires one of the following permissions: android.permission.CONNECTIVITY_INTERNAL, android.permission.MAINLINE_NETWORK_STACK.
In the code where I want to request permissions this two permissions are not defined and not available for a request.
Anyone knows about this Permissions or the cause of the error?
Jürgen
I had this issue when I was calling NetworkRequest.Builder#addCapability() and passing in a NetworkCapabilities.TRANSPORT_X value. I needed to call addTransportType() instead.
As per the android docs:
"android.permission.CONNECTIVITY_INTERNAL" : deprecated This permission used to allow too broad access to sensitive methods and all its uses have been replaced by a more appropriate permission. Most uses have been replaced with a NETWORK_STACK or NETWORK_SETTINGS check. Please look up the documentation of the individual functions to figure out what permission now protects the individual function. #SystemApi Allows an internal user to use privileged ConnectivityManager APIs.

my app requires more permissions than needs

I have these permissions in my manifest.xml
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="dea.gov.ge.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
But in google play my app requires more permissions:
Identity
find accounts on the device;
Contacts
find accounts on the device;
Location
approximate location (network-based);
precise location (GPS and network-based);
Phone
read phone status and identity;
Photos/Media/Files
modify or delete the contents of your USB storage;
read the contents of your USB storage;
Storage
modify or delete the contents of your USB storage;
read the contents of your USB storage;
Wi-Fi connection information
view Wi-Fi connections;
Device ID & call information;
read phone status and identity
Other
receive data from Internet;
full network access;
read Google service configuration;
view network connections;
use accounts on the device;
I have google maps where i'm inserting some markers from xml file. and getting IMEI code.
But why does it needs contacts, photo/media files, storage etc.?

Categories

Resources