Modern life is inseparable from the Internet. In public places such as restaurants and shopping malls, mobile phone connection to WiFi has always been a high-frequency usage scenario. Although free WiFi in public places is increasing, the process of connecting to the Internet is cumbersome. Some need to open the webpage to register or click on the advertisement link to access the Internet, and some require to download a specific app to connect.
So is there a more convenient way to connect to the Internet? " Scan the code to connect to the Internet " proposes a solution, merchants can place a QR code containing WiFi information in the store. Users can connect to WiFi by turning on the mobile phone camera and scanning the code. It also supports sharing the QR code with friends around them, making network access faster and more intuitive, and there is no need to worry about privacy leakage and useless information being pushed.
Show results
Implementation principle
Through the code generation and code scanning capabilities of the HMS Core unified code scanning service , the scenario of scanning code to connect to WiFi can be easily realized.
development practice
1. Build the scanning function
Development preparation
1.1 Configure Huawei Maven warehouse address
Configure the Maven repository address of the HMS Core SDK in "buildscript > repositories" in the project. Configure the Maven repository address of the HMS Core SDK in "allprojects > repositories".
buildscript {
repositories {
google()
jcenter()
// 配置HMS Core SDK的Maven仓地址。
maven {url 'https://developer.huawei.com/repo/'}
}
}
allprojects {
repositories {
google()
jcenter()
// 配置HMS Core SDK的Maven仓地址。
maven {url 'https://developer.huawei.com/repo/'}
}
}
Gradle 7.0版本后,“allprojects > repositories”配置已迁移到项目级“settings.gradle”文件中。“settings.gradle”文件配置示例如下:
dependencyResolutionManagement {
...
repositories {
google()
jcenter()
maven {url 'https://developer.huawei.com/repo/'}
}
}
1.2 Add compilation dependencies
Location app build.gradle
dependencies{
//Scan SDK
implementation 'com.huawei.hms:scan:2.3.0.300'
}
1.3 Code Obfuscation
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
<!--相机权限-->
<uses-permission android:name="android.permission.CAMERA" />
<!--文件读取权限-->
<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 Result of application for permission
@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方式扫码接口
// 参数说明:
// activity 请求扫码的activity
// requestCode 扫码请求的请求码,用于鉴定activity返回时是否从扫码界面返回。
// option 扫码格式选项,可以置为“null”
ScanUtil.startScan(this, REQUEST_CODE_SCAN_ONE, new HmsScanAnalyzerOptions.Creator().create());
}
}
1.7 The callback interface receives the scan code result, and both the camera scan code and the 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 != null) {
// 码值结果 rawResult自定义TextView
rawResult.setText(hmsScan.getOriginalValue());
}
}
}
2. Build code generation function
To build the scan code function, you also need to configure the Marven warehouse address, add compilation dependencies and configure the obfuscation script, 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 Apply for dynamic permission
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},requestCode);
2.3 Result of application for permission
@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:
Huawei Developer Alliance official website
Get development guidance document
To participate in developer discussions, please go to Reddit community
To download demo and sample code, please go to Github
For integration issues please go to Stack Overflow
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。