How can i check whether NFC is enabled or not programmatically? Is there any way to enable the NFC on the device from my program? Please help me
NfcManager manager = (NfcManager) context.getSystemService(Context.NFC_SERVICE);
NfcAdapter adapter = manager.getDefaultAdapter();
if (adapter != null && adapter.isEnabled()) {
// adapter exists and is enabled.
}
You cannot enable the NFC programmatically. The user has to do it manually through settings or hardware button.
This can be done simply using the following code:
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
if (nfcAdapter == null) {
// NFC is not available for device
} else if (!nfcAdapter.isEnabled()) {
// NFC is available for device but not enabled
} else {
// NFC is enabled
}
Remember that the user can turn off NFC, even while using your app.
Source: https://developer.android.com/guide/topics/connectivity/nfc/nfc#manifest
Although you can't programically enable NFC yourself, you can ask the user to enable it by having a button to open NFC settings like so:
Intent intent
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
intent = new Intent(Settings.ACTION_NFC_SETTINGS);
} else {
Intent intent = new Intent(Settings.ACTION_WIRELESS_SETTINGS);
}
startActivity(intent);
I might be a little late here, but I've implemented a 'complete' example with detection of
NFC capability (hardware), and
Initial NFC state (enabled or disabled in settings), and
Changes to the state
I've also added a corresponding Beam example which uses the
nfcAdapter.isNdefPushEnabled()
method introduced in later Android versions to detect beam state like in 2) and 3).
Use PackageManager and hasSystemFeature("android.hardware.nfc"), matching the <uses-feature android:name="android.hardware.nfc" android:required="false" /> element you should have in your manifest.
Since 2.3.3 you can also use NfcAdapter.getDefaultAdapter() to get the adapter (if available) and call its isEnabled() method to check whether NFC is currently turned on.
mNfcAdapter = NfcAdapter.getDefaultAdapter(this.getApplicationContext());
try {
if (mNfcAdapter != null) {
result = true;
}
}
We can verify using NfcAdapter with context.
Related
I develop an app with NFC Tags interaction. Most of time application must ignore tag discovery events. And other apps should not catch tag discoery events when my app is foreground. So I used EnableForegroundDispatch to handle that.
This works fine. I handle excess TAG_DISCOVERED intents into my activity's onNewIntent methods.
The problem: When my activity is launched from another app, enableForegroundDispatch is not working. I receive TAG_DISCOVERED intents in new activity's onCreate method.
Here is my foregroundDispatch
if (nfcAdapter == null) {
Log.w(TAG, "enableForegroundNfcDispatch: no nfcAdapter", new Exception());
return;
}
Log.d(TAG, "enableForegroundNfcDispatch: ");
if (!nfcMonopolyMode) {
if (nfcTagDiscoveryPendingIntent != null) {
nfcAdapter.enableForegroundDispatch(this, nfcTagDiscoveryPendingIntent, nfcTagDiscoveryFilter, null);
return;
}
IntentFilter discovery = new IntentFilter(ACTION_TAG_DISCOVERED);
nfcTagDiscoveryFilter = new IntentFilter[]{discovery};
Intent nfcProcessIntent = new Intent(getBaseContext(), getClass())
.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
nfcTagDiscoveryPendingIntent = PendingIntent.getActivity(getApplicationContext(), REQUEST_CODE_NFC_DISCOVERED_TAG, nfcProcessIntent, 0);
nfcMonopolyMode = true;
nfcAdapter.enableForegroundDispatch(this, nfcTagDiscoveryPendingIntent, nfcTagDiscoveryFilter, null);
}
May be there is something to do with flags? Please help
My experience is that if you enableReaderMode with all the flags (including skip NDEF check) then basically detection of any card type is sent to your App if running in the foreground.
As well as enableReaderModein onResume I have Broadcaster Receiver to enableReaderMode of NFC service State change.
Checking a number of Different Tags this seems to work BUT they are all NfcA based of varying formats. As confirmed by stackoverflow.com/questions/33633736/… but it seems Android 10 might have a timing issues with Kiosk Mode
I am able to read and wirte in my NFC demo app but I need to check if the NFC option in setting menu is enabled or not . If its not enable I want to direct the user to the setting menu(I know how to do this) similar to NFC TagWriter by NXP.
In my application I am using the following SDK version
<uses-sdk android:minSdkVersion="7" />
<uses-sdk android:maxSdkVersion="16"/>
I am unable to check if the setting is enabled or not.
TNR gets it right, however also note that from Android version 16, there is a more specific settings action for NFC:
protected void startNfcSettingsActivity() {
if (android.os.Build.VERSION.SDK_INT >= 16) {
startActivity(new Intent(android.provider.Settings.ACTION_NFC_SETTINGS));
} else {
startActivity(new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS));
}
}
Use the below code to get the NFCAdapter.
NfcAdapter nfcAdpt = NfcAdapter.getDefaultAdapter(this);
if(nfcAdpt!=null)
{
if(nfcAdpt.isEnabled())
{
//Nfc settings are enabled
}
else
{
//Nfc Settings are not enabled
}
}
If you want to navigate user to Settings of NFC then use below Code
Intent setnfc = new Intent(Settings.ACTION_WIRELESS_SETTINGS);
startActivity(setnfc);
There is no NFC for API version 7. So change you manifest file as below
<uses-sdk android:minSdkVersion="10" />
<uses-sdk android:maxSdkVersion="16"/>
if (Build.VERSION.SDK_INT >= 10) {
i = new Intent("android.settings.NFC_SETTINGS");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(i);
} else {
i = new Intent(Settings.ACTION_WIRELESS_SETTINGS);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(i);
}
I use this code to start bluetooth:
BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
if(btAdapter == null)
returns; /no bluetooth
if(btAdapter.isEnabled() == false)
{ Toast("Bluetooth off. Starting it...");
if(btAdapter.enable() == false)
Toast("Error enabling bluetooth.");
}
It should be very simple. Just gets and adapter and if it is not enabled then I start it.
The problem is that isEnabled() returns false when bluetooth is actualy ON (it should return true) And calling to enable() returns false so it shows "Error enabling bluetooth." I guess because it was already ON. After that my bluetooth symbols (in the status bar) is gone.
Any hint?
Of course I have the permissions.
ensure you have the permissions correct in the manifest file
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
You can't enable Bluetooth without the user's confirmation. You need to do it like this,
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter != null) {
// Device supports Bluetooth
if (!mBluetoothAdapter.isEnabled()) {
// Bluetooth isn't enabled, so enable it.
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}
A dialog will appear requesting user permission to enable Bluetooth, as shown below.
If the user responds "Yes," the system will begin to enable Bluetooth and focus will return to your application once the process completes (or fails).
To check Bluetooth state, ON programmatically:
Add Following Permission : -
android.permission.BLUETOOTH
Use Following Function For Enable BLUETOOTH:-
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
private void turnOn() {
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new
Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivity(enableBtIntent);
}
}
I'd like to know if there is any way I can simply check whether a Bluetooth device is currently connected - I don't want to use a broadcast receiver - just check the state. I can't seem find out how this is done.
I currently have a listener that does listen to state changes with Bluetooth, and changes an internal variable accordingly - but, even though it sounds weird just saying it, it actually seems to miss the Bluetooth device disconnect broadcast sometimes. what I would like to do is run an additional check to see if the device really is still connected, or if the broadcast was missed...
so, how do I do this?
Thanks for reading/helping!
I use this to check the state of Bluetooth. I don't know how to check if is currently connected to another device but I think this can be a start point.
private void CheckBlueToothState() {
if (bluetoothAdapter == null) {
//stateBluetooth.setText("Bluetooth NOT support");
} else {
if (bluetoothAdapter.isEnabled()) {
if (bluetoothAdapter.isDiscovering()) {
//stateBluetooth.setText("Bluetooth is currently in device discovery process.");
} else {
//stateBluetooth.setText("Bluetooth is Enabled.");
}
} else {
//stateBluetooth.setText("Bluetooth is NOT Enabled!");
Intent enableBtIntent = new Intent(
BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}
}
I am developing an app that is going to use a ToggleButton to enable and disable BlueTooth. I manage to make the ToggelButton turn on and off BlueTooth, but I can not make it check if BlueTooth is turned on and off. The problem is that if you turn BlueTooth on or off from another location, you may turn off BlueTooth when you actually want to turn it on. Here is my code so far:
public void onClick(View v) {
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
if(adapter != null) {
if(adapter.getState() == BluetoothAdapter.STATE_ON) {
adapter.disable();
} else if (adapter.getState() == BluetoothAdapter.STATE_OFF){
adapter.enable();
} else {
//State.INTERMEDIATE_STATE;
}
}
}
How can I make it be checked when BlueTooth is on and unchecked when BlueTooth is off?
You will have to register a broadcast receiver with the following intent filter:
"android.bluetooth.intent.action.BLUETOOTH_STATE_CHANGED".
Whenever you receive this broadcast you should recheck for actual bluetooth state.
Check out Google's PowerWidget implementation.
The receiver with appropriate intent-filters is registered in the manifest.