I have written an app to turn on bluetooth using voice.(Just called the intent and used the Action.request.enable method). Its working fine..
Now I want to turn off bluetooth. I tried using bluetoothadapter.disable() but the whole application crashes. Please help me.. This is my code
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if(mBluetoothAdapter.isEnabled())
{
mBluetoothAdapter.disable();
}
try this for disable
Intent discoverableIntent = new
Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 1);
startActivity(discoverableIntent);
for more help :: this
Paste some logcat information will be helpful.
I found that Android doesn't provide startActivityForResult to popup a window to remind user to turn off Bluetooth.So you must call the BluetoothAdapter.disable() method to turn off Bluetooth.
1.When you use startActivityForResult with intent to remind user to turn on Bluetooth,it's working.It means that you are already with the declaration of the permission
<uses-permission android:name="android.permission.BLUETOOTH" />
2.When you use the BluetoothAdapter.disable() method to turn off Bluetooth, the app crashed.I think that maybe you miss the the declaration of the permission
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
for BluetoothAdapter.disable().
NOTE:
BluetoothAdapter.getDefaultAdapter() needs:
<uses-permission android:name="android.permission.BLUETOOTH" />
BluetoothAdapter.disable() needs:
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
Related
I developed an Android App with Qt. Some time ago I got an E-Mail "Approval for background location" from Play Store. I'm not familiar with Java and I don't understand the problem.
My App uses GPS only if the app is visible (in foreground), but not if it is invisible (background).
My Manifest.xml looks like this:
<uses-sdk android:minSdkVersion="23" android:targetSdkVersion="28"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="com.android.vending.BILLING"/>
Now I found on Play Store
...the ACCESS_BACKGROUND_LOCATION permission only affects an app's access to location when it runs in the background.
For me this means because I don't use ACCESS_BACKGROUND_LOCATION my App only uses GPS if it is visible (foreground). So what is the problem? Thanks...
I had a very similar problem. I double checked my AndroidManifest.xml, there was no ACCESS_BACKGROUND_LOCATION in it.
I use the QGeoPositionInfoSource from C++ to get periodic location updates. I never called the stopUpdates() function, that had to be fixed. It must be called when the app goes to background, e.g.: onPause.
Here is a very useful video with the clue, and some other useful reading material:
https://youtu.be/xTVeFJZQ28c?t=394
https://developer.android.com/training/location/permissions
https://youtu.be/xTVeFJZQ28c?t=394
https://developer.android.com/about/versions/oreo/background-location-limits
There is a signal QGuiApplication::applicationStateChanged which can be utilized for this, an example code:
if (const auto *const gui =
qobject_cast<const QGuiApplication *const>(qApp)) {
connect(gui, &QGuiApplication::applicationStateChanged, this,
[&](Qt::ApplicationState state) {
switch (state) {
case Qt::ApplicationState::ApplicationActive:
source->startUpdates();
break;
default:
source->stopUpdates();
}
});
}
The source is a pointer for the QGeoPositionInfoSource.
I'm developing an alarm aplication, so when the alarm goes on it start an activity with an off button
It's very simple, but my question, is there a way to start the activity even if the device is locked???
You need to use the WakeLock and also disable your keyboard for the respective Activity. They can be achieved by help of WindowManager.LayoutParams class.
WakeLock allows your app to wake up the screen i.e. turn the display on while your Activity or your complete app is running in the foreground. To learn more about implementing the WakeLock go through the following link:
http://developer.android.com/training/scheduling/wakelock.html
To disable your keyguard you need to use WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED and/or WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD flags for the respective Activity.
To know more about them, go to the following links:
http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#FLAG_DISMISS_KEYGUARD
http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#FLAG_SHOW_WHEN_LOCKED
You will also need to use the following permissions in the Manifest file of your Android Project:
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
You need the following permission in AndroidManifest.xml file:
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
Check this link
I tried to make a call when I click on Android Mobile field with this code
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:"+contactNo));
startActivity(callIntent);
And added these in AndroidManifest.xml
<uses-permission android:name="packagename.permission.C2D_MESSAGE"/>
<uses-permission android:name="android.permission.CALL_PHONE" />
And finally I am getting **java.lang.SecurityException** exception.
Why it Happens? and What is the Correct Procedure to Make a Call when click on Contact field?
use Intent.ACTION_DIAL instead of Intent.ACTION_CALL
And action_dial does not need permission, and it will show dialed phone number to user on dialer and then it will be users wish to place a call or not. This will be better approach to give user a control of what they wants to do.
hope that will help you
In order to use Intent.ACTION_CALL you have to add a permission to your manifest.
Add this permission to your manifest and it should be ok
<uses-permission android:name="android.permission.CALL_PHONE"/>
I work with and Android WiFiDirect app. I changed strongly given WiFiDirectDemo to make fallowing things:
group owner is the one to send data to rest of peers,
while pictre is sent, new activity is created,
moving picture with onTouch forces to create an xml fie.
The problem: while I start new service to send this xml (with onTouch in new Activity) I got connection refused to given ip and port, which to I've sent just sec. ago a picture.
I totaly can't handle that connection refused error, I suspect that IP is busy. While I click back button to previous activity it allows me to create an xml file and again shows connection refused.
I would like to know what to do to avoid this error:
E/Screen sharing: failed to connect to /192.168.49.10 (port 8988) after 5000ms: isConnected failed: ECONNREFUSED (Connection refused)
My permissions:
<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" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />
<uses-permission android:name="android.permission.MANAGE_DOCUMENTS"/>
UPDATE
Problem solved:
You start from one activity 2nd activity to which You put sent image. Now You want to again ask for service to send another data to sent IP and You can't. IP is busy and connection is refused BECAUSE Broadcast Receiver is still searching for message from 1st activity. What You need to do is to #Override OnResume method in 2nd activity like this.
#Override
public void onResume(){
super.onResume();
receiver = new WiFiDirectBroadcastReceiver(manager, channel, MainActivity.activityMain);
registerReceiver(receiver, intentFilter);
}
Where WiFiBroadcast receiver is my broadcast receiver manager and channel is like in 1st activity and activityMain is simple Activity passed from 1st to 2nd like this
public static MainActivity activityMain;
activityMain = this;
I have the code that enables the blueooth on a device.
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, 1);
I am wondering how can I disable it?
Take a look at BluetoothAdapter.disable() function. You should ask a user before disabling bluetooth though.
Don't forget to add next permission to your AndroidManifest.xml :
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
I managed to use .disable and .enable with the BLUETOOTH_ADMIN permission.