check whether NFC Is active when camera is on in Android - android

I've made a screen where camera and nfc is used together but have found out that some devices or after Android 11, NFC seems to get blocked when camera is used.
Therefore, I was trying to implement where if NFC is blocked, I was trying to show a text in the screen that NFC is blocked due to camera but have a hard time checking if NFC is active or not.
Is there a way I can check is NFC is blocked or not when using a camera?
Finding a way the check if NFC is active or not when using a camera

I've not had any Android 11 device to find this myself and it depends on what the camera is doing to block/disable NFC but there is a Broadcast Receiver for the state of NFC
e.g.
// Listen to NFC setting changes
this.registerReceiver(mReceiver, filter);
}
// Listen for NFC being turned on while in the App
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (action.equals(NfcAdapter.ACTION_ADAPTER_STATE_CHANGED)) {
final int state = intent.getIntExtra(NfcAdapter.EXTRA_ADAPTER_STATE,
NfcAdapter.STATE_OFF);
switch (state) {
case NfcAdapter.STATE_OFF:
// Tell the user to turn NFC on if App requires it
break;
case NfcAdapter.STATE_TURNING_OFF:
break;
case NfcAdapter.STATE_ON:
// Enabled NFC;
break;
case NfcAdapter.STATE_TURNING_ON:
break;
}
}
}
};

Related

Using a Bluetooth Headset as a Mic and Speaker in a Voice Chat App

I have a voice chat app, which works just fine. Currently I'm trying to make the app support bluetooth headsets in case any was connected. There are two cases that I need to handle:
The bluetooth headset is connected before the call starts.
The bluetooth headset connects to the device during the call.
For the first case, the app should automatically starts using the headset as its default input/output audio device. On the other hand, the app, upon headset connection to the device, should switch from the current input/output audio device to the bluetooth headset.
I was able to handle the first case successfully using the following code:
mAudioManager.setMode(0);
mAudioManager.startBluetoothSco();
mAudioManager.setBluetoothScoOn(true);
mAudioManager.setMode(android.media.AudioManager.MODE_IN_CALL);
As for the second case, I created a BroadcastReciever that listens when a bluetooth headset is connected as follows:
mBroadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
mAudioManager.setMode(0);
mAudioManager.startBluetoothSco();
mAudioManager.setBluetoothScoOn(true);
mAudioManager.setMode(android.media.AudioManager.MODE_IN_CALL);
} else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
mAudioManager.setMode(0);
mAudioManager.startBluetoothSco();
mAudioManager.setBluetoothScoOn(false);
mAudioManager.setMode(android.media.AudioManager.MODE_IN_CALL);
}
}
};
The BroadcastReciever was able to detect headset connections/disconnections, and the call was directing the sound to the headset rather than the phone's earpiece. The issue is that the app keeps using the device's mic as the input audio device rather than the headset's. After a very long inspection, I realized that when the BroadcastReciever gets a notification that a headset is connected, I need to wait a little bit before calling mAudioManager.startBluetoothSco(); to get the app to use the headset's mic.
The question is, what kind of event should I be listening to, after knowing that the bluetooth headset is connected, so that I can start capturing the voice from the headset's mic?
It turns out the I shouldn't be listening to BluetoothDevice.ACTION_ACL_CONNECTED, rather the one I should consider is BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED. The BroadcastReciever should be initialized as follows:
mBroadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED.equals(action)) {
int state = intent.getIntExtra(BluetoothHeadset.EXTRA_STATE, -1);
boolean result = state == BluetoothHeadset.STATE_CONNECTED;
mCallback.onConnected(result);
}
}
};

Bluetooth enable time

I am using the below code as per requirement from client to internally enable Bluetooth and disable it when exit the application.
if (!bluetoothAdapter.isEnabled()) {
MMLogger.logInfo(MMLogger.LOG_BLUETOOTH, "BluetoothSyncController - Bluetooth was OFF, so Turn it ON");
bluetoothAdapter.enable();
try {
Thread.sleep(WAIT_FOR_SOMETIME_TO_START_BLUETOOTH);
} catch (InterruptedException ignore) {
}
MMLogger.logInfo(MMLogger.LOG_BLUETOOTH, "BluetoothSyncController - Bluetooth turned ON");
}
IS there any standard time for WAIT_FOR_SOMETIME_TO_START_BLUETOOTH ? I mean any documentation ?
You might try this answer. There seem to be some standard bluetooth events and handlers out there.
From that source: There are events that your activity can manage such as
STATE_OFF, STATE_TURNING_ON, STATE_ON, STATE_TURNING_OFF
and you can catch these with a BroadcastReciever. First you want to make sure that you grant permissions for bluetooth inside of your manifest with:
<uses-permission android:name="android.permission.BLUETOOTH" />
Then you can create a custom broadcast receiver that has the following onReceive():
#Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
switch(state) {
case BluetoothAdapter.STATE_OFF:
..
break;
case BluetoothAdapter.STATE_TURNING_OFF:
..
break;
case BluetoothAdapter.STATE_ON:
..
break;
case BluetoothAdapter.STATE_TURNING_ON:
..
break;
}
}
}
Then instead of making a thread to wait you can have a receive event trigger the code you want to run. For more info on using a BroadcastReciever, see the link I provided or go straight to the android documentation.

