diff --git a/network/NFC/README.md b/network/NFC/README.md new file mode 100644 index 0000000000000000000000000000000000000000..bb2a232aefedef86b1143848bffa47a1886803e0 --- /dev/null +++ b/network/NFC/README.md @@ -0,0 +1,9 @@ +# NFC + +- 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 + diff --git a/network/NFC/README_zh.md b/network/NFC/README_zh.md new file mode 100644 index 0000000000000000000000000000000000000000..432d21be7ec98fa419ecfff43e327124f86d78ce --- /dev/null +++ b/network/NFC/README_zh.md @@ -0,0 +1,10 @@ +# NFC + +- NFC(Near Field Communication,近距离无线通信技术) 是一种非接触式识别和互联技术,让移动设备、消费类电子产品、PC和智能设备之间可以进行近距离无线通信。 + + 应用或者其他模块可以通过接口完成以下功能: + + \* 查询本机是否支持NFC能力。 + + \* 开启或者关闭本机NFC。 + diff --git a/network/NFC/build.gradle b/network/NFC/build.gradle new file mode 100644 index 0000000000000000000000000000000000000000..91af1ca64995ab365fbf9f4d6b6c9a5f816d163f --- /dev/null +++ b/network/NFC/build.gradle @@ -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() + } +} \ No newline at end of file diff --git a/network/NFC/entry/build.gradle b/network/NFC/entry/build.gradle new file mode 100644 index 0000000000000000000000000000000000000000..83f59debbd68242b503a22270f4378d7843ca204 --- /dev/null +++ b/network/NFC/entry/build.gradle @@ -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']) +} \ No newline at end of file diff --git a/network/NFC/entry/src/main/config.json b/network/NFC/entry/src/main/config.json new file mode 100644 index 0000000000000000000000000000000000000000..af1be8ab17bb348e7b2e53a7e1a66a918f981a60 --- /dev/null +++ b/network/NFC/entry/src/main/config.json @@ -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" + } + ] + } +} \ No newline at end of file diff --git a/network/NFC/entry/src/main/java/ohos/samples/nfcsample/MainAbility.java b/network/NFC/entry/src/main/java/ohos/samples/nfcsample/MainAbility.java new file mode 100644 index 0000000000000000000000000000000000000000..477a131706befb08ec1c0f71e34a9e02b3f839b6 --- /dev/null +++ b/network/NFC/entry/src/main/java/ohos/samples/nfcsample/MainAbility.java @@ -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()); + } +} diff --git a/network/NFC/entry/src/main/java/ohos/samples/nfcsample/slice/MainAbilitySlice.java b/network/NFC/entry/src/main/java/ohos/samples/nfcsample/slice/MainAbilitySlice.java new file mode 100644 index 0000000000000000000000000000000000000000..8496d31c9ecf6bf7b247d25f51c02325cf803994 --- /dev/null +++ b/network/NFC/entry/src/main/java/ohos/samples/nfcsample/slice/MainAbilitySlice.java @@ -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"); + } + } +} diff --git a/network/NFC/entry/src/main/java/ohos/samples/nfcsample/utils/LogUtil.java b/network/NFC/entry/src/main/java/ohos/samples/nfcsample/utils/LogUtil.java new file mode 100644 index 0000000000000000000000000000000000000000..26aad8698b4147597ca51e53615b65e84638f25b --- /dev/null +++ b/network/NFC/entry/src/main/java/ohos/samples/nfcsample/utils/LogUtil.java @@ -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)); + } +} diff --git a/network/NFC/entry/src/main/resources/base/element/string.json b/network/NFC/entry/src/main/resources/base/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..dcd70ec80b5dd8f36b6dc9e18e02b50e91a121d3 --- /dev/null +++ b/network/NFC/entry/src/main/resources/base/element/string.json @@ -0,0 +1,12 @@ +{ + "string": [ + { + "name": "app_name", + "value": "NFC" + }, + { + "name": "mainability_description", + "value": "hap sample empty page" + } + ] +} \ No newline at end of file diff --git a/network/NFC/entry/src/main/resources/base/layout/main_ability_slice.xml b/network/NFC/entry/src/main/resources/base/layout/main_ability_slice.xml new file mode 100644 index 0000000000000000000000000000000000000000..9cf258a40d4736162b95732c00263a72c6a5234d --- /dev/null +++ b/network/NFC/entry/src/main/resources/base/layout/main_ability_slice.xml @@ -0,0 +1,42 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/network/NFC/entry/src/main/resources/base/media/icon.png b/network/NFC/entry/src/main/resources/base/media/icon.png new file mode 100644 index 0000000000000000000000000000000000000000..ce307a8827bd75456441ceb57d530e4c8d45d36c Binary files /dev/null and b/network/NFC/entry/src/main/resources/base/media/icon.png differ diff --git a/network/NFC/screenshots/phone/NfcDisabled.png b/network/NFC/screenshots/phone/NfcDisabled.png new file mode 100644 index 0000000000000000000000000000000000000000..2235df60d93432b5dc95bdbe1215b97c4747c78c Binary files /dev/null and b/network/NFC/screenshots/phone/NfcDisabled.png differ diff --git a/network/NFC/screenshots/phone/NfcEnabled.png b/network/NFC/screenshots/phone/NfcEnabled.png new file mode 100644 index 0000000000000000000000000000000000000000..bc648b1e1b996100bbd98c085d68e7d67d8b4a72 Binary files /dev/null and b/network/NFC/screenshots/phone/NfcEnabled.png differ diff --git a/network/NFC/screenshots/phone/NfcTurningOff.png b/network/NFC/screenshots/phone/NfcTurningOff.png new file mode 100644 index 0000000000000000000000000000000000000000..abdcfd8d897ba300de426bb9fe6f61d513c4bc31 Binary files /dev/null and b/network/NFC/screenshots/phone/NfcTurningOff.png differ diff --git a/network/NFC/screenshots/phone/NfcTurningOn.png b/network/NFC/screenshots/phone/NfcTurningOn.png new file mode 100644 index 0000000000000000000000000000000000000000..6eeac0cd4feb220f51010606247bff293b1c702b Binary files /dev/null and b/network/NFC/screenshots/phone/NfcTurningOn.png differ diff --git a/network/NFC/settings.gradle b/network/NFC/settings.gradle new file mode 100644 index 0000000000000000000000000000000000000000..4773db73233a570c2d0c01a22e75321acfbf7a07 --- /dev/null +++ b/network/NFC/settings.gradle @@ -0,0 +1 @@ +include ':entry'