Some GSM devices crashing with CDMA SignalStrength error on Android - android

My Android app monitors cellular signal strengths. On CDMA devices, it works without any problems. On many GSM devices, it works without any problems. However, on some GSM devices, I am getting force closes with the following error message:
java.lang.ClassCastException: android.telephony.cdma.CdmaCellLocation cannot be cast to android.telephony.gsm.GsmCellLocation
I am fairly certain that this issue occurs on some Samsung devices in the US when they receive a 4G LTE signal; I believe it occurs on other devices and countries as well. I am still trying to determine the exact service providers and devices involved.
Here is the relevant snippet of code from MyService.java; I have marked the line referenced by the error message with //********:
public void onCreate() {
tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
...
}
public int onStartCommand(Intent in, int flags, int startId) {
signalStrengthListener = new SignalStrengthListener();
((TelephonyManager)getSystemService(TELEPHONY_SERVICE)).listen(signalStrengthListener, SignalStrengthListener.LISTEN_SIGNAL_STRENGTHS | SignalStrengthListener.LISTEN_SERVICE_STATE | SignalStrengthListener.LISTEN_DATA_CONNECTION_STATE | SignalStrengthListener.LISTEN_CELL_LOCATION);
return START_STICKY;
...
}
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
isGSM = signalStrength.isGsm();
if (isGSM == true) {
GCellLoc = (GsmCellLocation)tm.getCellLocation(); //********
...
}
Not sure why is anything from android.telephony.cdma is appearing if this code should only be triggered when isGSM returns true. Perhaps when an LTE signal is detected, that puts the phone in some sort of dual CDMA/GSM mode as far as the Android API is concerned? I have not been able to find any documentation of similar behavior. How can I handle this in my code? Thanks.

I cannot explain why it is happening. I have some theories, but you have similar ones.
While you sort out the reason, you can deal with issue pragmatically. You know that sometimes even though you are getting a GSM SignalStrength in your callback, sometimes on some devices you get a CDMA CellLocation from TelephonyManager and write your code to handle that case using instanceOf instead of relying on .isGSM() from the SignalStrength.
CellLocation cellLoc = tm.getCellLocation();
if(cellLoc instanceof GsmCellLocation) {
GCellLoc = (GsmCellLocation) cellLoc;
// do work
}

Related

How to check if SIM is active?

I have a use case where I need to check whether a SIM is active in the device. In older devices, I can use TelephonyManager to get the SIM state and check whether it is SIM_STATE_READY. The issue is with API 22 and above.
Using SubscriptionManager, when I call getActiveSubscriptionInfoList, it sends me details about the SIMs present, even if I have turned them off. I went through the documentation of SubscriptionManager but couldn't find a similar method to check SIM's state. Using TelephonyManager in API above 22 gives information only about the default SIM, I would like to know this about both slots in dual SIM phones. Also, I found an overloaded variant of getSimState in TelephonyManager which does accept the slot as a parameter, but that got introduced in API 26. I would like a solution that will work in APIs 22-25 as well.
Is there a way I could identify that even though the SIM is present in the device, it isn't active?
You can check by like this if Device is dual sim
SubscriptionManager sManager = (SubscriptionManager) context.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
SubscriptionInfo infoSim1 = sManager.getActiveSubscriptionInfoForSimSlotIndex(0);
SubscriptionInfo infoSim2 = sManager.getActiveSubscriptionInfoForSimSlotIndex(1);
int count = 0;
if (infoSim1 != null ) {
count++;
}
if (infoSim2 != null){
count++;
}
count decides that how many sims are active.
OR
You can get the count then check one by one
sManager.getActiveSubscriptionInfoCount()
Hope it's work.
TelephonyManager manager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
if (manager.getPhoneCount() == 2) {
// Dual sim
}
Examples here

How to get Bluetooth Headset battery level?