Programmatically bonding to BLE device on Android

I'm writing an Android application in which I'd like to programmatically bond to a custom BLE device. I have the manual bonding working in which the user enters the PIN using the standard Android Bluetooth pairing dialog, but I have not been able to find any information on how to automatically bond a BLE device programatically, without user intervention. Is that possible? If so, what's the process?
I was able to make this work MOST OF THE TIME by registering a BroadcastReceiver to receive the BluetoothDevice.ACTION_BOND_STATE_CHANGED intent and then calling BluetoothDevice.setPin after receiving the BluetoothDevice.BOND_BONDING message. As is the case with most BLE things in Android, this seems to act slightly differently depending on the device and Android version. Unfortunately, I can't seem to stop Android from also receiving the bluetooth intent, so the PIN entry screen still pops up for a second before the bonding is completed.
private final BroadcastReceiver mReceiver = new BroadcastReceiver()
{
#Override
public void onReceive(Context context, Intent intent)
{
final String action = intent.getAction();
Logger("Broadcast Receiver:" + action);
if (action.equals(BluetoothDevice.ACTION_BOND_STATE_CHANGED))
{
final int state = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.ERROR);
if(state == BluetoothDevice.BOND_BONDING)
{
Logger("Bonding...");
if (mDevice != null) {
mDevice.setPin(BONDING_CODE.getBytes());
Logger("Setting bonding code = " + BONDING_CODE);
}
}
else if(state == BluetoothDevice.BOND_BONDED)
{
Logger("Bonded!!!");
mOwner.unregisterReceiver(mReceiver);
}
else if(state == BluetoothDevice.BOND_NONE)
{
Logger("Not Bonded");
}
}
}
};
I managed to do this - see my answer here.
The TL;DR is: forget about ACTION_BOND_STATE_CHANGED; you don't need it. Instead listen to ACTION_PAIRING_REQUEST, and set the priority high. In the broadcast receiver when you get ACTION_PAIRING_REQUEST, call setPin() with your PIN and then abortBroadcast() to prevent the system showing the notification.
All you can do to avoid user interaction is to force Just Works pairing. To do that, program the peripheral to accept pairing with NoInputNoOutput IO Capability.

Detect headphones Android

