• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.connectedTag (有源标签)
2
3本模块提供有源标签的使用,包括初始化有源标签芯片、读取有源标签内容、写入内容到有源标签等。
4
5> **说明:**
6>
7> 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
8
9## 导入模块
10
11```js
12import connectedTag from '@ohos.connectedTag';
13```
14
15## connectedTag.init
16
17init(): boolean
18
19初始化有源标签芯片。
20
21**需要权限**:ohos.permission.NFC_TAG
22
23**系统能力**:SystemCapability.Communication.ConnectedTag
24
25**返回值:**
26
27| **类型** | **说明** |
28| -------- | -------- |
29| boolean | true:初始化成功, false:初始化失败。 |
30
31## connectedTag.uninit
32
33uninit(): boolean
34
35卸载有源标签芯片资源。
36
37**需要权限**:ohos.permission.NFC_TAG
38
39**系统能力**:SystemCapability.Communication.ConnectedTag
40
41**返回值:**
42
43| **类型** | **说明** |
44| -------- | -------- |
45| boolean | true:卸载操作成功, false:卸载操作失败。 |
46
47## connectedTag.readNdefTag
48
49readNdefTag(): Promise<string>
50
51读取有源标签内容,使用promise方式作为异步方法。
52
53**需要权限**:ohos.permission.NFC_TAG
54
55**系统能力**:SystemCapability.Communication.ConnectedTag
56
57**返回值:**
58
59| **类型** | **说明** |
60| -------- | -------- |
61| Promise<string> | 返回读取有源标签内容。 |
62
63**示例:**
64
65```js
66import connectedTag from '@ohos.connectedTag';
67
68connectedTag.readNdefTag().then((data) => {
69    console.log("connectedTag readNdefTag Promise data = " + data);
70}).catch((err)=> {
71    console.log("connectedTag readNdefTag Promise err: " + err);
72});
73```
74
75## connectedTag.readNdefTag
76
77readNdefTag(callback: AsyncCallback<string>): void
78
79读取有源标签内容,使用AsyncCallback方式作为异步方法。
80
81**需要权限**:ohos.permission.NFC_TAG
82
83**系统能力**:SystemCapability.Communication.ConnectedTag
84
85**参数:**
86
87| **参数名** | **类型** | **必填** | **说明** |
88| -------- | -------- | -------- | -------- |
89| callback | AsyncCallback<string> | 是 | 读取有源标签内容回调函数。 |
90
91**示例:**
92
93```js
94import connectedTag from '@ohos.connectedTag';
95
96connectedTag.readNdefTag((err, data)=> {
97    if (err) {
98        console.log("connectedTag readNdefTag AsyncCallback err: " + err);
99    } else {
100        console.log("connectedTag readNdefTag AsyncCallback data: " + data);
101    }
102});
103```
104
105## connectedTag.writeNdefTag
106
107writeNdefTag(data: string): Promise<void>
108
109写入内容到有源标签,使用promise方式作为异步方法。
110
111**需要权限**:ohos.permission.NFC_TAG
112
113**系统能力**:SystemCapability.Communication.ConnectedTag
114
115**参数:**
116
117| **参数名** | **类型** | **必填** | **说明** |
118| -------- | -------- | -------- | -------- |
119| data | string | 是 | 有源标签内容, 长度最大是1024个字节。 |
120
121**返回值:**
122
123| **类型** | **说明** |
124| -------- | -------- |
125| Promise<void> | 无返回值。 |
126
127**示例:**
128
129```js
130import connectedTag from '@ohos.connectedTag';
131
132var rawData = "010203"; // change it tobe correct.
133connectedTag.writeNdefTag(rawData).then(() => {
134    console.log("connectedTag writeNdefTag Promise success.");
135}).catch((err)=> {
136    console.log("connectedTag writeNdefTag Promise err: " + err);
137});
138```
139
140## connectedTag.writeNdefTag
141
142writeNdefTag(data: string, callback: AsyncCallback<void>): void
143
144写入内容到有源标签,使用AsyncCallback方式作为异步方法。
145
146**需要权限**:ohos.permission.NFC_TAG
147
148**系统能力**:SystemCapability.Communication.ConnectedTag
149
150**参数:**
151
152| **参数名** | **类型** | **必填** | **说明** |
153| -------- | -------- | -------- | -------- |
154| data | string | 是 | 有源标签内容, 长度最大是1024个字节。 |
155| callback | AsyncCallback<void> | 是 | 读取有源标签内容回调函数。 |
156
157**示例:**
158
159```js
160import connectedTag from '@ohos.connectedTag';
161
162var rawData = "010203"; // change it tobe correct.
163connectedTag.writeNdefTag(rawData, (err)=> {
164    if (err) {
165        console.log("connectedTag writeNdefTag AsyncCallback err: " + err);
166    } else {
167        console.log("connectedTag writeNdefTag AsyncCallback success.");
168    }
169});
170```
171
172## connectedTag.on('notify')
173
174on(type: "notify", callback: Callback<number>): void
175
176注册NFC场强状态事件。
177
178**需要权限**:ohos.permission.NFC_TAG
179
180**系统能力**:SystemCapability.Communication.ConnectedTag
181
182**参数:**
183
184| **参数名** | **类型** | **必填** | **说明** |
185| -------- | -------- | -------- | -------- |
186| type | string | 是 | 固定填"notify"字符串 |
187| callback | Callback<number> | 是 | 状态改变回调函数,返回值参见[NfcRfType](#nfcrftype)。 |
188
189## connectedTag.off('notify')
190
191off(type: "notify", callback?: Callback<number>): void
192
193取消NFC场强状态事件的注册。
194
195**需要权限**:ohos.permission.NFC_TAG
196
197**系统能力**:SystemCapability.Communication.ConnectedTag
198
199**参数:**
200
201| **参数名** | **类型** | **必填** | **说明** |
202| -------- | -------- | -------- | -------- |
203| type | string | 是 | 固定填"notify"字符串 |
204| callback | Callback<number> | 否 | 状态改变回调函数。如果callback不填,将“去注册”该事件关联的所有回调函数。|
205
206**示例:**
207
208```js
209import connectedTag from '@ohos.connectedTag';
210
211// Register event
212connectedTag.on("notify", (err, rfState)=> {
213    if (err) {
214        console.log("connectedTag on Callback err: " + err);
215    } else {
216        console.log("connectedTag on Callback rfState: " + rfState);
217    }
218});
219
220var initStatus = connectedTag.init();
221console.log("connectedTag init status: " + initStatus);
222
223// Add nfc connecected tag business oprations here...
224// connectedTag.writeNdefTag(rawData)
225// connectedTag.readNdefTag()
226
227var uninitStatus = connectedTag.uninit();
228console.log("connectedTag uninit status: " + uninitStatus);
229
230// Unregister event
231connectedTag.off("notify", (err, rfState)=> {
232    if (err) {
233        console.log("connectedTag off Callback err: " + err);
234    } else {
235        console.log("connectedTag off Callback rfState: " + rfState);
236    }
237});
238```
239
240## NfcRfType
241
242表示NFC场强状态的枚举。
243
244**系统能力**:SystemCapability.Communication.ConnectedTag
245
246| 名称 | 值 | 说明 |
247| -------- | -------- | -------- |
248| NFC_RF_LEAVE | 0 | NFC离场事件 |
249| NFC_RF_ENTER | 1 | NFC进场事件 |
250