I'm trying to build an app which gets battery level of currently connected Bluetooth headset. This app can be used on phones which don't have this functionality built-in.
While searching on stackoverflow, I found How to get Bluetooth Headset battery status in android this question. I got the currently connected Bluetooth headset using BluetoothProfile.HEADSET profile.
But in the device object of type BluetoothDevice I don't see any method or property to get battery level of Bluetooth Headset.
I can get the device name and isAudioConnected.
If question is about Bluetooth HFP feature: HF indicators feature is optional for the both sides. If the both sides support it, BluetoothHeadset will broadcast BluetoothHeadset.ACTION_HF_INDICATORS_VALUE_CHANGED with BluetoothHeadset.EXTRA_HF_INDICATORS_IND_ID equal 2 (Battery Level) and BluetoothHeadset.EXTRA_HF_INDICATORS_IND_VALUE with scope 0..100. Do not remember Android version were it was implemented, you should check it.
Also battery level can be implemented in device using vendor specific HFP AT commands (especially for old handsfree devices) and maybe BLE.
I found a solution, but it only works on android 8 and above
I took this code from here
Kotlin
fun getBatteryLevel(pairedDevice: BluetoothDevice?): Int {
return pairedDevice?.let { bluetoothDevice ->
(bluetoothDevice.javaClass.getMethod("getBatteryLevel"))
.invoke(pairedDevice) as Int
} ?: -1
}
The first thing to register BroadcastReciver by "android.bluetooth.device.action.BATTERY_LEVEL_CHANGED"
and you can receive this action by the broadcast receiver then get extra data by "android.bluetooth.device.extra.BATTERY_LEVEL"
and if you want to trigger this action, you need to reconnect your Bluetooth device or Bluetooth device battery level happened to change.
Good luck for you.
Connected AirPods Pro to OnePlus 5T with Android 9.
None of those registered events happen:
"android.bluetooth.device.action.BATTERY_LEVEL_CHANGED"
"android.bluetooth.headset.profile.action.AUDIO_STATE_CHANGED"
"android.bluetooth.headset.action.HF_INDICATORS_VALUE_CHANGED"
I am Able to achieve the handset battery Level in Java
try {
BluetoothDevice device = bluetoothAdapter.getRemoteDevice("Connected device ID");
java.lang.reflect.Method method;
method = device.getClass().getMethod("getBatteryLevel");
int value = (int) method.invoke(device);
result.success(value);
} catch (Exception ex) {
result.error("invalid_argument", "'deviceId' argument is required to be string", null);
break;
}
This is #Kirill Martyuk answer as an Extension variable
val BluetoothDevice.batteryLevel
get() = this.let { device ->
val method = device.javaClass.getMethod("getBatteryLevel")
method.invoke(device) as Int?
} ?: -1
Usage would be something like
val manager = context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager?
val adapter = manager?.adapter
val devices = adapter?.bondedDevices.orEmpty()
devices.forEach { device ->
Log.d("DEVICE_NAME", device.name)
Log.d("CHARGE_LEVEL", device.batteryLevel.toString())
}

How to detect when a BLE device is not in range anymore?