I program in recent years to Android and I wonder something:
How to detect the presence of headphones?
There is a method: isWiredHeadsetOn() but it doesn't work.
I've tried that but it doesn't work:
AudioManager am = (AudioManager)getSystemService(AUDIO_SERVICE);
Log.i("am.isWiredHeadsetOn()", am.isWiredHeadsetOn() + "");
if (am.isWiredHeadsetOn()) {
//code
}
Thank you (and sorry if I made spelling mistakes, I am French)
#Phil answer at https://stackoverflow.com/a/19102661/4758255 is a good implementation to detect the headphone.
Here I'm quoting from his answer. Please upvoted his answer not this!
Upvote the answer: https://stackoverflow.com/a/19102661/4758255
I've had an app in the store for three years that monitors both the wired headset and bluetooth state and nobody has ever complained about battery drain. But that is because I am successfully using a single service and broadcast receiver for detecting events from both. Here's the two key classes:
public class HeadsetStateBroadcastReceiver extends BroadcastReceiver {
public static final String[] HEADPHONE_ACTIONS = {
Intent.ACTION_HEADSET_PLUG,
"android.bluetooth.headset.action.STATE_CHANGED",
"android.bluetooth.headset.profile.action.CONNECTION_STATE_CHANGED"
};
#Override
public void onReceive(final Context context, final Intent intent) {
boolean broadcast = false;
// Wired headset monitoring
if (intent.getAction().equals(HEADPHONE_ACTIONS[0]) {
final int state = intent.getIntExtra("state", 0);
AudioPreferences.setWiredHeadphoneState(context, state > 0);
broadcast = true;
}
// Bluetooth monitoring
// Works up to and including Honeycomb
if (intent.getAction().equals(HEADPHONE_ACTIONS[1])) {
int state = intent.getIntExtra("android.bluetooth.headset.extra.STATE", 0);
AudioPreferences.setBluetoothHeadsetState(context, state == 2);
broadcast = true;
}
// Works for Ice Cream Sandwich
if (intent.getAction().equals(HEADPHONE_ACTIONS[2])) {
int state = intent.getIntExtra("android.bluetooth.profile.extra.STATE", 0);
AudioPreferences.setBluetoothHeadsetState(context, state == 2);
broadcast = true;
}
// Used to inform interested activities that the headset state has changed
if (broadcast) {
LocalBroadcastManager.getInstance(context).sendBroadcast(new Intent("headsetStateChange"));
}
}
}
Here is the service I use to register the broadcast receiver:
public class HeadsetMonitoringService extends Service {
HeadsetStateBroadcastReceiver headsetStateReceiver;
#Override
public void onCreate() {
headsetStateReceiver = new HeadsetStateBroadcastReceiver();
final IntentFilter filter = new IntentFilter();
for (String action: HeadsetStateBroadcastReceiver.HEADPHONE_ACTIONS) {
filter.addAction(action);
}
registerReceiver(headsetStateReceiver, filter);
}
#Override
public int onStartCommand(final Intent intent, final int flags, final int startId) {
return START_STICKY;
}
#Override
public void onDestroy() {
unregisterReceiver(headsetStateReceiver);
}
#Override
public IBinder onBind(final Intent intent) {
return null;
}
}
And here is my manifest entry:
<service
android:name=".services.HeadsetMonitoringService"
android:enabled="true"
android:exported="false" >
<intent-filter>
<action android:name="initialiseHeadsetService" />
</intent-filter>
</service>
How it works is as follows:
I use an on boot broadcast receiver to send a start service message to the HeadsetMonitoringService (you don't have to do it this way, you could just do this when your application starts instead). The HeadsetMonitoringService in turn registers an instance of a broadcast listener that listens to all the headset events I am interested in - they are held in the HEADPHONE_ACTIONS array. Because the service is sticky it hangs around - and therefore so does the broadcast listener. But because both the service and the broadcast listener are event driven they do not consume any power until a headset state change occurs. Additionally, because the service is sticky, it will be restarted by the OS if it dies unexpectedly.
Whenever I receive a headset state change event I also fire a local broadcast so that interested activities can check the new state and take action if required.
For completeness, I should point out that I use another class (not shown here), AudioPreferences, to store as preferences both the Bluetooth and wired headset state, which can then be accessed whenever I need to know the headset state.
Your application will need the android.permission.BLUETOOTH permission if you are interested in the state of a Bluetooth headset. If not, just take out the Bluetooth related actions from the HEADPHONE_ACTIONS array and delete the associated if blocks from the onReceive method.
If you are OK with Marshmallow and up the AudioDeviceCallback might be what you are looking for. It works with an AudioManager and tells you when anything connects and disconnects.
AudioManager.isWiredHeadsetOn() appeared to be the right thing to do. According to the Android developer doc :
Checks whether a wired headset is connected or not.
This is not a valid indication that audio playback is actually over the wired headset as audio routing depends on other conditions.
Returns
true if a wired headset is connected. false if otherwise
But :
you have to add the associated permission to your manifest (MODIFY_AUDIO_SETTINGS)
according to this post, it doesn't work well with Bluetooth headset.

Way to ensure delivery of Action HEADSET_PLUG for ZTE T815 Android phone

I have registered a BroadcastReceiver to receive ACTION_HEADSET_PLUG which works fine for most devices, ie it is called whenever the headset is plugged or unplugged. But on others eg the ZTE T815, the Intent is never sent/received when the headset is plugged/unplugged.
For reference here is the code for the receiver registration:
private final BroadcastReceiver headsetPlugReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "Received intent=" + intent);
if (intent.getAction().equalsIgnoreCase(Intent.ACTION_HEADSET_PLUG)) {
// do stuff
}
}
};
public void onCreate(Bundle savedState) {
super.onCeate(savedState);
// ...
registerReceiver(headsetPlugReceiver, new IntentFilter(Intent.ACTION_HEADSET_PLUG));
}
Further info:
The Intent is dispatched but only after the HEADSET_HOOK command is fired on the headset.
And when the Intent is dispatched
final int microphone = intent.getIntExtra("microphone", 0);
always returns 0 (ie no microphone).
So
Is there some config/code that can force the delivery of this
Intent?
How do I get the Intent to correctly report whether a
microphone exists or not?
It turns out the ZTE T815 has an OMTP TRRS config for its audio socket instead of CTIA/AHJ like every other modern Android device.
See http://en.wikipedia.org/wiki/Phone_connector_%28audio%29
A sad state of affairs, especially when trying to use audio feed inpout across products.

Categories

Resources