mirror of
https://gitee.com/openharmony/applications_app_samples
synced 2024-11-23 08:30:06 +00:00
commit
50ad666a2d
9
network/NFC/README.md
Normal file
9
network/NFC/README.md
Normal file
@ -0,0 +1,9 @@
|
||||
# NFC<a name="EN-US_TOPIC_0000001080439848"></a>
|
||||
|
||||
- Near-Field Communication \(NFC\) is a short-range wireless communications technology that allows mobile devices, consumer electronic devices, PCs, and smart devices to connect in countless ways.
|
||||
|
||||
Applications or other modules can implement the following functions through corresponding APIs:
|
||||
|
||||
- Checking whether a device supports NFC
|
||||
- Enabling or disabling NFC on a device
|
||||
|
10
network/NFC/README_zh.md
Normal file
10
network/NFC/README_zh.md
Normal file
@ -0,0 +1,10 @@
|
||||
# NFC<a name="ZH-CN_TOPIC_0000001080439848"></a>
|
||||
|
||||
- NFC(Near Field Communication,近距离无线通信技术) 是一种非接触式识别和互联技术,让移动设备、消费类电子产品、PC和智能设备之间可以进行近距离无线通信。
|
||||
|
||||
应用或者其他模块可以通过接口完成以下功能:
|
||||
|
||||
\* 查询本机是否支持NFC能力。
|
||||
|
||||
\* 开启或者关闭本机NFC。
|
||||
|
36
network/NFC/build.gradle
Normal file
36
network/NFC/build.gradle
Normal file
@ -0,0 +1,36 @@
|
||||
// Top-level build file where you can add configuration options common to all sub-projects/modules.
|
||||
apply plugin: 'com.huawei.ohos.app'
|
||||
|
||||
ohos {
|
||||
compileSdkVersion 5
|
||||
defaultConfig {
|
||||
compatibleSdkVersion 4
|
||||
}
|
||||
}
|
||||
|
||||
buildscript {
|
||||
repositories {
|
||||
maven {
|
||||
url 'https://repo.huaweicloud.com/repository/maven/'
|
||||
}
|
||||
maven {
|
||||
url 'https://developer.huawei.com/repo/'
|
||||
}
|
||||
jcenter()
|
||||
}
|
||||
dependencies {
|
||||
classpath 'com.huawei.ohos:hap:2.4.4.2'
|
||||
}
|
||||
}
|
||||
|
||||
allprojects {
|
||||
repositories {
|
||||
maven {
|
||||
url 'https://repo.huaweicloud.com/repository/maven/'
|
||||
}
|
||||
maven {
|
||||
url 'https://developer.huawei.com/repo/'
|
||||
}
|
||||
jcenter()
|
||||
}
|
||||
}
|
19
network/NFC/entry/build.gradle
Normal file
19
network/NFC/entry/build.gradle
Normal file
@ -0,0 +1,19 @@
|
||||
apply plugin: 'com.huawei.ohos.hap'
|
||||
ohos {
|
||||
compileSdkVersion 5
|
||||
defaultConfig {
|
||||
compatibleSdkVersion 4
|
||||
}
|
||||
buildTypes {
|
||||
release {
|
||||
proguardOpt {
|
||||
proguardEnabled false
|
||||
rulesFiles 'proguard-rules.pro'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation fileTree(dir: 'libs', include: ['*.jar', '*.har'])
|
||||
}
|
53
network/NFC/entry/src/main/config.json
Normal file
53
network/NFC/entry/src/main/config.json
Normal file
@ -0,0 +1,53 @@
|
||||
{
|
||||
"app": {
|
||||
"bundleName": "ohos.samples.nfcsample",
|
||||
"version": {
|
||||
"code": 1000000,
|
||||
"name": "1.0"
|
||||
}
|
||||
},
|
||||
"deviceConfig": {},
|
||||
"module": {
|
||||
"package": "ohos.samples.nfcsample",
|
||||
"name": ".MainAbility",
|
||||
"reqCapabilities": [
|
||||
"video_support"
|
||||
],
|
||||
"deviceType": [
|
||||
"default"
|
||||
],
|
||||
"distro": {
|
||||
"deliveryWithInstall": true,
|
||||
"moduleName": "entry",
|
||||
"moduleType": "entry",
|
||||
"installationFree":false
|
||||
},
|
||||
"abilities": [
|
||||
{
|
||||
"skills": [
|
||||
{
|
||||
"entities": [
|
||||
"entity.system.home"
|
||||
],
|
||||
"actions": [
|
||||
"action.system.home"
|
||||
]
|
||||
}
|
||||
],
|
||||
"orientation": "portrait",
|
||||
"formsEnabled": false,
|
||||
"name": ".MainAbility",
|
||||
"icon": "$media:icon",
|
||||
"description": "$string:mainability_description",
|
||||
"label": "$string:app_name",
|
||||
"type": "page",
|
||||
"launchType": "standard"
|
||||
}
|
||||
],
|
||||
"reqPermissions": [
|
||||
{
|
||||
"name": "ohos.permission.NFC_TAG"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Copyright (c) 2021 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package ohos.samples.nfcsample;
|
||||
|
||||
import ohos.samples.nfcsample.slice.MainAbilitySlice;
|
||||
|
||||
import ohos.aafwk.ability.Ability;
|
||||
import ohos.aafwk.content.Intent;
|
||||
|
||||
/**
|
||||
* MainAbility extends Ability
|
||||
*/
|
||||
public class MainAbility extends Ability {
|
||||
@Override
|
||||
public void onStart(Intent intent) {
|
||||
super.onStart(intent);
|
||||
super.setMainRoute(MainAbilitySlice.class.getName());
|
||||
}
|
||||
}
|
@ -0,0 +1,118 @@
|
||||
/*
|
||||
* Copyright (c) 2021 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package ohos.samples.nfcsample.slice;
|
||||
|
||||
import ohos.samples.nfcsample.ResourceTable;
|
||||
import ohos.samples.nfcsample.utils.LogUtil;
|
||||
|
||||
import ohos.aafwk.ability.AbilitySlice;
|
||||
import ohos.aafwk.content.Intent;
|
||||
import ohos.agp.components.Text;
|
||||
import ohos.event.commonevent.CommonEventData;
|
||||
import ohos.event.commonevent.CommonEventManager;
|
||||
import ohos.event.commonevent.CommonEventSubscribeInfo;
|
||||
import ohos.event.commonevent.CommonEventSubscriber;
|
||||
import ohos.event.commonevent.CommonEventSupport;
|
||||
import ohos.event.commonevent.MatchingSkills;
|
||||
import ohos.nfc.NfcController;
|
||||
import ohos.rpc.RemoteException;
|
||||
|
||||
/**
|
||||
* MainAbilitySlice extends AbilitySlice
|
||||
*/
|
||||
public class MainAbilitySlice extends AbilitySlice {
|
||||
private static final String TAG = MainAbilitySlice.class.getSimpleName();
|
||||
|
||||
private Text nfcStateText;
|
||||
|
||||
private NfcController nfcController;
|
||||
|
||||
private CommonEventSubscriber nfcEventSubscriber;
|
||||
|
||||
@Override
|
||||
public void onStart(Intent intent) {
|
||||
super.onStart(intent);
|
||||
super.setUIContent(ResourceTable.Layout_main_ability_slice);
|
||||
initComponents();
|
||||
initEvent();
|
||||
subscribe();
|
||||
}
|
||||
|
||||
private void initComponents() {
|
||||
nfcStateText = (Text) findComponentById(ResourceTable.Id_nfc_state_text);
|
||||
}
|
||||
|
||||
private void initEvent() {
|
||||
nfcController = NfcController.getInstance(this);
|
||||
onNfcStateChange(nfcController.getNfcState());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStop() {
|
||||
super.onStop();
|
||||
unsubscribe();
|
||||
}
|
||||
|
||||
private void onNfcStateChange(int stateCode) {
|
||||
switch (stateCode) {
|
||||
case NfcController.STATE_OFF: {
|
||||
nfcStateText.setText("NFC_DISABLED");
|
||||
break;
|
||||
}
|
||||
case NfcController.STATE_ON: {
|
||||
nfcStateText.setText("NFC_ENABLED");
|
||||
break;
|
||||
}
|
||||
case NfcController.STATE_TURNING_ON: {
|
||||
nfcStateText.setText("NFC_TURNING_ON");
|
||||
break;
|
||||
}
|
||||
case NfcController.STATE_TURNING_OFF: {
|
||||
nfcStateText.setText("NFC_TURNING_OFF");
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void subscribe() {
|
||||
MatchingSkills matchingSkills = new MatchingSkills();
|
||||
matchingSkills.addEvent(CommonEventSupport.COMMON_EVENT_NFC_ACTION_ADAPTER_STATE_CHANGED);
|
||||
CommonEventSubscribeInfo subscribeInfo = new CommonEventSubscribeInfo(matchingSkills);
|
||||
nfcEventSubscriber = new CommonEventSubscriber(subscribeInfo) {
|
||||
@Override
|
||||
public void onReceiveEvent(CommonEventData commonEventData) {
|
||||
if (NfcController.STATE_CHANGED.equals(commonEventData.getIntent().getAction())) {
|
||||
onNfcStateChange(nfcController.getNfcState());
|
||||
}
|
||||
}
|
||||
};
|
||||
try {
|
||||
CommonEventManager.subscribeCommonEvent(nfcEventSubscriber);
|
||||
} catch (RemoteException e) {
|
||||
LogUtil.error(TAG, "exception occurs when invoking subscribeCommonEvent");
|
||||
}
|
||||
}
|
||||
|
||||
private void unsubscribe() {
|
||||
try {
|
||||
CommonEventManager.unsubscribeCommonEvent(nfcEventSubscriber);
|
||||
} catch (RemoteException e) {
|
||||
LogUtil.error(TAG, "exception occurs when invoking unsubscribeCommonEvent");
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,78 @@
|
||||
/*
|
||||
* Copyright (c) 2021 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package ohos.samples.nfcsample.utils;
|
||||
|
||||
import ohos.hiviewdfx.HiLog;
|
||||
import ohos.hiviewdfx.HiLogLabel;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* LogUtil
|
||||
*/
|
||||
public class LogUtil {
|
||||
private static final String TAG_LOG = "LogUtil:";
|
||||
|
||||
private static final int DOMAIN_ID = 0xD000F00;
|
||||
|
||||
private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, DOMAIN_ID, LogUtil.TAG_LOG);
|
||||
|
||||
private static final String LOG_FORMAT = "%s: %s";
|
||||
|
||||
private LogUtil() {
|
||||
/* Do nothing */
|
||||
}
|
||||
|
||||
/**
|
||||
* Print debug log
|
||||
*
|
||||
* @param tag log tag
|
||||
* @param msg log message
|
||||
*/
|
||||
public static void debug(String tag, String msg) {
|
||||
HiLog.debug(LABEL_LOG, String.format(Locale.ROOT, LOG_FORMAT, tag, msg));
|
||||
}
|
||||
|
||||
/**
|
||||
* Print info log
|
||||
*
|
||||
* @param tag log tag
|
||||
* @param msg log message
|
||||
*/
|
||||
public static void info(String tag, String msg) {
|
||||
HiLog.info(LABEL_LOG, String.format(Locale.ROOT, LOG_FORMAT, tag, msg));
|
||||
}
|
||||
|
||||
/**
|
||||
* Print warn log
|
||||
*
|
||||
* @param tag log tag
|
||||
* @param msg log message
|
||||
*/
|
||||
public static void warn(String tag, String msg) {
|
||||
HiLog.warn(LABEL_LOG, String.format(Locale.ROOT, LOG_FORMAT, tag, msg));
|
||||
}
|
||||
|
||||
/**
|
||||
* Print error log
|
||||
*
|
||||
* @param tag log tag
|
||||
* @param msg log message
|
||||
*/
|
||||
public static void error(String tag, String msg) {
|
||||
HiLog.error(LABEL_LOG, String.format(Locale.ROOT, LOG_FORMAT, tag, msg));
|
||||
}
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
{
|
||||
"string": [
|
||||
{
|
||||
"name": "app_name",
|
||||
"value": "NFC"
|
||||
},
|
||||
{
|
||||
"name": "mainability_description",
|
||||
"value": "hap sample empty page"
|
||||
}
|
||||
]
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!--
|
||||
Copyright (c) 2021 Huawei Device Co., Ltd.
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
-->
|
||||
<DirectionalLayout xmlns:ohos="http://schemas.huawei.com/res/ohos"
|
||||
ohos:width="match_parent"
|
||||
ohos:height="match_parent"
|
||||
ohos:orientation="vertical">
|
||||
|
||||
<DirectionalLayout
|
||||
ohos:width="match_parent"
|
||||
ohos:height="match_content"
|
||||
ohos:orientation="horizontal"
|
||||
ohos:top_margin="20vp">
|
||||
|
||||
<Text
|
||||
ohos:text="NFC State:"
|
||||
ohos:height="match_content"
|
||||
ohos:width="match_content"
|
||||
ohos:text_size="20fp"/>
|
||||
|
||||
<Text
|
||||
ohos:id="$+id:nfc_state_text"
|
||||
ohos:text_color="#4169E1"
|
||||
ohos:text_alignment="right"
|
||||
ohos:weight="1"
|
||||
ohos:height="match_content"
|
||||
ohos:width="match_content"
|
||||
ohos:text_size="20fp"/>
|
||||
</DirectionalLayout>
|
||||
</DirectionalLayout>
|
BIN
network/NFC/entry/src/main/resources/base/media/icon.png
Normal file
BIN
network/NFC/entry/src/main/resources/base/media/icon.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 6.6 KiB |
BIN
network/NFC/screenshots/phone/NfcDisabled.png
Normal file
BIN
network/NFC/screenshots/phone/NfcDisabled.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 43 KiB |
BIN
network/NFC/screenshots/phone/NfcEnabled.png
Normal file
BIN
network/NFC/screenshots/phone/NfcEnabled.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 42 KiB |
BIN
network/NFC/screenshots/phone/NfcTurningOff.png
Normal file
BIN
network/NFC/screenshots/phone/NfcTurningOff.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 43 KiB |
BIN
network/NFC/screenshots/phone/NfcTurningOn.png
Normal file
BIN
network/NFC/screenshots/phone/NfcTurningOn.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 43 KiB |
1
network/NFC/settings.gradle
Normal file
1
network/NFC/settings.gradle
Normal file
@ -0,0 +1 @@
|
||||
include ':entry'
|
Loading…
Reference in New Issue
Block a user