I use a LeScanCallback (can not use the newer scan methods because I'm developing for api 18. Not that it matters, since the android 5.0+ apis don't offer this functionality either) to detect when a nearby BLE device is detected:
private BluetoothAdapter.LeScanCallback bleCallback = new BluetoothAdapter.LeScanCallback() {
#Override
public void onLeScan(BluetoothDevice bluetoothDevice, int i, byte[] bytes) {
discoveredDevices.add(bluetoothDevice);
}
};
I am not pairing or connecting with the devices because that's not required, I simply want to see which devices are nearby.
I'm trying to make a service that, every 5 mins or so, calls a webserver to update which devices are nearby at that moment.
Tricky part is that the android device will be moving, so a bluetooth device that is nearby right now, might not be in 5 mins. In that case I need to remove it from discoveredDevices.
Ideally, I would like to receive a callback when a bluetooth device was in range before, but is not anymore. This callback doesn't exist though.
(I'm aware of the android.bluetooth.device.action.ACL_CONNECTED and android.bluetooth.device.action.ACL_DISCONNECTED broadcasts, but those are for when you connect to a bluetooth device, which I don't want.)
An option is to do a fresh scan every 5 mins, but you can't tell when all nearby devices have been discovered, so you would have to do a timed scan, e.g. scan for 5 seconds and then send the collected data to the webservice.
This sounds dirty and risky because you can never know for sure all nearby devices were discovered within the allotted time, so I would very much like to avoid doing it like that.
Is there another way to do this?
Edit
Some devices continuously report discovery of nearby bluetooth devices, even if they were already discovered before. If that functionality was universal I could solve my problem, however this is device specific.
My phone's bluetooth adapter for example only discovers nearby devices once. Some other devices I have tested with do continuously report the same nearby devices, but not all devices do, so I can't rely on that unfortunately.
This sounds dirty and risky because you can never know for sure all nearby devices were discovered within the allotted time, so I would very much like to avoid doing it like that.
That sounds like a reasonable assumption, but it's wrong.
Bluetooth low energy works in a particular way and BLE devices have some limits. For instance, they have a fixed range of possible advertising frequencies, ranging from 20 milliseconds to 10.24 seconds, in steps of 0.625 milliseconds. See here and here for more detailed information.
This means that it can take at most 10.24 seconds before a device will broadcast a new advertisement package. BLE devices generally, if not always, provide a way for their owner to adjust their advertising frequency, so the frequency can of course vary.
In cases where you are periodically collecting data about nearby devices, like yours, it is fine to use a scan with a fixed time limit, save that data somewhere, restart the scan, collect new data, compare with old data --> get results.
For example, if a device was found in scan 1 but not in scan 2, you can conclude that the device was in range, but is not anymore.
Same goes for the other way around: if a device was found in scan 4 but not in scan 3, it is a newly discovered device.
Finally, if a device was found in scan 5, was not found in scan 6, but was again found in scan 7, it is rediscovered and can be handled as such if need be.
Because I'm answering my own question here, I'll add the code that I used to implement this.
I have the scanning done in a background service, and communicate to other parts of the app using BroadcastReceivers. Asset is a custom class of mine that holds some data. DataManager is a custom class of mine that - how did you guess it - manages data.
public class BLEDiscoveryService extends Service {
// Broadcast identifiers.
public static final String EVENT_NEW_ASSET = "EVENT_NEW_ASSET ";
public static final String EVENT_LOST_ASSET = "EVENT_LOST_ASSET ";
private static Handler handler;
private static final int BLE_SCAN_TIMEOUT = 11000; // 11 seconds
// Lists to keep track of current and previous detected devices.
// Used to determine which are in range and which are not anymore.
private List<Asset> previouslyDiscoveredAssets;
private List<Asset> currentlyDiscoveredAssets;
private BluetoothAdapter bluetoothAdapter;
private BluetoothAdapter.LeScanCallback BLECallback = new BluetoothAdapter.LeScanCallback() {
#Override
public void onLeScan(BluetoothDevice bluetoothDevice, int i, byte[] bytes) {
Asset asset = DataManager.getAssetForMACAddress(bluetoothDevice.getAddress());
handleDiscoveredAsset(asset);
}
};
#Override
public void onCreate() {
super.onCreate();
BluetoothManager manager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
bluetoothAdapter = manager.getAdapter();
previouslyDiscoveredAssets = new ArrayList<>();
currentlyDiscoveredAssets = new ArrayList<>();
handler = new Handler();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
// Start scanning.
startBLEScan();
// After a period of time, stop the current scan and start a new one.
// This is used to detect when assets are not in range anymore.
handler.postDelayed(new Runnable() {
#Override
public void run() {
performRepeatingTask();
// Repeat.
handler.postDelayed(this, BLE_SCAN_TIMEOUT);
}
}, BLE_SCAN_TIMEOUT);
// Service is not restarted if it gets terminated.
return Service.START_NOT_STICKY;
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onDestroy() {
handler.removeCallbacksAndMessages(null);
stopBLEScan();
super.onDestroy();
}
private void startBLEScan() {
bluetoothAdapter.startLeScan(BLECallback);
}
private void stopBLEScan() {
bluetoothAdapter.stopLeScan(BLECallback);
}
private void handleDiscoveredAsset(Asset asset) {
currentlyDiscoveredAssets.add(asset);
// Notify observers that we have a new asset discovered, but only if it was not
// discovered previously.
if (currentlyDiscoveredAssets.contains(asset) &&
!previouslyDiscoveredAssets.contains(asset)) {
notifyObserversOfNewAsset(asset);
}
}
private void performRepeatingTask() {
// Check if a previously discovered asset is not discovered this scan round,
// meaning it's not in range anymore.
for (Asset asset : previouslyDiscoveredAssets) {
if (!currentlyDiscoveredAssets.contains(asset)) {
notifyObserversOfLostAsset(asset);
}
}
// Update lists for a new round of scanning.
previouslyDiscoveredAssets.clear();
previouslyDiscoveredAssets.addAll(currentlyDiscoveredAssets);
currentlyDiscoveredAssets.clear();
// Reset the scan.
stopBLEScan();
startBLEScan();
}
private void notifyObserversOfNewAsset(Asset asset) {
Intent intent = new Intent();
intent.putExtra("macAddress", asset.MAC_address);
intent.setAction(EVENT_NEW_ASSET);
sendBroadcast(intent);
}
private void notifyObserversOfLostAsset(Asset asset) {
Intent intent = new Intent();
intent.putExtra("macAddress", asset.MAC_address);
intent.setAction(EVENT_LOST_ASSET);
sendBroadcast(intent);
}
}
This code is not perfect and might even be buggy, but it will at least give you an idea or example of how this can be implemented.
I can recommend this approach:
Use Map<BluetoothDevice, Long> structure to store the discovered devices, where Long is the time of detection of the device (can be System.currentTimeMillis() for example).
Then in your service (as far as I understand from the question there will be implemented some kind of repeated task) just extract actual devices based on the time of their detection.
And you are absolutely right, there are no guarantee that all nearby devices were discovered within the allotted time. Especially this is actual for the Android devices.
iOS devices in it's turn have another issue - they can change their BluetoothDevice's adress in runtime without apparent external cause.
Hope this will help you to save the time during debugging.
Edit
As a result of research of this topic found this discussion on code.google.com
Issue is still open and seems that it is related to the hardware features and can't be fixed programmatically. Moreover, it seems that bug will remains on problem devices even after a system updates.
So restarting the scan periodically might be acceptable workaround for this case.

Android (Lollipop) cannot detect ongoing phone call from second SIM

I have a piece of code that makes phone calls and hangs up after a certain amount of time.
I've managed to make calls from both SIMs (using different tricks for the 2nd SIM), however, Android does not seem to be able to detect whether the 2nd SIM is off-hook;
Take a look at this piece of code:
Class<?> c = Class.forName(telMgr.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
ITelephony telephonyService = (ITelephony)m.invoke(telMgr);
if (telephonyService.isOffhook()) { // DO SOMETHING }
If the first SIM makes the call, I get isOffHook() to be true, but from the second SIM, the phone is in progress, but I get false.
Is there a way to detect if I'm off-hook on both SIMs?
Thanks
Thanks for the comments, but I have found a solution.
Rather than use old methodology of retrieving the ITelephony "instance" from the TelephonyManager (I used this trick in older versions cause other ways were making me troubles), I use the TelephonyManager directly by calling getCallState(), and it seems informative and accurate for both SIMs.
A code sample:
TelephonyManager telMgr = (TelephonyManager)(this.getMainContext()
.getSystemService(Context.TELEPHONY_SERVICE));
/* Making a call... */
if (telMgr.getCallState() != TelephonyManager.CALL_STATE_OFFHOOK) { /* Do your stuff */ }
Simple and straight forward. Working with my current 5.1 Lollipop version.

Detect the status of two SIM cards in a dual-SIM Android phone

I want to detect whether two SIM cards are there in my dual-SIM android phone programmatically. I found one API (TelephonyManager.getSIMState()), but it is for normal single-SIM phones. Are there any APIs to detect whether or not two SIMs are inserted in my dual-SIM phone?
Android does not support multiple SIMs, at least from the SDK. Device manufacturers who have created multi-SIM devices are doing so on their own. You are welcome to contact your device manufacturer and see if they have an SDK add-on or something that allows you to access the second SIM.
Edit: (15th July, 2015)
Since API 22, you can check for multiple SIMs using SubscriptionManager's method getActiveSubscriptionInfoList(). More details on Android Docs.
From now, if the phone is MTK powered one, you can use TelephonyManagerEx class from MediaTek SDK.
Take a look at the docs.
final SubscriptionManager subscriptionManager = SubscriptionManager.from(getApplicationContext());
final List<SubscriptionInfo> activeSubscriptionInfoList = subscriptionManager.getActiveSubscriptionInfoList();
int simCount = activeSubscriptionInfoList.size();
btnBack.setText(simCount+" Sim available");
Log.d("MainActivity: ","simCount:" +simCount);
for (SubscriptionInfo subscriptionInfo : activeSubscriptionInfoList) {
Log.d("MainActivity: ","iccId :"+ subscriptionInfo.getIccId()+" , name : "+ subscriptionInfo.getDisplayName());
}
Well, this is not fool proof. But if you have two SIMs which are on two different network operators you can try something like this:
PhoneServiceStateListener listener = new PhoneServiceStateListener(this);
tm.listen(listener, PhoneStateListener.LISTEN_SERVICE_STATE);
.
.
.
class PhoneServiceStateListener extends PhoneStateListener {
Context context = null;
public PhoneServiceStateListener(Context context) {
this.context = context;
}
public PhoneServiceStateListener() {
}
#Override
public void onServiceStateChanged(ServiceState serviceState) {
if (serviceState.getState() == ServiceState.STATE_IN_SERVICE) {
//You get this event when your SIM is in service.
//If you get this event twice, chances are more that your phone is Dual SIM.
//Alternatively, you can toggle Flight Mode programmatically twice so
//that you'll get service state changed event.
}
super.onServiceStateChanged(serviceState);
}
}
Ideally you'll get SIM service state changed event for both the SIMs and then you can check for network operator name or something like that to check if you have two SIM cards. But you need to have two SIM cards running on two different networks.

Categories

Resources