现代生活离不开网络。在餐厅、商场等公共场所,手机连接WiFi一直是高频使用场景。虽然公共场所的免费WiFi越来越多,但网络连接过程却很麻烦。有的需要打开网页注册或点击广告链接才能上网,还有的要求下载特定App才能连接。
那么有没有什么更方便的联网方式呢?“扫码联网”提出了解决方案,商家可以在店放置包含WiFi信息的二维码。用户打开手机相机扫码即可连接WiFi,还支持将二维码分享给身边好友,使入网更快、更直观,并且无需担心隐私泄露、被推送无用信息等问题。
效果展示
实现原理
通过HMS Core统一扫码服务的码生成和扫码能力,轻松实现扫码连WiFi这一场景。
开发实战
一、构建扫码功能
开发准备
1.1配置华为Maven仓地址
在项目中“buildscript > repositories”中配置HMS Core SDK的Maven仓地址。在“allprojects > repositories”中配置HMS Core SDK的Maven仓地址。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添加编译依赖
位置应用build.gradledependencies{
//Scan SDK
implementation 'com.huawei.hms:scan:2.3.0.300'
} 1.3代码混淆
在应用级根目录下打开混淆配置文件“proguard-rules.pro”,加入排除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.4Manifest.xml添加权限<!--相机权限-->
<uses-permission android:name="android.permission.CAMERA" />
<!--文件读取权限-->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 1.5申请动态权限ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE}, requestCode); 1.6 申请权限结果@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 回调接口接收扫码结果,相机扫码和导入图片扫码均通过该接口返回。@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());
}
}
} 二、构建码生成功能
构建扫码功能同样需要配置Marven仓地址,添加编译依赖和配置混淆脚本,参考上方1.1,1.2和1.3
2.1 Manifest.xml添加权限<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 2.2 申请动态权限ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},requestCode); 2.3 申请权限结果@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 生成二维码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 保存二维码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();
}
} 欲了解更多HMS Core统一扫码服务详情,请参阅:
https://developer.huawei.com/consumer/cn/doc/development/HMSCore-Guides/service-introduction-0000001050041994?ha_source=hms1华为开发者联盟官网
获取开发指导文档
参与开发者讨论请到Reddit社区
下载demo和示例代码请到Github
解决集成问题请到Stack Overflow
|