[Huawei Developers] yagmur.kilic:
​

Sending SMS with Huawei Scan Kit​

​

This article is about sending sms with the Huawei scan kit.

Huawei Scan Kit Integration

Configure app information in AppGallery Connect

Integrate the HMS Core SDK

Assign permissions

Sending SMS with Huawei Scan Kit

Add Required Permissions for Barcode Scanning and sms sending to AndroidManifest.xml

<!--Camera permission-->
<uses-permission android:name="android.permission.CAMERA"/>

<!--Sms permission-->
<uses-permission android:name="android.permission.SEND_SMS" />

Dynamically request the permissions in the MainActivity.java file

private fun requestPermission(requestCode: Int) {
ActivityCompat.requestPermissions(
this,
arrayOf(Manifest.permission.CAMERA, Manifest.permission.SEND_SMS),
requestCode)
}

override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray){
if (grantResults.size < 2 || permissions == null || grantResults == null || grantResults[0] != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "missing permission", Toast.LENGTH_LONG).show()
return
}

if (requestCode == REQUEST_SMS) {
val options = HmsScanAnalyzerOptions
.Creator()
.setHmsScanTypes(HmsScan.SMS_FORM, HmsScan.DATAMATRIX_SCAN_TYPE)
.create()
ScanUtil.startScan(this,
REQUEST_CODE_SMS , options)
}
}

You can use the link for more detail about required permissions.

In line 12, scan process starts. HmsScanType value must set SMS_FORM for sending sms.

Sample barcode value is as follows.

Original value : smsto:18300000000 : Hello World!

Mobile number of an SMS message : 18300000000

SMS message content : Hello World!

Data need to be parsed in onActivityResult to get mobile number and message content.

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (data != null) {
when(resultCode){
Activity.RESULT_OK ->{
if(requestCode == REQUEST_CODE_SMS) {
val result : HmsScan? = data.getParcelableExtra(ScanUtil.RESULT)
val sms = result?.let { getMessage(it) }
sms?.let { sendMessage(it) }
}
}
}
}
}

private fun getMessage(result : HmsScan): HmsScan.SmsContent? {
return when(result?.smsContent == null) {
true -> null
false -> {
result.smsContent
}
}
}

You can use URI schemes for composing and sending messages.

private fun sendMessage (smsInfo: HmsScan.SmsContent) {
if(smsInfo == null){
Toast.makeText(this, "sms information could not found", Toast.LENGTH_LONG).show()
return
}

if(smsInfo.getDestPhoneNumber().isBlank() || smsInfo.getMsgContent().isBlank()) {
Toast.makeText(this, "incorrect phone number or message content", Toast.LENGTH_LONG).show()
return
}

val intent = Intent(Intent.ACTION_VIEW, Uri.fromParts("sms", smsInfo.destPhoneNumber, null))
intent.putExtra("sms_body", smsInfo.msgContent)
startActivity(intent)

}

Create an message intent with the sms content and mobile number obtained from the barcode. The correct scheme is “sms” for messaging.

Use Cases

Contact potential customers : You can add a QR code to the your card with the note “Scan this to contact us”. When the QR code is scaned, a pre-written message “I want to get information about your product” may appear.

Subscribe or purchase digital products : You can add a QR code to your web site so that the your product can be purchased using the QR code. When the QR code is scanned, a message that says “I want to buy discount coupon or subscribe your channel” may appear.

Conclusion

I hope this article helps you better understand the Huawei scan kit and its use cases.

Thanks for reading. If you have question, don’t hesitate to contact me.

References:​

Scan Kit - Code Scanning Service - HUAWEI Developer

Scan Kit scans and parses all major 1D and 2D barcodes and generates QR codes, helping you quickly build barcode scanning functions into your apps.

developer.huawei.com

List of URI schemes - Wikipedia

en.wikipedia.org

https://github.com/HMS-Core/hms-scan-demo