add js api for nfc tag and hce

Signed-off-by: l00438547 <liujiawei11@huawei.com>
This commit is contained in:
l00438547 2022-03-01 22:22:03 +08:00
parent f3f8cf5307
commit 80c20685e5
5 changed files with 610 additions and 0 deletions

105
api/@ohos.nfc.cardEmulation.js Executable file
View File

@ -0,0 +1,105 @@
/*
* Copyright (c) 2021-2022 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.
*/
/**
* Provides methods to operate or manage NFC card emulation.
*
* @import import cardEmulation from '@ohos.nfc.cardEmulation';
*
* @since 6
* @syscap SystemCapability.Communication.NFC.Core
*/
declare namespace cardEmulation {
enum Featuretype {
/** This constant is used to check whether HCE card emulation is supported. */
HCE = 0,
/** This constant is used to check whether SIM card emulation is supported. */
UICC = 1,
/** This constant is used to check whether eSE card emulation is supported. */
ESE = 2,
}
/**
* Checks whether a specified type of card emulation is supported.
*
* <p>This method is used to check Whether the host or secure element supports card emulation.
*
* @param feature Indicates the card emulation type, {@code HCE}, {@code UICC}, or {@code ESE}.
* @return Returns {@code true} if the specified type of card emulation is supported; returns
* {@code false} otherwise.
*
* @since 6
*/
function isSupported(feature: number):boolean;
/**
* A class for NFC host application.
*
* <p>The NFC host application use this class, then Nfc service can access the application
* installation information and connect to services of the application.
*
* @since 8
* @syscap SystemCapability.Communication.NFC.Core
*/
export class HceService {
/**
* start HCE
*
* @return Returns {@code true} if HCE is enabled or has been enabled; returns {@code false} otherwise.
* @permission ohos.permission.NFC_CARD_EMULATION
*
* @since 8
*/
startHCE(aidList: string[]): boolean;
/**
* stop HCE
*
* @return Returns {@code true} if HCE is disabled or has been disabled; returns {@code false} otherwise.
* @permission ohos.permission.NFC_CARD_EMULATION
*
* @since 8
*/
stopHCE(): boolean;
/**
* register HCE event to receive the APDU data.
*
* @param type the type to register.
* @param callback Callback used to listen for HCE data device received.
* @permission ohos.permission.NFC_CARD_EMULATION
*
* @since 8
*/
on(type: "hceCmd", callback: AsyncCallback<number[]>): void;
/**
* Sends a response APDU to the remote device.
*
* <p>This method is used by a host application when swiping card.
*
* @param responseApdu Indicates the response, which is a byte array.
* @permission ohos.permission.NFC_CARD_EMULATION
*
* @since 8
*/
sendResponse(responseApdu: number[]): void;
}
}
export default cardEmulation;

116
api/@ohos.nfc.controller.js Executable file
View File

@ -0,0 +1,116 @@
/*
* Copyright (c) 2021-2022 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.
*/
import { Callback } from './basic';
/**
* Provides methods to operate or manage NFC.
*
* @import import controller from '@ohos.nfc.controller';
*
* @since 7
* @syscap SystemCapability.Communication.NFC.Core
*/
declare namespace nfcController {
enum NfcState {
/** Indicates that NFC is disabled. */
STATE_OFF = 1,
/** Indicates that NFC is being enabled. */
STATE_TURNING_ON = 2,
/** Indicates that NFC is enabled. */
STATE_ON = 3,
/** Indicates that NFC is being disabled. */
STATE_TURNING_OFF = 4,
}
/**
* Checks whether a device supports NFC.
*
* @return Returns {@code true} if the device supports NFC; returns {@code false} otherwise.
*
* @since 7
*/
function isNfcAvailable(): boolean
/**
* register nfc state changed event.
*
* @param type the type to register.
* @param callback Callback used to listen for the nfc state changed event.
*
* @since 7
*/
function on(type: "nfcStateChange", callback: Callback<NfcState>): void
/**
* unregister nfc state changed event.
*
* @param type the type to unregister.
* @param callback Callback used to listen for the nfc state changed event.
*
* @since 7
*/
function off(type: "nfcStateChange", callback?: Callback<NfcState>): void
/**
* Enables NFC.
*
* @return Returns {@code true} if NFC is enabled or has been enabled; returns {@code false} otherwise.
* @permission ohos.permission.MANAGE_SECURE_SETTINGS
*
* @since 7
*/
function openNfc(): boolean
/**
* Disables NFC.
*
* @return Returns {@code true} if NFC is disabled or has been disabled; returns {@code false} otherwise.
* @permission ohos.permission.MANAGE_SECURE_SETTINGS
*
* @since 7
*/
function closeNfc(): boolean
/**
* Checks whether NFC is enabled.
*
* @return Returns {@code true} if NFC is enabled; returns {@code false} otherwise.
*
* @since 7
*/
function isNfcOpen(): boolean
/**
* Obtains the NFC status.
*
* <p>The NFC status can be any of the following: <ul><li>{@link #STATE_OFF}: Indicates that NFC
* is disabled. <li>{@link #STATE_TURNING_ON}: Indicates that NFC is being enabled.
* <li>{@link #STATE_ON}: Indicates that NFC is enabled. <li>{@link #STATE_TURNING_OFF}: Indicates
* that NFC is being disabled.</ul>
*
* @return Returns the NFC status.
*
* @since 7
*/
function getNfcState(): boolean
}
export default nfcController;

