Modern life is inseparable from the Internet. In restaurants, shopping malls and other public places, mobile phone WiFi connection has always been a high-frequency use scenario. Although there are more and more free WiFi in public places, the process of network connection is very troublesome. Some need to open the web page to register or click the advertising link to surf the Internet, while others require to download specific apps to connect.
So is there any more convenient way to connect to the Internet? “ Code scanning networking ”A solution is proposed, and merchants can place QR codes containing WiFi information in stores. Users can connect to WiFi by opening the mobile camera to scan the code. They also support sharing the QR code with their friends, which makes the network faster and more intuitive, and there is no need to worry about privacy disclosure, being pushed useless information and other problems.
Effect display
Implementation principle
adopt HMS Core unified code scanning service Code generation and code scanning ability, easily realize the scene of code scanning and WiFi connection.
Development practice
1, Build code scanning function
Development preparation
1.1 configure Huawei Maven warehouse address
Configure Maven repository address of HMS Core SDK in "buildscript > repositories" in the project. Configure Maven repository address of HMS Core SDK in "allprojects > repositories".
buildscript { repositories { google() jcenter() // Configure Maven warehouse address of HMS Core SDK. maven {url 'https://developer.huawei.com/repo/'} } } allprojects { repositories { google() jcenter() // Configure Maven warehouse address of HMS Core SDK. maven {url 'https://developer.huawei.com/repo/'} } } Gradle 7.0 After version“ allprojects > repositories"Configuration migrated to project level“ settings.gradle"File. “ settings.gradle"An example of file configuration is as follows: dependencyResolutionManagement { ... repositories { google() jcenter() maven {url 'https://developer.huawei.com/repo/'} } }
1.2 add compilation dependency
Location application build gradle
dependencies{ //Scan SDK implementation 'com.huawei.hms:scan:2.3.0.300' }
1.3 code confusion
Open the obfuscation configuration file "proguard-rules.pro" in the application level root directory and add the obfuscation configuration script that excludes 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.**{*;}
1.4 Manifest.xml add permissions
<!--Camera permissions--> <uses-permission android:name="android.permission.CAMERA" /> <!--File read permission--> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
1.5 apply for dynamic permission
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE}, requestCode);
1.6 application authority results
@Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (permissions == null || grantResults == null) { return; } // Successfully applied for permission or already has permission if (requestCode == CAMERA_REQ_CODE) { // Default View Mode code scanning interface // Parameter Description: // Activity the activity that requests code scanning // requestCode the request code of the code scanning request, which is used to identify whether the activity returns from the code scanning interface. // Option scanning format option, which can be set to "null" ScanUtil.startScan(this, REQUEST_CODE_SCAN_ONE, new HmsScanAnalyzerOptions.Creator().create()); } }
1.7 the callback interface receives the scanning result, and the camera scanning and imported picture scanning 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) { // Import picture scanning results HmsScan hmsScan = data.getParcelableExtra(ScanUtil.RESULT); if (hmsScan != null) { // Code value result rawResult custom TextView rawResult.setText(hmsScan.getOriginalValue()); } } }
2, Build code generation function
To build the code scanning function, you also need to configure the Marven warehouse address, add compilation dependency and configuration confusion scripts, refer to 1.1, 1.2 and 1.3 above
2.1 Manifest.xml add permissions
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
2.2 applying for dynamic permission
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},requestCode);
2.3 Application authority results
@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); } }
2.4 generate QR 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(); } }
2.5 save 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(); } }
For more details of HMS Core unified code scanning service, please refer to:
https://developer.huawei.com/...
Official website of Huawei developer Alliance
obtain Development guidance document
To participate in developer discussion, please go to Reddit community
Download the demo and sample code at Github
Solve integration problems Stack Overflow