39 lines
1.4 KiB
Kotlin
39 lines
1.4 KiB
Kotlin
package us.beckmeyer.vvmsmsreceiver
|
|
|
|
import android.content.BroadcastReceiver
|
|
import android.content.Context
|
|
import android.content.Intent
|
|
import android.os.Bundle
|
|
import android.provider.Telephony
|
|
import android.telephony.SmsMessage
|
|
import android.util.Log
|
|
|
|
|
|
class SmsReceiver : BroadcastReceiver() {
|
|
|
|
private val TAG = "SmsReceiver"
|
|
|
|
@OptIn(ExperimentalStdlibApi::class)
|
|
override fun onReceive(context: Context, intent: Intent) {
|
|
if (intent.action == Telephony.Sms.Intents.SMS_RECEIVED_ACTION ||
|
|
intent.action == Telephony.Sms.Intents.DATA_SMS_RECEIVED_ACTION) {
|
|
val bundle: Bundle? = intent.extras
|
|
bundle?.let {
|
|
val pdus = bundle.get("pdus") as Array<ByteArray>
|
|
for (pdu in pdus) {
|
|
val smsMessage =
|
|
SmsMessage.createFromPdu(pdu, bundle.getString("format"))
|
|
smsMessage?.let {
|
|
if (intent.action == Telephony.Sms.Intents.DATA_SMS_RECEIVED_ACTION) {
|
|
Log.d(TAG, "Data SMS received on port 5499")
|
|
}
|
|
val messageBody = smsMessage.messageBody
|
|
Log.d(TAG, "Message body: $messageBody")
|
|
val pduHexString = pdu.toHexString()
|
|
Log.d(TAG, "PDU: $pduHexString")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |