• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2021-2022 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 NfcController from '@ohos.nfc.controller';
17import { BusinessError } from 'basic';
18import Log from '../../../../../../../../common/src/main/ets/default/Log';
19import createOrGet from '../../../../../../../../common/src/main/ets/default/SingleInstanceHelper';
20
21const TAG = 'NFCModel';
22
23export interface NFCModeStatusListener {
24  updateNFCMode(status: NfcController.NfcState): void;
25}
26
27export class NFCModeService {
28  mIsStart = false;
29  mListeners = new Set<NFCModeStatusListener>();
30  mNFCManager: any;
31
32  startService(): void {
33    if (this.mIsStart) {
34      return;
35    }
36    Log.showInfo(TAG, 'startService');
37    this.mIsStart = true;
38
39    this.mNFCManager = NfcController
40
41    this.getNFCMode();
42
43    this.mNFCManager.on('nfcStateChange', (data: NfcController.NfcState) => {
44      Log.showInfo(TAG, `startService->nfcStateChange, data: ${JSON.stringify(data)}`);
45      if(data == NfcController.NfcState.STATE_OFF || data == NfcController.NfcState.STATE_ON) {
46        this.mListeners.forEach(listener => listener.updateNFCMode(data));
47      }
48    });
49  }
50
51  stopService(): void {
52    if (!this.mIsStart) {
53      return;
54    }
55    Log.showInfo(TAG, 'stopService');
56    this.mIsStart = false;
57
58    this.mNFCManager = null;
59  }
60
61  registerListener(listener: NFCModeStatusListener): void {
62    let res = this.mListeners.add(listener);
63    Log.showInfo(TAG, `registser NfcState Listener ${res}`);
64  }
65
66  unregisterListener(listener: NFCModeStatusListener): void {
67    let res = this.mListeners.delete(listener);
68    Log.showInfo(TAG, `unregistser NfcState Listener ${res}`);
69  }
70
71  getNFCMode(): void {
72       let action = this.mNFCManager.getNfcState();
73       Log.showInfo(TAG, `getNFCMode${action}`);
74      this.mListeners.forEach(listener => listener.updateNFCMode(action));
75  }
76
77  setNFCMode(mode: NfcController.NfcState): void {
78    Log.showInfo(TAG, `setNFCMode, mode: ${JSON.stringify(mode)}`);
79    let isSucceed = false;
80    switch (mode) {
81      case NfcController.NfcState.STATE_OFF:
82        isSucceed = this.mNFCManager.openNfc();
83        Log.showInfo(TAG, `openNFC` + isSucceed);
84        break;
85      case NfcController.NfcState.STATE_ON:
86        isSucceed = this.mNFCManager.closeNfc();
87        Log.showInfo(TAG, `closeNFC` + isSucceed);
88        break;
89      default:
90        break;
91    }
92  }
93}
94
95let sNFCModeService = createOrGet(NFCModeService, TAG);
96
97export default sNFCModeService;