Modern life is inseparable from the Internet. In restaurants, shopping malls and other public places, mobile phones connected to WiFi have always been used frequently. Free WiFi in public places is on the rise, but connecting to the Internet can be tricky. Some require a web page to register or click on an AD link, while others require a specific App to be downloaded.

So is there a more convenient way to connect? “Scan code network” proposed a solution, merchants can place a QR code containing WiFi information in the store. Users can connect to WiFi by scanning the camera code of their mobile phones, and can also share the QR code with friends around them, which makes the Internet access faster and more intuitive, and does not need to worry about privacy disclosure and useless information being pushed.

Results show

Realize the principle of

Through the code generation and code scanning capabilities of HMS Core unified code scanning service, it is easy to realize the scenario of scanning and WiFi connection.

The development of actual combat

First, build the scan function

The development of preparation

1.1 Configuring the Huawei Maven Warehouse Address

Configure the Maven repository for the HMS Core SDK in the project “BuildScript > Repositories”. Configure the Maven repository for the HMS Core SDK in AllProjects > Repositories.

Buildscript {repositories {Google () jCenter () // Configure Maven repository for HMS Core SDK. Allprojects maven {url 'https://developer.huawei.com/repo/'}}} {repositories {Google jcenter () () / / configure HMS Core Maven repository address for SDK. Maven {url 'https://developer.huawei.com/repo/'}}} Gradle version 7.0, Configuration "AllProjects > Repositories" has been migrated to the project level "Settings. gradle" file. "Settings. Gradle file example configuration is as follows:" dependencyResolutionManagement {... repositories { google() jcenter() maven {url 'https://developer.huawei.com/repo/'} } }Copy the code

1.2 Adding compile dependencies

Location application build.gradle

Dependencies {/ / Scan the SDK implementation 'com. Huawei. HMS: Scan: 2.3.0.300'}Copy the code

1.3 Code Confusion

Open the obfuscated configuration file Proguard-rules. pro in the application-level root directory and add the obfuscated configuration script to exclude the HMS Core SDK.

-ignorewarnings -keepattributes *Annotation* -keepattributes Exceptions -keepattributes InnerClasses -keepattributes Signature -keepattributes SourceFile,LineNumberTable -keep class com.huawei.hianalytics.**{*; } -keep class com.huawei.updatesdk.**{*; } -keep class com.huawei.hms.**{*; }Copy the code

1.4 Manifest.xml Add permissions

<! - CAMERA permissions - > < USES - permission android: name = "android. Permission. CAMERA" / > <! --> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />Copy the code

1.5 Applying for Dynamic Rights

ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE}, requestCode);
Copy the code

1.6 Permission Application Result

@Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (permissions == null || grantResults == null) { return; If (requestCode == CAMERA_REQ_CODE) {// Default View Mode Interface for scanning codes // // requestCode specifies the requestCode to check whether the activity returns from the scan interface. // option Scan format options, Can be set to "null" ScanUtil. StartScan (this, REQUEST_CODE_SCAN_ONE, new HmsScanAnalyzerOptions. The Creator (). The create ()); }}Copy the code

1.7 Callback Interface Receives the scan result. The camera scan code and imported image scan code are returned through this interface.

@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode ! = RESULT_OK || data == null) { return; } if (requestCode == REQUEST_CODE_SCAN_ONE) {HmsScan HmsScan = data.getParcelableExtra(scanutil.result); if (hmsScan ! TextView rawResult.settext (hmsscan.getoriginalValue ()); }}}Copy the code

Second, build code generation function

Building the sweep function also requires configuring the Marven warehouse address, adding compilation dependencies and configuration obfuscating scripts, see 1.1, 1.2 and 1.3 above

2.1 Manifest.xml Add permissions

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Copy the code

2.2 Applying for Dynamic Rights

ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},requestCode);
Copy the code

2.3 Permission Application Result

@Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { if (permissions == null || grantResults == null) { return; } if (grantResults[0] == PackageManager.PERMISSION_GRANTED && requestCode == GENERATE_CODE) { Intent intent = new Intent(this, GenerateCodeActivity.class); this.startActivity(intent); }}Copy the code

2.4 Generating TWO-DIMENSIONAL Code

public void generateCodeBtnClick(View v) {
        try {  
            HmsBuildBitmapOption options = new HmsBuildBitmapOption.Creator()
                    .setBitmapMargin(margin)
                    .setBitmapColor(color)
                    .setBitmapBackgroundColor(background)
                    .create();
            resultImage = ScanUtil.buildBitmap(content, type, width, height, options);
            barcodeImage.setImageBitmap(resultImage);

        } catch (WriterException e) {
            Toast.makeText(this, "Parameter Error!", Toast.LENGTH_SHORT).show();
        }

    }
Copy the code

2.5 Saving the QR Code

public void saveCodeBtnClick(View v) { if (resultImage == null) { Toast.makeText(GenerateCodeActivity.this, "Please generate barcode first!" , Toast.LENGTH_LONG).show(); return; } try { String fileName = System.currentTimeMillis() + ".jpg"; String storePath = Environment.getExternalStorageDirectory().getAbsolutePath(); File appDir = new File(storePath); if (! appDir.exists()) { appDir.mkdir(); } File file = new File(appDir, fileName); FileOutputStream fileOutputStream = new FileOutputStream(file); boolean isSuccess = resultImage.compress(Bitmap.CompressFormat.JPEG, 70, fileOutputStream); fileOutputStream.flush(); fileOutputStream.close(); Uri uri = Uri.fromFile(file); GenerateCodeActivity.this.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri)); if (isSuccess) { Toast.makeText(GenerateCodeActivity.this, "Barcode has been saved locally", Toast.LENGTH_LONG).show(); } else { Toast.makeText(GenerateCodeActivity.this, "Barcode save failed", Toast.LENGTH_SHORT).show(); } } catch (Exception e) { Toast.makeText(GenerateCodeActivity.this, "Unkown Error", Toast.LENGTH_SHORT).show(); }}Copy the code
For more details on HMS Core Unified Scan service, please refer to:

Developer.huawei.com/consumer/cn…

Huawei Developer Alliance official website obtain development guidance documents to participate in developer discussions. Download demo and sample code from Reddit. Visit Github to resolve integration problems