[Huawei Developers] yagmur.kilic:
Connecting to Wifi with Huawei Scan Kit​

This article is about connecting to the wireless network with the Huawei scan kit.

Huawei Scan Kit Integration

Configure app information in AppGallery Connect

Integrate the HMS Core SDK

Assign permissions

Connecting to Wifi with Huawei Scan Kit

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

<!--Network permission-->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />

Dynamically request the permissions in the MainActivity.java file

private fun requestPermission(requestCode: Int) {
ActivityCompat.requestPermissions (
this,
arrayOf(Manifest.permission.CAMERA, Manifest.permission.ACCESS_WIFI_STATE,
Manifest.permission.CHANGE_WIFI_STATE, Manifest.permission.ACCESS_NETWORK_STATE,
Manifest.permission.CHANGE_NETWORK_STATE),
requestCode)
}

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

if (requestCode == REQUEST_WIFI) {
val options = HmsScanAnalyzerOptions
.Creator()
.setHmsScanTypes(HmsScan.WIFI_CONNECT_INFO_FORM, HmsScan.DATAMATRIX_SCAN_TYPE)
.create()
ScanUtil.startScan(this,
REQUEST_CODE_WIFI_INFORMATION, options)

}
}

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

In line 12, scan process starts. HmsScanType value must set WIFI_CONNECT_INFO_FORM for getting information of wifi.

HmsScan.WiFiConnectionInfo

Sample barcode value is as follows.

Original value : WIFI:S:WIFI_123;T:WPA;P:12345678;;

Wi-Fi SSID : WIFI_123

Wi-Fi Password : 12345678

Wi-Fi encryption type : WPA

Data need to be parsed in onActivityResult to get password and ssid.

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_WIFI_INFORMATION) {
val result : HmsScan? = data.getParcelableExtra(ScanUtil.RESULT)
val wifiConnectionInfo = result?.let { getWifiInformation(it) }
wifiConnectionInfo?.let { connectToWifi(it) }

}
}
}
}
}

private fun getWifiInformation(result : HmsScan): HmsScan.WiFiConnectionInfo? {
return when(null == result || null == result.wiFiConnectionInfo == null) {
true -> null
false -> {
result.wiFiConnectionInfo
}
}
}

You can use the android.net library to connect to wifi using the password and ssid.

private fun connectToWifi(wifiConnectionInfo : HmsScan.WiFiConnectionInfo) {
if(wifiConnectionInfo == null){
Toast.makeText(this, "wifi information could not found", Toast.LENGTH_LONG).show()
return
}

if(wifiConnectionInfo.password.isBlank() || wifiConnectionInfo.password.isBlank()) {
Toast.makeText(this, "incorrect password or connection name", Toast.LENGTH_LONG).show()
return
}

val connectivityManager = this.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val wifiNetworkSpecifier = createWifiNetworkSpecifier(wifiConnectionInfo)
val networkRequest = createNetworkRequest(wifiNetworkSpecifier)
val networkCallback = createNetworkCallback()
networkRequest?.let { connectivityManager.requestNetwork(it, networkCallback) }
}

The first step is to create a WifiNetworkSpecifier with the ssid and password obtained from the barcode.

private fun createWifiNetworkSpecifier(wifiConnectionInfo: HmsScan.WiFiConnectionInfo): WifiNetworkSpecifier {
return WifiNetworkSpecifier
.Builder()
.setSsid(wifiConnectionInfo.ssidNumber)
.setWpa2Passphrase(wifiConnectionInfo.password)
.build()
}

The second step is to create a NetworkRequest with the wifiNetworkSpecifier created in the previous step.

private fun createNetworkRequest(wifiNetworkSpecifier : WifiNetworkSpecifier): NetworkRequest? {
return NetworkRequest.Builder()
.addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
.setNetworkSpecifier(wifiNetworkSpecifier)
.build()
}

NetworkCallback is base class for NetworkRequest. Used for notifications about network changes. Should be extended by applications wanting notifications.

private fun createNetworkCallback(): ConnectivityManager.NetworkCallback {
return object : ConnectivityManager.NetworkCallback() {
override fun onAvailable(network: Network) {
Log.i("ConnectToNetwork", "network is available")
}

override fun onUnavailable() {
Log.e("ConnectToNetwork", "network is unavailable")
}
}
}

If a network matching the ssid is found, the connect to wireless network option will be shown. After clicking connect button, If the password is correct, it will be successfully connected to the network.

Following alert will be shown, if a network matching the ssid cannot be found.

Use Cases

Sharing wifi with customers in the cafe and restaurant.

Sharing wifi with passengers in public transportation

Conclusion

I hope this article helps you better understand the usage scenarios of the Huawei scanning kit.

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

References:​

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

https://developer.android.com/reference/android/net/package-summary