118
api/@ohos.nfc.tag.js Executable file
View File

@ -0,0 +1,118 @@
/*
* Copyright (c) 2021-2022 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.
*/
import { NfcATag, NfcBTag, NfcFTag, NfcVTag } from './tag/nfctech';
/**
* Provides methods to operate or manage NFC tag.
*
* @import import tag from '@ohos.nfc.tag';
*
* @since 7
* @sysCap SystemCapability.Communication.NFC.Core
*/
declare namespace tag {
/** Indicates an NFC-A tag. */
const NFC_A = 1;
/** Indicates an NFC-B tag. */
const NFC_B = 2;
/** Indicates an ISO-DEP tag. */
const ISO_DEP = 3;
/** Indicates an NFC-F tag. */
const NFC_F = 4;
/** Indicates an NFC-V tag. */
const NFC_V = 5;
/** Indicated an NDEF tag. */
const NDEF = 6;
/** Indicates a MifareClassic tag. */
const MIFARE_CLASSIC = 8;
/** Indicates a MifareUltralight tag. */
const MIFARE_ULTRALIGHT = 9;
/**
* Obtains an {@code NfcATag} object based on the tag information.
*
* <p>During tag reading, if the tag supports the NFC-A technology, an {@code NfcATag} object
* will be created based on the tag information.
*
* @param tagInfo Indicates the tag information.
* @permission ohos.permission.NFC_TAG
*
* @since 7
*/
function getNfcATag(tagInfo: TagInfo): NfcATag
/**
* Obtains an {@code NfcBTag} object based on the tag information.
*
* <p>During tag reading, if the tag supports the NFC-B technology, an {@code NfcBTag} object
* will be created based on the tag information.
*
* @param tagInfo Indicates the tag information.
* @permission ohos.permission.NFC_TAG
*
* @since 7
*/
function getNfcBTag(tagInfo: TagInfo): NfcBTag
/**
* Obtains an {@code NfcFTag} object based on the tag information.
*
* <p>During tag reading, if the tag supports the NFC-F technology, an {@code NfcFTag} object
* will be created based on the tag information.
*
* @param tagInfo Indicates the tag information.
* @permission ohos.permission.NFC_TAG
*
* @since 7
*/
function getNfcFTag(tagInfo: TagInfo): NfcFTag
/**
* Obtains an {@code NfcVTag} object based on the tag information.
*
* <p>During tag reading, if the tag supports the NFC-V technology, an {@code NfcVTag} object
* will be created based on the tag information.
*
* @param tagInfo Indicates the tag information.
* @permission ohos.permission.NFC_TAG
*
* @since 7
*/
function getNfcVTag(tagInfo: TagInfo): NfcVTag
/**
* Provides tag information.
*
* <p>This class provides the technology a tag supports, for example, NFC-A. Applications can create
* different tags based on the supported technology.
*
* @since 7
* @syscap SystemCapability.Communication.NFC.Core
* @permission ohos.permission.NFC_TAG
*/
export interface TagInfo {
supportedProfiles: number[];
}
}
export default tag;

139
api/tag/nfctech.js Executable file
View File

