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.
Related
We know you can open a call application using this code:
startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse("tel:0377778888")));
Is it possible to make a direct call without having to go through the default application of the device?
This code snippet makes the direct call:
// Check the SDK version and whether the permission is already granted or not.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.CALL_PHONE}, PERMISSIONS_REQUEST_PHONE_CALL);
} else {
//Open call function
String phone = "7769942159";
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:+91" + phone));
startActivity(intent);
}
Use this permission in manifest:
<uses-permission android:name="android.permission.CALL_PHONE" />
Yes, You can do it by just replacing Intent.ACTION_DIAL with Intent.ACTION_CALL in your code.
And you must need the CALL permission to the app.
For below Marshmallow devices you can make it by simply placing the below line in your manifest under manifest tag
<uses-permission android:name="android.permission.CALL_PHONE" />.
But for Marshmallow or above OS devices you need to declare the permission in manifest like below
<uses-permission-sdk-23 android:name="android.permission.CALL_PHONE" />
And you need to ask the user Runtime permission for CALL_PHONE access.
To request permission
ActivityCompat.requestPermissions(thisActivity,
new String[]{Manifest.permission.CALL_PHPNE})
To check permission
ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.CALL_PHONE)
!= PackageManager.PERMISSION_GRANTED
For more info https://developer.android.com/training/permissions/requesting.html
You are looking for ACTION_CALL: https://developer.android.com/reference/android/content/Intent.html
Intent intent=new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+phno);
startActivity(intent);
Android Manifest
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
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"/>
With the following code you can pop up a window with bluetooth acces request:
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
The output of the code is the following image:
Is it possible to change the message (so not "An app wants to turn on Bluetooth", but something like "Hey can you please turn on Bluetooth?"
You should create your own dialog with your own message and then call
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mBluetoothAdapter.enable();
And do not forget to declare the BLUETOOTH_ADMIN permission in your manifest.
Anyway, this operation is discouraged... The enable() method is provided only for applications that include a user interface for changing system settings, such as a "power manager" app.
No, this is build-in Android functionality, so you would have to make a custom dialog which asks the question the way you want.
Then you would enable or disable Bluetooth programmatically based on what the users choice was.
I am developing an app and in the manifest I have:
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
When I click on the button to execute this code:
Intent intentcall = new Intent();
intentcall.setAction(Intent.ACTION_CALL);
intentcall.setData(Uri.parse("tel:" + phonenumber)); // set the Uri
startActivity(intentcall);
It will run fine on phones, and on tablets it pops up with a display where you can view or add the number to contacts. However, if I keep the permission in the manifest, it isn't available for tablets in the market. How can I keep the code behavior and still have it display in the market for tablets as well as phones?
In the AndroidManifest you need:
<uses-feature android:name="android.hardware.telephony" android:required="false" />
The CALL_PHONE permission implies telephony is required, but if you specify that is not you won't be filtered.
Try to use Intent.ACTION_DIAL instead Intent.ACTION_CALL.
For example:
try {
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phone_number));
startActivity(intent);
} catch (Exception e) {
//TODO smth
}
And in this case you can completely remove these tags from AndroidManifest.xml:
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-feature android:name="android.hardware.telephony" android:required="false" />
Regarding "uses-feature" and it crashing - are you checking that telephony is available before actually making the call? It might be you need to do that extra step for the case when the app is on tablets. All you are saying in the manifest is that the feature is not required. It probably relies on you to actually implement the logic around that.
Instead of adding a user with the ACTION_CALL identifier, change it to ACTION_INSERT_OR_EDIT.
You'll need these permissions too, instead of the CALL_PHONE permission:
<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
<uses-permission android:name="android.permission.WRITE_CONTACTS"></uses-permission>
Take a look at this related question:
can't find app on market
From google docs:
Declared elements are informational only, meaning that
the Android system itself does not check for matching feature support
on the device before installing an application
usage is only for google play
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" />