Chitholian
Chitholian

Reputation: 472

How do get SIM slot index of inbox SMS in android by ContentProvider?

I am developing an app which dials some USSD and waits for SMS related to them. It uses both SIM slots in a dual-SIM phone. I need to identify which SIM (e.g. slot index) received a particular SMS. I can get it when received SMS using BroadcastReceiver. But in case I miss some broadcasts (e.g. user uninstalled the app -> SMS received -> app re-installed. In this scenario I must recheck messages after every start-up of my application process.

I am reading SMS using ContentProvider:

val cursor = resolver.query(Uri.parse("content://sms/inbox"), null, null, null, null)

In android docs, there is a column named sub_id (i.e. Telephony.TextBasedSmsColumns.SUBSCRIPTION_ID). But I cannot obtain that column from my testing device Xiaomi Redmi 5 Plus, MIUI Global 11.0.2, Android 8.1.0. It throws exception when I try to get that column using cursor.getString(c.getColumnIndexOrThrow(Telephony.TextBasedSmsColumns.SUBSCRIPTION_ID)) saying that only available columns are: [_id, thread_id, address, person, date, date_sent, protocol, read, status, type, reply_path_present, subject, body, service_center, locked, error_code, seen, timed, deleted, sync_state, marker, source, bind_id, mx_status, mx_id, mx_id_v2, out_time, account, sim_id, block_type, advanced_seen, b2c_ttl, b2c_numbers, fake_cell_type, url_risky_type, creator, favorite_date].

EDIT: Note that sim_id gives me larger values like 11, 13, 14 etc. I cannot map them to sim slots anyway.

Upvotes: 1

Views: 1222

Answers (1)

Chitholian
Chitholian

Reputation: 472

I got it finally, sim_id is equivalent to the documented sub_id column. Therefore, we need to be careful when using android Telephony API.

fun findSlotFromSubId(sm: SubscriptionManager, subId: Int): Int {
    try {
        for (s in sm.activeSubscriptionInfoList) {
            if (s.subscriptionId == subId) {
                return s.simSlotIndex
            }
        }
    } catch (e: SecurityException) {
        e.printStackTrace()
    }
    return -1
}

Upvotes: 4

Related Questions