1/** 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import LogUtil from '../../../../../../../common/utils/src/main/ets/default/baseUtil/LogUtil'; 17import ConfigData from '../../../../../../../common/utils/src/main/ets/default/baseUtil/ConfigData'; 18import nfcController from '@ohos.nfc.controller'; 19 20const TAG = ConfigData.TAG + 'NfcModel: '; 21 22export class NfcModel { 23 private nfcStatus: boolean; 24 private isNfcEnabled: boolean; 25 26 /** 27 * register Nfc Status change 28 * @param callback 29 */ 30 registerNfcStatusObserver(callback) { 31 LogUtil.info(TAG + 'start register nfc status observer' ); 32 nfcController.on('nfcStateChange', (code) => { 33 if(code == nfcController.NfcState.STATE_OFF || code == nfcController.NfcState.STATE_ON) { 34 if (code == nfcController.NfcState.STATE_ON) { 35 this.isNfcEnabled = true; 36 this.nfcStatus = true; 37 } 38 if (code == nfcController.NfcState.STATE_OFF) { 39 this.isNfcEnabled = false; 40 this.nfcStatus = false; 41 } 42 AppStorage.SetOrCreate('isNfcEnabled', this.isNfcEnabled); 43 AppStorage.SetOrCreate('nfcStatus', this.nfcStatus); 44 LogUtil.info(TAG + 'nfc active status code : ' + code + " isNfcEnabled" + this.isNfcEnabled); 45 } 46 callback(code); 47 }) 48 LogUtil.info(TAG + 'end register nfc status observer' ); 49 } 50 51 /** 52 * check whether NFC is open 53 * return boolean. true is mean of NFC open, false is mean of NFC close 54 */ 55 isNfcOpen(): boolean { 56 const isOpen: boolean = nfcController.isNfcOpen(); 57 LogUtil.info(TAG + 'check nfc is open: ' + isOpen); 58 return isOpen; 59 } 60 61 /** 62 * open NFC 63 * return boolean. true is mean of NFC open success, false is mean of NFC open failed 64 */ 65 openNfc(): boolean { 66 let enableNfc = nfcController.openNfc(); 67 LogUtil.info(TAG + 'open nfc: ' + enableNfc); 68 return enableNfc; 69 } 70 71 /** 72 * close NFC 73 * return boolean. true is mean of NFC close success, false is mean of NFC close failed 74 */ 75 closeNfc(): boolean { 76 let disableNfc = nfcController.closeNfc(); 77 LogUtil.info(TAG + 'close nfc' + disableNfc); 78 return disableNfc; 79 } 80} 81 82let nfcModel = new NfcModel(); 83export default nfcModel as NfcModel;