@ -0,0 +1,139 @@
/*
* Copyright (c) 2021-2022 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.
*/
import { TagSession } form './tagSession';
/**
* Provides interfaces to control the read and write of tags that support the NFC-A technology.
*
* <p>This class is inherited from the {@link TagSession} abstract class, and provides methods to create
* {@code NfcATag} objects and obtain the ATQA and SAK.
*
* @since 7
* @syscap SystemCapability.Communication.NFC.Core
*/
export interface NfcATag extends TagSession{
/**
* Obtains the SAK of an NFC-A tag.
*
* @return Returns the SAK of the NFC-A tag.
* @permission ohos.permission.NFC_TAG
*
* @since 7
*/
getSak():number;
/**
* Obtains the ATQA of an NFC-A tag.
*
* @return Returns the ATQA of the NFC-A tag.
* @permission ohos.permission.NFC_TAG
*
* @since 7
*/
getAtqa():number[];
}
/**
* Provides interfaces to create an {@code NfcBTag} and perform I/O operations on the tag.
*
* <p>This class inherits from the {@link TagSession} abstract class and provides interfaces to create an
* {@code NfcBTag} and obtain the tag information.
*
* @since 7
* @syscap SystemCapability.Communication.NFC.Core
*/
export interface NfcBTag extends TagSession{
/**
* Obtains the application data of a tag.
*
* @return Returns the application data of the tag.
* @permission ohos.permission.NFC_TAG
*
* @since 7
*/
getRespAppData():number[];
/**
* Obtains the protocol information of a tag.
*
* @return Returns the protocol information of the tag.
* @permission ohos.permission.NFC_TAG
*
* @since 7
*/
getRespProtocol():number[];
}
/**
* Provides methods for creating an NFC-F tag, obtaining tag information, and controlling tag read and write.
*
* <p>This class inherits from the {@link TagSession} abstract class and provides interfaces to create an
* {@code NfcFTag} and obtain the tag information.
*
* @since 7
* @syscap SystemCapability.Communication.NFC.Core
*/
export interface NfcFTag extends TagSession{
/**
* Obtains the system code from this {@code NfcFTag} instance.
*
* @return Returns the system code.
* @permission ohos.permission.NFC_TAG
*
* @since 7
*/
getSystemCode():number[];
/**
* Obtains the PMm (consisting of the IC code and manufacturer parameters) from this {@code NfcFTag} instance.
*
* @return Returns the PMm.
* @permission ohos.permission.NFC_TAG
*
* @since 7
*/
getPmm():number[];
}
/**
* Provides methods for creating an NFC-V tag, obtaining tag information, and controlling tag read and write.
*
* <p>This class inherits from the {@link TagSession} abstract class and provides interfaces to create an
* {@code NfcVTag} and obtain the tag information.
*
* @since 7
* @syscap SystemCapability.Communication.NFC.Core
*/
export interface NfcVTag extends TagSession{
/**
* Obtains the response flags from this {@code NfcVTag} instance.
*
* @return Returns the response flags.
* @permission ohos.permission.NFC_TAG
*
* @since 7
*/
getResponseFlags():number;
/**
* Obtains the data storage format identifier (DSFID) from this {@code NfcVTag} instance.
*
* @return Returns the DSFID.
* @permission ohos.permission.NFC_TAG
*
* @since 7
*/
getDsfId():number;
}

132
api/tag/tagSession.js Executable file
View File

@ -0,0 +1,132 @@
/*
* Copyright (c) 2021-2022 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.
*/
import tag from '../ohos.nfc.tag';
import { AsyncCallback } form './basic';
/**
* Controls tag read and write.
*
* <p>Classes for different types of tags inherit from this abstract class to control connections to
* tags, read data from tags, and write data to tags.
*
* @since 7
* @syscap SystemCapability.Communication.NFC.Core
*/
export interface TagSession {
/**
* Obtains the tag information.
*
* @return Returns the tag information, which is a {@link TagInfo} object.
* @permission ohos.permission.NFC_TAG
*
* @since 7
*/
getTagInfo(): tag.TagInfo;
/**
* Connects to a tag.
*
* <p>This method must be called before data is read from or written to the tag.
*
* @return Returns {@code true} if the connection is set up; returns {@code false} otherwise.
* @permission ohos.permission.NFC_TAG
*
* @since 7
*/
connectTag(): boolean;
/**
* Resets a connection with a tag and restores the default timeout duration for writing data to the tag.
*
* @permission ohos.permission.NFC_TAG
*
* @since 7
*/
reset(): void;
/**
* Checks whether a connection has been set up with a tag.
*
* @return Returns {@code true} if a connection has been set up with the tag;
* returns {@code false} otherwise.
*
* @permission ohos.permission.NFC_TAG
*
* @since 7
*/
isTagConnected(); boolean;
/**
* Sets the timeout duration (ms) for sending data to a tag.
*
* <p>If data is not sent to the tag within the duration, data sending fails.
*
* @param timeout Indicates the timeout duration to be set.
* @return Returns {@code true} if the setting is successful; returns {@code false} otherwise.
*
* @permission ohos.permission.NFC_TAG
*
* @since 7
*/
setSendDataTimeout(timeout: number): boolean;
/**
* Queries the timeout duration (ms) for sending data to a tag.
*
* @return Returns the timeout duration.
*
* @permission ohos.permission.NFC_TAG
*
* @since 7
*/
getSendDataTimeout(): number;
/**
* Writes data to a tag.
*
* @param data Indicates the data to be written to the tag.
* @return Returns bytes received in response. Or bytes with a length of 0 if the
* data fails to be written to the tag.
*
* @permission ohos.permission.NFC_TAG
*
* @since 7
*/
sendData(data: number[]): Promise<number[]>;
/**
* Writes data to a tag.
*
* @param data Indicates the data to be written to the tag.
* @return Returns bytes received in response. Or bytes with a length of 0 if the
* data fails to be written to the tag.
*
* @permission ohos.permission.NFC_TAG
*
* @since 7
*/
sendData(data: number[], callback: AsyncCallback<number[]>): void;
/**
* Queries the maximum length of data that can be sent to a tag.
*
* @return Returns the maximum length of the data to be sent to the tag.
*
* @permission ohos.permission.NFC_TAG
*
* @since 7
*/
getMaxSendLength(): number;
}