vite-uni-dev-tool 0.0.48 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (292) hide show
  1. package/README.md +20 -0
  2. package/dist/const.cjs +1 -0
  3. package/dist/const.d.ts +78 -161
  4. package/dist/const.d.ts.map +1 -1
  5. package/dist/const.js +1 -47
  6. package/dist/core-shared.d.ts +44 -0
  7. package/dist/core-shared.d.ts.map +1 -0
  8. package/dist/core-shared.js +1 -0
  9. package/dist/core.d.ts +2 -40
  10. package/dist/core.d.ts.map +1 -1
  11. package/dist/core.js +4 -159
  12. package/dist/i18n/index.cjs +1 -0
  13. package/dist/i18n/index.d.ts +9 -0
  14. package/dist/i18n/index.d.ts.map +1 -0
  15. package/dist/i18n/index.js +1 -0
  16. package/dist/i18n/instance.cjs +1 -0
  17. package/dist/i18n/instance.d.ts +15 -0
  18. package/dist/i18n/instance.d.ts.map +1 -0
  19. package/dist/i18n/instance.js +1 -0
  20. package/dist/i18n/locales/en.cjs +1 -0
  21. package/dist/i18n/locales/en.d.ts +470 -0
  22. package/dist/i18n/locales/en.d.ts.map +1 -0
  23. package/dist/i18n/locales/en.js +1 -0
  24. package/dist/i18n/locales/zh-Hans.cjs +1 -0
  25. package/dist/i18n/locales/zh-Hans.d.ts +476 -0
  26. package/dist/i18n/locales/zh-Hans.d.ts.map +1 -0
  27. package/dist/i18n/locales/zh-Hans.js +1 -0
  28. package/dist/index.cjs +1 -0
  29. package/dist/index.d.ts.map +1 -1
  30. package/dist/index.js +1 -1
  31. package/dist/modules/devConsole/index.cjs +4 -0
  32. package/dist/{devConsole → modules/devConsole}/index.d.ts +1 -1
  33. package/dist/modules/devConsole/index.d.ts.map +1 -0
  34. package/dist/modules/devConsole/index.js +4 -0
  35. package/dist/modules/devEvent/index.cjs +5 -0
  36. package/dist/modules/devEvent/index.d.ts +115 -0
  37. package/dist/modules/devEvent/index.d.ts.map +1 -0
  38. package/dist/modules/devEvent/index.js +5 -0
  39. package/dist/modules/devIntercept/index.cjs +13 -0
  40. package/dist/modules/devIntercept/index.d.ts +71 -0
  41. package/dist/modules/devIntercept/index.d.ts.map +1 -0
  42. package/dist/modules/devIntercept/index.js +13 -0
  43. package/dist/modules/devStore/index.cjs +1 -0
  44. package/dist/modules/devStore/index.d.ts +159 -0
  45. package/dist/modules/devStore/index.d.ts.map +1 -0
  46. package/dist/modules/devStore/index.js +1 -0
  47. package/dist/modules/devToolInfo/index.cjs +1 -0
  48. package/dist/modules/devToolInfo/index.d.ts +27 -0
  49. package/dist/modules/devToolInfo/index.d.ts.map +1 -0
  50. package/dist/modules/devToolInfo/index.js +1 -0
  51. package/dist/plugins/uniDevTool/transform/transformApp.cjs +44 -0
  52. package/dist/plugins/uniDevTool/transform/transformApp.d.ts.map +1 -1
  53. package/dist/plugins/uniDevTool/transform/transformApp.js +17 -17
  54. package/dist/plugins/uniDevTool/transform/transformMain.cjs +3 -0
  55. package/dist/plugins/uniDevTool/transform/transformMain.d.ts.map +1 -1
  56. package/dist/plugins/uniDevTool/transform/transformMain.js +3 -3
  57. package/dist/plugins/uniDevTool/transform/transformVue.cjs +41 -0
  58. package/dist/plugins/uniDevTool/transform/transformVue.d.ts +1 -2
  59. package/dist/plugins/uniDevTool/transform/transformVue.d.ts.map +1 -1
  60. package/dist/plugins/uniDevTool/transform/transformVue.js +32 -19
  61. package/dist/plugins/uniDevTool/uniDevTool.cjs +5 -0
  62. package/dist/plugins/uniDevTool/uniDevTool.d.ts +3 -11
  63. package/dist/plugins/uniDevTool/uniDevTool.d.ts.map +1 -1
  64. package/dist/plugins/uniDevTool/uniDevTool.js +3 -3
  65. package/dist/plugins/uniGlobalComponents/uniGlobalComponents.cjs +19 -0
  66. package/dist/plugins/uniGlobalComponents/uniGlobalComponents.d.ts.map +1 -1
  67. package/dist/plugins/uniGlobalComponents/uniGlobalComponents.js +11 -19
  68. package/dist/plugins/uniParseStock/index.d.ts +10 -0
  69. package/dist/plugins/uniParseStock/index.d.ts.map +1 -0
  70. package/dist/plugins/utils/index.cjs +2 -0
  71. package/dist/plugins/utils/index.d.ts +1 -3
  72. package/dist/plugins/utils/index.d.ts.map +1 -1
  73. package/dist/plugins/utils/index.js +2 -2
  74. package/dist/shims-uni.d.ts +140 -0
  75. package/dist/type.d.ts +174 -16
  76. package/dist/type.d.ts.map +1 -1
  77. package/dist/utils/array.cjs +1 -0
  78. package/dist/utils/array.d.ts.map +1 -1
  79. package/dist/utils/array.js +1 -12
  80. package/dist/utils/date.cjs +1 -0
  81. package/dist/utils/date.d.ts.map +1 -1
  82. package/dist/utils/date.js +1 -59
  83. package/dist/utils/file.cjs +1 -0
  84. package/dist/utils/file.d.ts.map +1 -1
  85. package/dist/utils/file.js +1 -85
  86. package/dist/utils/function.cjs +1 -0
  87. package/dist/utils/function.d.ts +13 -0
  88. package/dist/utils/function.d.ts.map +1 -1
  89. package/dist/utils/function.js +1 -68
  90. package/dist/utils/index.cjs +1 -0
  91. package/dist/utils/index.d.ts.map +1 -1
  92. package/dist/utils/index.js +1 -63
  93. package/dist/utils/ip.cjs +1 -0
  94. package/dist/utils/ip.d.ts +1 -1
  95. package/dist/utils/ip.d.ts.map +1 -1
  96. package/dist/utils/ip.js +1 -45
  97. package/dist/utils/language.cjs +1 -0
  98. package/dist/utils/language.d.ts +2 -1
  99. package/dist/utils/language.d.ts.map +1 -1
  100. package/dist/utils/language.js +1 -63
  101. package/dist/utils/object.cjs +1 -0
  102. package/dist/utils/object.d.ts +4 -5
  103. package/dist/utils/object.d.ts.map +1 -1
  104. package/dist/utils/object.js +1 -136
  105. package/dist/utils/openLink.cjs +1 -0
  106. package/dist/utils/openLink.d.ts.map +1 -1
  107. package/dist/utils/openLink.js +1 -28
  108. package/dist/utils/page.cjs +1 -0
  109. package/dist/utils/page.d.ts.map +1 -1
  110. package/dist/utils/page.js +1 -7
  111. package/dist/utils/platform.cjs +1 -0
  112. package/dist/utils/platform.d.ts.map +1 -1
  113. package/dist/utils/platform.js +1 -17
  114. package/dist/utils/string.cjs +1 -0
  115. package/dist/utils/string.d.ts.map +1 -1
  116. package/dist/utils/string.js +1 -114
  117. package/dist/utils/utils.cjs +1 -0
  118. package/dist/utils/utils.d.ts +1 -1
  119. package/dist/utils/utils.d.ts.map +1 -1
  120. package/dist/utils/utils.js +1 -126
  121. package/dist/v3/AppTransition/index.vue +170 -0
  122. package/dist/v3/AutoSizer/index.vue +4 -5
  123. package/dist/v3/AutoSizer/index1.vue +4 -6
  124. package/dist/v3/AutoSizer/utils.ts +1 -1
  125. package/dist/v3/CaptureScreen/index.vue +19 -8
  126. package/dist/v3/CircularButton/index.vue +64 -13
  127. package/dist/v3/ConsoleList/ConsoleItem.vue +56 -5
  128. package/dist/v3/ConsoleList/RunJSInput.vue +10 -6
  129. package/dist/v3/ConsoleList/index.vue +51 -35
  130. package/dist/v3/CustomSwiper/CustomSwiperItem.vue +49 -0
  131. package/dist/v3/CustomSwiper/index.vue +104 -0
  132. package/dist/v3/DevTool/fps-collector.render.ts +148 -0
  133. package/dist/v3/DevTool/index.vue +209 -102
  134. package/dist/v3/DevToolButton/index.vue +40 -59
  135. package/dist/v3/DevToolTitle/index.vue +4 -1
  136. package/dist/v3/DevToolWindow/DevToolOverlay.vue +182 -0
  137. package/dist/v3/DevToolWindow/const.ts +47 -69
  138. package/dist/v3/DevToolWindow/hooks/dataUtils.ts +48 -0
  139. package/dist/v3/DevToolWindow/hooks/useDevToolData.ts +338 -0
  140. package/dist/v3/DevToolWindow/hooks/useDevToolHandlers.ts +549 -0
  141. package/dist/v3/DevToolWindow/hooks/useDevToolOverlay.ts +184 -0
  142. package/dist/v3/DevToolWindow/index.css +89 -0
  143. package/dist/v3/DevToolWindow/index.vue +800 -1450
  144. package/dist/v3/DraggableContainer/index.vue +456 -0
  145. package/dist/v3/ElEvent/ElEventItem.vue +4 -3
  146. package/dist/v3/ElEvent/index.vue +26 -9
  147. package/dist/v3/Empty/index.vue +1 -0
  148. package/dist/v3/FilterInput/index.vue +8 -22
  149. package/dist/v3/FilterSelect/index.vue +37 -24
  150. package/dist/v3/Instance/components/InstanceTreeNode.vue +265 -0
  151. package/dist/v3/Instance/flatten.ts +226 -0
  152. package/dist/v3/Instance/index.vue +94 -0
  153. package/dist/v3/Instance/registry.ts +49 -0
  154. package/dist/v3/Instance/transformTree.ts +375 -0
  155. package/dist/v3/Instance/transformTreeCtx.ts +268 -0
  156. package/dist/v3/{InstanceTree → Instance}/typing.d.ts +15 -2
  157. package/dist/v3/InstanceDetail/index.vue +433 -44
  158. package/dist/v3/JsonDetail/index.vue +12 -8
  159. package/dist/v3/JsonPretty/components/Brackets/index.vue +1 -0
  160. package/dist/v3/JsonPretty/components/Carets/index.vue +2 -1
  161. package/dist/v3/JsonPretty/components/CheckController/index.vue +26 -15
  162. package/dist/v3/JsonPretty/components/TreeNode/index.vue +42 -20
  163. package/dist/v3/JsonPretty/index.vue +60 -37
  164. package/dist/v3/JsonPretty/utils/index.ts +41 -44
  165. package/dist/v3/MovableContainer/index.vue +89 -0
  166. package/dist/v3/NFCList/NFCItem.vue +14 -14
  167. package/dist/v3/NFCList/NFCTool.vue +99 -81
  168. package/dist/v3/NFCList/index.vue +14 -10
  169. package/dist/v3/NetworkList/InterceptConfig.vue +154 -364
  170. package/dist/v3/NetworkList/InterceptItem.vue +14 -6
  171. package/dist/v3/NetworkList/NetworkDetail.vue +34 -29
  172. package/dist/v3/NetworkList/NetworkIntercept.vue +11 -8
  173. package/dist/v3/NetworkList/NetworkItem.vue +27 -11
  174. package/dist/v3/NetworkList/NetworkSend.vue +132 -290
  175. package/dist/v3/NetworkList/hooks/useNetworkForm.ts +86 -0
  176. package/dist/v3/NetworkList/index.vue +32 -24
  177. package/dist/v3/NetworkList/utils.ts +101 -0
  178. package/dist/v3/Performance/index.vue +495 -0
  179. package/dist/v3/Performance/modules/PerformanceMetrics.vue +153 -0
  180. package/dist/v3/Performance/modules/PerformanceWidget.vue +249 -0
  181. package/dist/v3/Performance/modules/usePerformanceChart.ts +460 -0
  182. package/dist/v3/Performance/modules/usePerformanceData.ts +258 -0
  183. package/dist/v3/Pick/index.vue +322 -0
  184. package/dist/v3/PiniaList/index.vue +38 -17
  185. package/dist/v3/RouteList/index.vue +32 -15
  186. package/dist/v3/RunJS/index.vue +30 -10
  187. package/dist/v3/ScanCodeList/ScanCodeItem.vue +2 -9
  188. package/dist/v3/ScanCodeList/index.vue +12 -9
  189. package/dist/v3/SettingButton/index.vue +45 -0
  190. package/dist/v3/SettingList/index.css +120 -0
  191. package/dist/v3/SettingList/index.vue +96 -537
  192. package/dist/v3/SettingList/modules/SettingDevTool.vue +208 -0
  193. package/dist/v3/SettingList/modules/SettingInfo.vue +119 -0
  194. package/dist/v3/SettingList/modules/SettingLanguage.vue +74 -0
  195. package/dist/v3/SettingList/modules/SettingLog.vue +230 -0
  196. package/dist/v3/SettingList/modules/SettingNetwork.vue +120 -0
  197. package/dist/v3/SettingList/modules/SettingTheme.vue +312 -0
  198. package/dist/v3/SourceCode/Line.vue +15 -2
  199. package/dist/v3/SourceCode/index.vue +25 -24
  200. package/dist/v3/SourceCode/parseCode.ts +114 -56
  201. package/dist/v3/StorageList/index.vue +33 -33
  202. package/dist/v3/Tabs/index.vue +31 -28
  203. package/dist/v3/Tag/index.vue +39 -20
  204. package/dist/v3/TransferList/TransferDetail.vue +268 -0
  205. package/dist/v3/TransferList/TransferItem.vue +125 -0
  206. package/dist/v3/TransferList/index.vue +181 -0
  207. package/dist/v3/UniEvent/UniEventItem.vue +1 -0
  208. package/dist/v3/UniEvent/index.vue +28 -29
  209. package/dist/v3/{VirtualListPro → VirtualList}/AutoSize.vue +1 -1
  210. package/dist/v3/VirtualList/index.vue +382 -82
  211. package/dist/v3/VuexList/index.vue +38 -17
  212. package/dist/v3/WebSocket/WebSocketDetail.vue +142 -80
  213. package/dist/v3/WebSocket/WebSocketItem.vue +10 -4
  214. package/dist/v3/WebSocket/index.vue +44 -30
  215. package/dist/v3/hooks/useNFC/index.ts +6 -6
  216. package/dist/v3/hooks/useNFC/typing.d.ts +2 -2
  217. package/dist/v3/hooks/useNFC/useNFCAndroid.ts +215 -626
  218. package/dist/v3/hooks/useNFC/useNFCMpWeiXin.ts +54 -85
  219. package/dist/v3/hooks/useNFC/utils.ts +3 -3
  220. package/dist/v3/hooks/useRequest/index.ts +2 -2
  221. package/dist/v3/hooks/useScanCode/index.ts +10 -8
  222. package/dist/v3/styles/theme.css +290 -0
  223. package/dist/v3/styles/theme.ts +12 -0
  224. package/package.json +25 -5
  225. package/dist/devConsole/index.d.ts.map +0 -1
  226. package/dist/devConsole/index.js +0 -273
  227. package/dist/devEvent/index.d.ts +0 -328
  228. package/dist/devEvent/index.d.ts.map +0 -1
  229. package/dist/devEvent/index.js +0 -697
  230. package/dist/devEventBus/index.d.ts +0 -42
  231. package/dist/devEventBus/index.d.ts.map +0 -1
  232. package/dist/devEventBus/index.js +0 -70
  233. package/dist/devIntercept/index.d.ts +0 -281
  234. package/dist/devIntercept/index.d.ts.map +0 -1
  235. package/dist/devIntercept/index.js +0 -949
  236. package/dist/devRunJS/index.d.ts +0 -67
  237. package/dist/devRunJS/index.d.ts.map +0 -1
  238. package/dist/devStore/index.d.ts +0 -199
  239. package/dist/devStore/index.d.ts.map +0 -1
  240. package/dist/devStore/index.js +0 -562
  241. package/dist/devToolInfo/index.d.ts +0 -17
  242. package/dist/devToolInfo/index.d.ts.map +0 -1
  243. package/dist/devToolInfo/index.js +0 -15
  244. package/dist/v3/AutoSizer/utils.d.ts +0 -5
  245. package/dist/v3/AutoSizer/utils.d.ts.map +0 -1
  246. package/dist/v3/ConsoleList/staticTips.d.ts +0 -13
  247. package/dist/v3/ConsoleList/staticTips.d.ts.map +0 -1
  248. package/dist/v3/DevToolWindow/const.d.ts +0 -46
  249. package/dist/v3/DevToolWindow/const.d.ts.map +0 -1
  250. package/dist/v3/InstanceTree/components/InstanceTreeNode.vue +0 -81
  251. package/dist/v3/InstanceTree/flatten.d.ts +0 -10
  252. package/dist/v3/InstanceTree/flatten.d.ts.map +0 -1
  253. package/dist/v3/InstanceTree/flatten.ts +0 -75
  254. package/dist/v3/InstanceTree/index.vue +0 -51
  255. package/dist/v3/InstanceTree/transformTree.d.ts +0 -17
  256. package/dist/v3/InstanceTree/transformTree.d.ts.map +0 -1
  257. package/dist/v3/InstanceTree/transformTree.ts +0 -237
  258. package/dist/v3/JsonPretty/hooks/useClipboard.d.ts +0 -4
  259. package/dist/v3/JsonPretty/hooks/useClipboard.d.ts.map +0 -1
  260. package/dist/v3/JsonPretty/hooks/useError.d.ts +0 -8
  261. package/dist/v3/JsonPretty/hooks/useError.d.ts.map +0 -1
  262. package/dist/v3/JsonPretty/type.d.ts +0 -94
  263. package/dist/v3/JsonPretty/type.d.ts.map +0 -1
  264. package/dist/v3/JsonPretty/utils/index.d.ts +0 -10
  265. package/dist/v3/JsonPretty/utils/index.d.ts.map +0 -1
  266. package/dist/v3/NFCList/const.d.ts +0 -13
  267. package/dist/v3/NFCList/const.d.ts.map +0 -1
  268. package/dist/v3/NetworkList/const.d.ts +0 -5
  269. package/dist/v3/NetworkList/const.d.ts.map +0 -1
  270. package/dist/v3/SourceCode/parseCode.d.ts +0 -27
  271. package/dist/v3/SourceCode/parseCode.d.ts.map +0 -1
  272. package/dist/v3/UploadList/UploadDetail.vue +0 -255
  273. package/dist/v3/UploadList/UploadItem.vue +0 -122
  274. package/dist/v3/UploadList/index.vue +0 -125
  275. package/dist/v3/VirtualListPro/index.vue +0 -229
  276. package/dist/v3/hooks/useContainerStyle.d.ts +0 -15
  277. package/dist/v3/hooks/useContainerStyle.d.ts.map +0 -1
  278. package/dist/v3/hooks/useNFC/index.d.ts +0 -5
  279. package/dist/v3/hooks/useNFC/index.d.ts.map +0 -1
  280. package/dist/v3/hooks/useNFC/useNFCAndroid.d.ts +0 -2
  281. package/dist/v3/hooks/useNFC/useNFCAndroid.d.ts.map +0 -1
  282. package/dist/v3/hooks/useNFC/useNFCMpWeiXin.d.ts +0 -2
  283. package/dist/v3/hooks/useNFC/useNFCMpWeiXin.d.ts.map +0 -1
  284. package/dist/v3/hooks/useNFC/utils.d.ts +0 -95
  285. package/dist/v3/hooks/useNFC/utils.d.ts.map +0 -1
  286. package/dist/v3/hooks/useRequest/index.d.ts +0 -126
  287. package/dist/v3/hooks/useRequest/index.d.ts.map +0 -1
  288. package/dist/v3/hooks/useRequest/utils.d.ts +0 -35
  289. package/dist/v3/hooks/useRequest/utils.d.ts.map +0 -1
  290. package/dist/v3/hooks/useScanCode/index.d.ts +0 -36
  291. package/dist/v3/hooks/useScanCode/index.d.ts.map +0 -1
  292. /package/dist/v3/{VirtualListPro → VirtualList}/readme.md +0 -0
@@ -16,48 +16,36 @@ import {
16
16
  generateWSCBytesForAndroid,
17
17
  } from './utils';
18
18
 
19
- const NfcAdapter: any = plus.android.importClass('android.nfc.NfcAdapter');
20
- const Context = plus.android.importClass('android.content.Context');
21
- const Tag = plus.android.importClass('android.nfc.Tag');
22
- const Ndef: any = plus.android.importClass('android.nfc.tech.Ndef');
23
- const NfcA: any = plus.android.importClass('android.nfc.tech.NfcA');
24
- const NfcB: any = plus.android.importClass('android.nfc.tech.NfcB');
25
- const NfcF: any = plus.android.importClass('android.nfc.tech.NfcF');
26
- const NfcV: any = plus.android.importClass('android.nfc.tech.NfcV');
27
- const NdefFormat = plus.android.importClass('android.nfc.tech.NdefFormat');
28
- const NdefFormatable: any = plus.android.importClass(
29
- 'android.nfc.tech.NdefFormatable',
30
- );
31
- const MifareClassic: any = plus.android.importClass(
32
- 'android.nfc.tech.MifareClassic',
33
- );
34
- const MifareUltralight: any = plus.android.importClass(
35
- 'android.nfc.tech.MifareUltralight',
36
- );
37
- const IsoDep: any = plus.android.importClass('android.nfc.tech.IsoDep');
38
- const Intent: any = plus.android.importClass('android.content.Intent');
39
- const PendingIntent: any = plus.android.importClass(
40
- 'android.app.PendingIntent',
41
- );
42
- const IntentFilter: any = plus.android.importClass(
43
- 'android.content.IntentFilter',
44
- );
45
- const Settings: any = plus.android.importClass('android.provider.Settings');
46
- /**
47
- * https://developer.android.google.cn/reference/kotlin/android/nfc/NdefRecord?hl=en
48
- */
49
- const NdefRecord: any = plus.android.importClass('android.nfc.NdefRecord');
50
- const NdefMessage: any = plus.android.importClass('android.nfc.NdefMessage');
51
-
52
- const INTENT_FLAGS_SINGLE_TOP = Intent.FLAG_ACTIVITY_SINGLE_TOP;
53
- const PENDING_INTENT_FLAGS = PendingIntent.FLAG_MUTABLE;
54
-
55
- const EXTRA_NDEF_MESSAGES = NfcAdapter.EXTRA_NDEF_MESSAGES;
56
- const EXTRA_ID = NfcAdapter.EXTRA_ID;
57
- const EXTRA_TAG = NfcAdapter.EXTRA_TAG;
58
-
59
- const TNF_MIME_MEDIA = NdefRecord.TNF_MIME_MEDIA;
60
- const ACTION_NFC_SETTINGS = Settings.ACTION_NFC_SETTINGS;
19
+ // 声明变量,但不立即初始化
20
+ let NfcAdapter: any;
21
+ let _Context: any;
22
+ let _Tag: any;
23
+ let Ndef: any;
24
+ let NfcA: any;
25
+ let NfcB: any;
26
+ let NfcF: any;
27
+ let NfcV: any;
28
+ let _NdefFormat: any;
29
+ let NdefFormatable: any;
30
+ let MifareClassic: any;
31
+ let MifareUltralight: any;
32
+ let IsoDep: any;
33
+ let Intent: any;
34
+ let PendingIntent: any;
35
+ let IntentFilter: any;
36
+ let Settings: any;
37
+ let NdefRecord: any;
38
+ let NdefMessage: any;
39
+
40
+ let INTENT_FLAGS_SINGLE_TOP: any;
41
+ let PENDING_INTENT_FLAGS: any;
42
+ let EXTRA_NDEF_MESSAGES: any;
43
+ let EXTRA_ID: any;
44
+ let EXTRA_TAG: any;
45
+ let _TNF_MIME_MEDIA: any;
46
+ let _ACTION_NFC_SETTINGS: any;
47
+
48
+ let NFC_INTENT_FILTERS: any[] = [];
61
49
 
62
50
  const NFC_TECH_LISTS = [
63
51
  ['android.nfc.tech.Ndef'],
@@ -71,11 +59,54 @@ const NFC_TECH_LISTS = [
71
59
  ['android.nfc.tech.MifareUltralight'],
72
60
  ];
73
61
 
74
- const NFC_INTENT_FILTERS = [
75
- NfcAdapter.ACTION_NDEF_DISCOVERED,
76
- NfcAdapter.ACTION_TECH_DISCOVERED,
77
- NfcAdapter.ACTION_TAG_DISCOVERED,
78
- ];
62
+ // 初始化 Android
63
+ function initAndroidClasses() {
64
+ try {
65
+ if (NfcAdapter) return; // 避免重复初始化
66
+
67
+ NfcAdapter = plus.android.importClass('android.nfc.NfcAdapter');
68
+ _Context = plus.android.importClass('android.content.Context');
69
+ _Tag = plus.android.importClass('android.nfc.Tag');
70
+ Ndef = plus.android.importClass('android.nfc.tech.Ndef');
71
+ NfcA = plus.android.importClass('android.nfc.tech.NfcA');
72
+ NfcB = plus.android.importClass('android.nfc.tech.NfcB');
73
+ NfcF = plus.android.importClass('android.nfc.tech.NfcF');
74
+ NfcV = plus.android.importClass('android.nfc.tech.NfcV');
75
+ _NdefFormat = plus.android.importClass('android.nfc.tech.NdefFormat');
76
+ NdefFormatable = plus.android.importClass(
77
+ 'android.nfc.tech.NdefFormatable',
78
+ );
79
+ MifareClassic = plus.android.importClass('android.nfc.tech.MifareClassic');
80
+ MifareUltralight = plus.android.importClass(
81
+ 'android.nfc.tech.MifareUltralight',
82
+ );
83
+ IsoDep = plus.android.importClass('android.nfc.tech.IsoDep');
84
+ Intent = plus.android.importClass('android.content.Intent');
85
+ PendingIntent = plus.android.importClass('android.app.PendingIntent');
86
+ IntentFilter = plus.android.importClass('android.content.IntentFilter');
87
+ Settings = plus.android.importClass('android.provider.Settings');
88
+ NdefRecord = plus.android.importClass('android.nfc.NdefRecord');
89
+ NdefMessage = plus.android.importClass('android.nfc.NdefMessage');
90
+
91
+ INTENT_FLAGS_SINGLE_TOP = Intent.FLAG_ACTIVITY_SINGLE_TOP;
92
+ PENDING_INTENT_FLAGS = PendingIntent.FLAG_MUTABLE;
93
+
94
+ EXTRA_NDEF_MESSAGES = NfcAdapter.EXTRA_NDEF_MESSAGES;
95
+ EXTRA_ID = NfcAdapter.EXTRA_ID;
96
+ EXTRA_TAG = NfcAdapter.EXTRA_TAG;
97
+
98
+ _TNF_MIME_MEDIA = NdefRecord.TNF_MIME_MEDIA;
99
+ _ACTION_NFC_SETTINGS = Settings.ACTION_NFC_SETTINGS;
100
+
101
+ NFC_INTENT_FILTERS = [
102
+ NfcAdapter.ACTION_NDEF_DISCOVERED,
103
+ NfcAdapter.ACTION_TECH_DISCOVERED,
104
+ NfcAdapter.ACTION_TAG_DISCOVERED,
105
+ ];
106
+ } catch (e) {
107
+ console.error('Core: initAndroidClasses error', e);
108
+ }
109
+ }
79
110
 
80
111
  export default function useNFCAndroid(
81
112
  listener: NFC.Listener,
@@ -98,9 +129,20 @@ export default function useNFCAndroid(
98
129
  });
99
130
 
100
131
  /** ndef 写入方式比较特殊,需要特殊处理 */
101
- let isNdef = ref(false);
132
+ const isNdef = ref(false);
133
+
134
+ // 延迟获取 mainActivity,确保 plus 已就绪
135
+ let mainActivity: any = null;
102
136
 
103
- const mainActivity: any = plus.android.runtimeMainActivity();
137
+ onMounted(() => {
138
+ initAndroidClasses();
139
+ try {
140
+ mainActivity = plus.android.runtimeMainActivity();
141
+ initNfcAdapter();
142
+ } catch (e) {
143
+ console.error('Core: failed to init NFC adapter', e);
144
+ }
145
+ });
104
146
 
105
147
  let checkInterval: any = null;
106
148
 
@@ -333,8 +375,8 @@ export default function useNFCAndroid(
333
375
 
334
376
  // 可将改行去除
335
377
  uni?.__dev_tool_nfc_collection__?.(data.value);
336
- } catch (error) {
337
- console.log('error: ', error);
378
+ } catch (_error) {
379
+ console.log('error: ', _error);
338
380
  }
339
381
  }
340
382
 
@@ -361,7 +403,7 @@ export default function useNFCAndroid(
361
403
 
362
404
  // 3. 初始化成功,注册监听
363
405
  isSupportNFC.value = true;
364
- } catch (error) {
406
+ } catch (_error) {
365
407
  isSupportNFC.value = false;
366
408
  }
367
409
  }
@@ -449,7 +491,7 @@ export default function useNFCAndroid(
449
491
  return Promise.reject({
450
492
  errMsg: '开始监听NFC',
451
493
  });
452
- } catch (error) {
494
+ } catch (_error) {
453
495
  uni.showToast({ title: 'NFC 监听开启失败', icon: 'none' });
454
496
  isListening.value = false;
455
497
  return Promise.reject({
@@ -499,11 +541,11 @@ export default function useNFCAndroid(
499
541
  return Promise.resolve({
500
542
  errMsg: '停止监听NFC',
501
543
  });
502
- } catch (error) {
544
+ } catch (_error) {
503
545
  uni.showToast({ title: 'NFC 监听停止失败', icon: 'error' });
504
546
  return Promise.resolve({
505
547
  errMsg: 'NFC 监听停止失败',
506
- error,
548
+ error: _error,
507
549
  });
508
550
  }
509
551
  }
@@ -623,7 +665,7 @@ export default function useNFCAndroid(
623
665
  clearInterval(checkInterval);
624
666
  uni.showToast({ title: 'NFC 监听已移除', icon: 'none' });
625
667
  return true;
626
- } catch (error) {
668
+ } catch (_error) {
627
669
  uni.showToast({ title: 'NFC 监听移除失败', icon: 'none' });
628
670
 
629
671
  return false;
@@ -631,146 +673,54 @@ export default function useNFCAndroid(
631
673
  }
632
674
 
633
675
  /**
634
- *
635
- * ndef 数据写入
636
- *
637
- * TODO: 暂时无法提供通用的写入方法
638
- *
639
- * @param {ArrayBuffer} writeData
640
- * @return {*}
676
+ * 确保 NFC 已连接
677
+ * @param checkNdef 是否检查 NDEF 支持
641
678
  */
642
- function write(writeData: ArrayBuffer) {
643
- if (isNdef.value) {
644
- uni.showToast({
645
- title: 'NDEF 请勿使用该方法',
646
- icon: 'none',
647
- });
648
- return Promise.reject({
649
- errMsg: 'NDEF 请勿使用该方法',
650
- });
679
+ function ensureConnected(checkNdef = false) {
680
+ if (checkNdef && !isNdef.value) {
681
+ uni.showToast({ title: 'NFC 标签不支持 NDEF', icon: 'none' });
682
+ throw { errMsg: 'NFC 标签不支持 NDEF' };
651
683
  }
652
-
653
684
  if (!nfcInstance) {
654
- uni.showToast({
655
- title: '请先建立连接',
656
- icon: 'none',
657
- });
658
- return Promise.reject({
659
- errMsg: '请先建立连接',
660
- });
685
+ uni.showToast({ title: '请先建立连接', icon: 'none' });
686
+ throw { errMsg: '请先建立连接' };
661
687
  }
662
-
663
688
  if (!isConnected.value) {
664
- uni.showToast({
665
- title: '请先建立连接',
666
- icon: 'none',
667
- });
668
- return Promise.reject({
669
- errMsg: '请先建立连接',
670
- });
671
- }
672
-
673
- uni.showToast({
674
- title: '开始写入数据,请勿断开连接',
675
- icon: 'none',
676
- });
677
-
678
- try {
679
- nfcInstance.connect();
680
- if (!nfcInstance.isWritable()) {
681
- uni.showToast({ title: 'NFC 标签不允许写入', icon: 'none' });
682
- nfcInstance.close();
683
- return Promise.reject({
684
- errMsg: 'NFC 标签不允许写入',
685
- });
686
- }
687
- if (nfcInstance.getMaxTransceiveLength() < writeData.byteLength) {
688
- uni.showToast({ title: '写入数据超出标签容量', icon: 'none' });
689
- nfcInstance.close();
690
- return Promise.reject({
691
- errMsg: '写入数据超出标签容量',
692
- });
693
- }
694
- const result = nfcInstance.transceive(writeData);
695
- nfcInstance.close();
696
- uni.showToast({ title: '数据写入成功', icon: 'none' });
697
- return Promise.resolve({
698
- errMsg: '数据写入成功',
699
- data: result,
700
- });
701
- } catch (error) {
702
- uni.showToast({ title: '数据写入失败', icon: 'none' });
703
- return Promise.reject({
704
- errMsg: '数据写入失败',
705
- error,
706
- });
689
+ uni.showToast({ title: '请先建立连接', icon: 'none' });
690
+ throw { errMsg: '请先建立连接' };
707
691
  }
708
692
  }
709
693
 
710
694
  /**
711
- * 通用的 ndef 写入方法
712
- *
713
- * @param {NFC.NdefData} data
695
+ * 执行 NDEF 写入操作的通用流程
696
+ * @param createMessage 创建 NdefMessage 的函数
697
+ * @returns Promise
714
698
  */
715
- function writeNdf(data: NFC.NdefRecord[]) {
716
- if (!isNdef.value) {
717
- uni.showToast({
718
- title: 'NFC 标签不支持 NDEF',
719
- icon: 'none',
720
- });
721
- return Promise.reject({
722
- errMsg: 'NFC 标签不支持 NDEF',
723
- });
724
- }
725
-
726
- if (!nfcInstance) {
727
- uni.showToast({
728
- title: '请先建立连接',
729
- icon: 'none',
730
- });
731
- return Promise.reject({
732
- errMsg: '请先建立连接',
733
- });
734
- }
699
+ async function executeNdefWrite(createMessage: () => any) {
700
+ try {
701
+ ensureConnected(true);
735
702
 
736
- if (!isConnected.value) {
737
703
  uni.showToast({
738
- title: '请先建立连接',
704
+ title: '开始写入数据,请勿断开连接',
739
705
  icon: 'none',
740
- });
741
- return Promise.reject({
742
- errMsg: '请先建立连接',
743
- });
744
- }
745
-
746
- uni.showToast({
747
- title: '开始写入数据,请勿断开连接',
748
- icon: 'none',
749
- duration: 3000,
750
- });
751
- try {
752
- const records = data?.map((item) => {
753
- const tnf = stringToBytes(item.tnf + '');
754
- const id = item.id ? stringToBytes(item.id) : null;
755
- const type = stringToBytes(item.type);
756
-
757
- return new NdefRecord(tnf, type, id, item.payload);
706
+ duration: 3000,
758
707
  });
759
708
 
760
- // 1. 构建 NDEF 消息
761
- const message = new NdefMessage(records);
709
+ const message = createMessage();
762
710
 
711
+ // 容量检查
763
712
  if (maxTransceiveLength < message.getByteArrayLength()) {
764
713
  uni.showToast({
765
714
  title: '写入数据超出标签容量',
766
715
  icon: 'none',
767
716
  duration: 3000,
768
717
  });
769
- nfcInstance.close();
770
-
771
- return Promise.reject({
772
- errMsg: '写入数据超出标签容量',
773
- });
718
+ try {
719
+ nfcInstance.close();
720
+ } catch (_e) {
721
+ // ignore error
722
+ }
723
+ return Promise.reject({ errMsg: '写入数据超出标签容量' });
774
724
  }
775
725
 
776
726
  if (nfcInstance.isWritable()) {
@@ -782,50 +732,47 @@ export default function useNFCAndroid(
782
732
  icon: 'none',
783
733
  duration: 3000,
784
734
  });
785
- return Promise.resolve({
786
- errMsg: '数据写入成功',
787
- });
735
+ return Promise.resolve({ errMsg: '数据写入成功' });
788
736
  } else {
789
737
  uni.showToast({
790
738
  title: 'NFC 标签不允许写入',
791
739
  icon: 'none',
792
740
  duration: 3000,
793
741
  });
794
- return Promise.reject({
795
- errMsg: 'NFC 标签不允许写入',
796
- });
742
+ nfcInstance.close();
743
+ return Promise.reject({ errMsg: 'NFC 标签不允许写入' });
797
744
  }
798
- } catch (error) {
745
+ } catch (_error) {
799
746
  uni.showToast({
800
747
  title: '数据写入失败',
801
748
  icon: 'none',
802
749
  duration: 3000,
803
750
  });
804
-
805
- return Promise.reject({
806
- errMsg: '数据写入失败',
807
- error,
808
- });
751
+ try {
752
+ nfcInstance?.close();
753
+ } catch (_e) {
754
+ // ignore error
755
+ }
756
+ return Promise.reject({ errMsg: '数据写入失败', error: _error });
809
757
  }
810
758
  }
811
759
 
812
760
  /**
813
- * 写入 NDEF 文本数据
761
+ * ndef 数据写入
814
762
  *
815
- * https://developer.android.google.cn/reference/kotlin/android/nfc/NdefRecord?hl=en#createtextrecord
763
+ * TODO: 暂时无法提供通用的写入方法
816
764
  *
817
- * @param {string} text
818
- * @param {string} [language='zh']
765
+ * @param {ArrayBuffer} writeData
819
766
  * @return {*}
820
767
  */
821
- function writeNdfText(text: string, language: string = 'zh') {
822
- if (!isNdef.value) {
768
+ function write(writeData: ArrayBuffer) {
769
+ if (isNdef.value) {
823
770
  uni.showToast({
824
- title: 'NFC 标签不支持 NDEF',
771
+ title: 'NDEF 请勿使用该方法',
825
772
  icon: 'none',
826
773
  });
827
774
  return Promise.reject({
828
- errMsg: 'NFC 标签不支持 NDEF',
775
+ errMsg: 'NDEF 请勿使用该方法',
829
776
  });
830
777
  }
831
778
 
@@ -852,58 +799,33 @@ export default function useNFCAndroid(
852
799
  uni.showToast({
853
800
  title: '开始写入数据,请勿断开连接',
854
801
  icon: 'none',
855
- duration: 3000,
856
802
  });
857
803
 
858
804
  try {
859
- // 创建 NDEF 消息
860
- const message = new NdefMessage([
861
- NdefRecord.createTextRecord(language, text),
862
- ]);
863
-
864
- // 检查是否支持写入
865
- if (maxTransceiveLength < message.getByteArrayLength()) {
866
- uni.showToast({
867
- title: '写入数据超出标签容量',
868
- icon: 'none',
869
- duration: 3000,
870
- });
805
+ nfcInstance.connect();
806
+ if (!nfcInstance.isWritable()) {
807
+ uni.showToast({ title: 'NFC 标签不允许写入', icon: 'none' });
871
808
  nfcInstance.close();
872
-
873
809
  return Promise.reject({
874
- errMsg: '写入数据超出标签容量',
810
+ errMsg: 'NFC 标签不允许写入',
875
811
  });
876
812
  }
877
-
878
- if (nfcInstance.isWritable()) {
879
- nfcInstance.writeNdefMessage(message);
813
+ if (nfcInstance.getMaxTransceiveLength() < writeData.byteLength) {
814
+ uni.showToast({ title: '写入数据超出标签容量', icon: 'none' });
880
815
  nfcInstance.close();
881
-
882
- uni.showToast({
883
- title: '数据写入成功',
884
- icon: 'none',
885
- duration: 3000,
886
- });
887
- return Promise.resolve({
888
- errMsg: '数据写入成功',
889
- });
890
- } else {
891
- uni.showToast({
892
- title: 'NFC 标签不允许写入',
893
- icon: 'none',
894
- duration: 3000,
895
- });
896
816
  return Promise.reject({
897
- errMsg: 'NFC 标签不允许写入',
817
+ errMsg: '写入数据超出标签容量',
898
818
  });
899
819
  }
900
- } catch (error) {
901
- uni.showToast({
902
- title: '数据写入失败',
903
- icon: 'none',
904
- duration: 3000,
820
+ const result = nfcInstance.transceive(writeData);
821
+ nfcInstance.close();
822
+ uni.showToast({ title: '数据写入成功', icon: 'none' });
823
+ return Promise.resolve({
824
+ errMsg: '数据写入成功',
825
+ data: result,
905
826
  });
906
-
827
+ } catch (error) {
828
+ uni.showToast({ title: '数据写入失败', icon: 'none' });
907
829
  return Promise.reject({
908
830
  errMsg: '数据写入失败',
909
831
  error,
@@ -911,6 +833,39 @@ export default function useNFCAndroid(
911
833
  }
912
834
  }
913
835
 
836
+ /**
837
+ * 通用的 ndef 写入方法
838
+ *
839
+ * @param {NFC.NdefData} data
840
+ */
841
+ function writeNdef(data: NFC.NdefRecord[]) {
842
+ return executeNdefWrite(() => {
843
+ const records = data?.map((item) => {
844
+ const tnf = stringToBytes(item.tnf + '');
845
+ const id = item.id ? stringToBytes(item.id) : null;
846
+ const type = stringToBytes(item.type);
847
+
848
+ return new NdefRecord(tnf, type, id, item.payload);
849
+ });
850
+ return new NdefMessage(records);
851
+ });
852
+ }
853
+
854
+ /**
855
+ * 写入 NDEF 文本数据
856
+ *
857
+ * https://developer.android.google.cn/reference/kotlin/android/nfc/NdefRecord?hl=en#createtextrecord
858
+ *
859
+ * @param {string} text
860
+ * @param {string} [language='zh']
861
+ * @return {*}
862
+ */
863
+ function writeNdefText(text: string, language: string = 'zh') {
864
+ return executeNdefWrite(() => {
865
+ return new NdefMessage([NdefRecord.createTextRecord(language, text)]);
866
+ });
867
+ }
868
+
914
869
  /**
915
870
  * 写入 NDEF URI 数据
916
871
  * https://developer.android.google.cn/reference/kotlin/android/nfc/NdefRecord?hl=en#createuri
@@ -925,207 +880,25 @@ export default function useNFCAndroid(
925
880
  * @return {*}
926
881
  */
927
882
  function writeNdefUri(uri: string) {
928
- if (!isNdef.value) {
929
- uni.showToast({
930
- title: 'NFC 标签不支持 NDEF',
931
- icon: 'none',
932
- });
933
- return Promise.reject({
934
- errMsg: 'NFC 标签不支持 NDEF',
935
- });
936
- }
883
+ return executeNdefWrite(() => {
884
+ return new NdefMessage([NdefRecord.createUri(uri)]);
885
+ });
886
+ }
937
887
 
938
- if (!nfcInstance) {
939
- uni.showToast({
940
- title: '请先建立连接',
941
- icon: 'none',
942
- });
943
- return Promise.reject({
944
- errMsg: '请先建立连接',
945
- });
946
- }
947
-
948
- if (!isConnected.value) {
949
- uni.showToast({
950
- title: '请先建立连接',
951
- icon: 'none',
952
- });
953
- return Promise.reject({
954
- errMsg: '请先建立连接',
955
- });
956
- }
957
-
958
- uni.showToast({
959
- title: '开始写入数据,请勿断开连接',
960
- icon: 'none',
961
- duration: 3000,
962
- });
963
-
964
- try {
965
- // 创建 NDEF 消息
966
- const message = new NdefMessage([NdefRecord.createUri(uri)]);
967
-
968
- // 检查是否支持写入
969
- if (maxTransceiveLength < message.getByteArrayLength()) {
970
- uni.showToast({
971
- title: '写入数据超出标签容量',
972
- icon: 'none',
973
- duration: 3000,
974
- });
975
- nfcInstance.close();
976
- return Promise.reject({
977
- errMsg: '写入数据超出标签容量',
978
- });
979
- }
980
-
981
- if (nfcInstance.isWritable()) {
982
- const result = nfcInstance.writeNdefMessage(message);
983
- nfcInstance.close();
984
-
985
- uni.showToast({
986
- title: '数据写入成功',
987
- icon: 'none',
988
- duration: 3000,
989
- });
990
- return Promise.resolve({
991
- errMsg: '数据写入成功',
992
- data: result,
993
- });
994
- } else {
995
- uni.showToast({
996
- title: 'NFC 标签不允许写入',
997
- icon: 'none',
998
- duration: 3000,
999
- });
1000
- return Promise.reject({
1001
- errMsg: 'NFC 标签不允许写入',
1002
- });
1003
- }
1004
- } catch (error) {
1005
- uni.showToast({
1006
- title: '数据写入失败',
1007
- icon: 'none',
1008
- duration: 3000,
1009
- });
1010
- return Promise.reject({
1011
- errMsg: '数据写入失败',
1012
- error,
1013
- });
1014
- }
1015
- }
1016
-
1017
- /**
1018
- * Android 应用包名 写入
1019
- *
1020
- * @param {string} packageName - 数据
1021
- * @param {string} [typeName] - 类型名称,默认为 "a" (Android Application Record)
1022
- * @return {*}
1023
- */
1024
- function writeNdefAndroidApp(packageName: string) {
1025
- if (!isNdef.value) {
1026
- uni.showToast({
1027
- title: 'NFC 标签不支持 NDEF',
1028
- icon: 'none',
1029
- });
1030
- return Promise.reject({
1031
- errMsg: 'NFC 标签不支持 NDEF',
1032
- });
1033
- }
1034
-
1035
- if (!nfcInstance) {
1036
- uni.showToast({
1037
- title: '请先建立连接',
1038
- icon: 'none',
1039
- });
1040
- return Promise.reject({
1041
- errMsg: '请先建立连接',
1042
- });
1043
- }
1044
-
1045
- if (!isConnected.value) {
1046
- uni.showToast({
1047
- title: '请先建立连接',
1048
- icon: 'none',
1049
- });
1050
- return Promise.reject({
1051
- errMsg: '请先建立连接',
1052
- });
1053
- }
1054
-
1055
- uni.showToast({
1056
- title: '开始写入数据,请勿断开连接',
1057
- icon: 'none',
1058
- duration: 3000,
1059
- });
1060
-
1061
- try {
1062
- // 创建 Android 应用记录 (AAR - Android Application Record)
1063
- const appRecord = NdefRecord.createApplicationRecord(packageName);
1064
-
1065
- // 将包名字符串转换为字节数组
1066
- // const packageBytes = plus.android.invoke(
1067
- // packageName,
1068
- // 'getBytes',
1069
- // 'UTF-8',
1070
- // );
1071
-
1072
- // 或者使用 createExternalTypeRecord 创建自定义外部类型记录
1073
- // const appRecord = NdefRecord.createExternalTypeRecord(
1074
- // 'android.com:pkg', // 域名:类型
1075
- // packageBytes // 负载数据
1076
- // );
1077
-
1078
- // 创建 NDEF 消息
1079
- const message = new NdefMessage([appRecord]);
1080
-
1081
- // 检查是否支持写入
1082
- if (maxTransceiveLength < message.getByteArrayLength()) {
1083
- uni.showToast({
1084
- title: '写入数据超出标签容量',
1085
- icon: 'none',
1086
- duration: 3000,
1087
- });
1088
- nfcInstance.close();
1089
- return Promise.reject({
1090
- errMsg: '写入数据超出标签容量',
1091
- });
1092
- }
1093
-
1094
- if (nfcInstance.isWritable()) {
1095
- nfcInstance.writeNdefMessage(message);
1096
- nfcInstance.close();
1097
-
1098
- uni.showToast({
1099
- title: '数据写入成功',
1100
- icon: 'none',
1101
- duration: 3000,
1102
- });
1103
- return Promise.resolve({
1104
- errMsg: '数据写入成功',
1105
- });
1106
- } else {
1107
- uni.showToast({
1108
- title: 'NFC 标签不允许写入',
1109
- icon: 'none',
1110
- duration: 3000,
1111
- });
1112
- return Promise.reject({
1113
- errMsg: 'NFC 标签不允许写入',
1114
- });
1115
- }
1116
- } catch (error) {
1117
- uni.showToast({
1118
- title: '数据写入失败',
1119
- icon: 'none',
1120
- duration: 3000,
1121
- });
1122
- console.error('数据写入失败:', error);
1123
- return Promise.reject({
1124
- errMsg: '数据写入失败',
1125
- error,
1126
- });
1127
- }
1128
- }
888
+ /**
889
+ * Android 应用包名 写入
890
+ *
891
+ * @param {string} packageName - 数据
892
+ * @param {string} [typeName] - 类型名称,默认为 "a" (Android Application Record)
893
+ * @return {*}
894
+ */
895
+ function writeNdefAndroidApp(packageName: string) {
896
+ return executeNdefWrite(() => {
897
+ // 创建 Android 应用记录 (AAR - Android Application Record)
898
+ const appRecord = NdefRecord.createApplicationRecord(packageName);
899
+ return new NdefMessage([appRecord]);
900
+ });
901
+ }
1129
902
 
1130
903
  /**
1131
904
  * 写入 WiFi 配置到 NFC 标签
@@ -1134,112 +907,16 @@ export default function useNFCAndroid(
1134
907
  * @return {*}
1135
908
  */
1136
909
  function writeNdefWifi({ ssid, password, authType }: NFC.WSCConfig) {
1137
- if (!isNdef.value) {
1138
- uni.showToast({
1139
- title: 'NFC 标签不支持 NDEF',
1140
- icon: 'none',
1141
- });
1142
- return Promise.reject({
1143
- errMsg: 'NFC 标签不支持 NDEF',
1144
- });
1145
- }
1146
-
1147
- if (!nfcInstance) {
1148
- uni.showToast({
1149
- title: '请先建立连接',
1150
- icon: 'none',
1151
- });
1152
- return Promise.reject({
1153
- errMsg: '请先建立连接',
1154
- });
1155
- }
1156
-
1157
- if (!isConnected.value) {
1158
- uni.showToast({
1159
- title: '请先建立连接',
1160
- icon: 'none',
1161
- });
1162
- return Promise.reject({
1163
- errMsg: '请先建立连接',
1164
- });
1165
- }
1166
-
1167
- uni.showToast({
1168
- title: '开始写入数据,请勿断开连接',
1169
- icon: 'none',
1170
- duration: 3000,
1171
- });
1172
-
1173
- try {
910
+ return executeNdefWrite(() => {
1174
911
  const mimeType = 'application/vnd.wfa.wsc';
1175
-
1176
- // const wifiData = convertWifiInfoToTLVBytes({
1177
- // ssid,
1178
- // password,
1179
- // authType,
1180
- // });
1181
-
1182
912
  const wifiData = generateWSCBytesForAndroid({
1183
913
  ssid,
1184
914
  password,
1185
915
  authType,
1186
916
  });
1187
-
1188
917
  const wifiRecord = NdefRecord.createMime(mimeType, wifiData);
1189
-
1190
- // 创建 WiFi 配置的 TLV 数据
1191
- // const wifiRecord = createWifiRecord(ssid, password, authType);
1192
-
1193
- // 创建 NDEF 消息
1194
- const message = new NdefMessage([wifiRecord]);
1195
-
1196
- // 检查是否支持写入
1197
- if (maxTransceiveLength < message.getByteArrayLength()) {
1198
- uni.showToast({
1199
- title: '写入数据超出标签容量',
1200
- icon: 'none',
1201
- duration: 3000,
1202
- });
1203
- nfcInstance.close();
1204
- return Promise.reject({
1205
- errMsg: '写入数据超出标签容量',
1206
- });
1207
- }
1208
-
1209
- if (nfcInstance.isWritable()) {
1210
- nfcInstance.writeNdefMessage(message);
1211
- nfcInstance.close();
1212
-
1213
- uni.showToast({
1214
- title: '数据写入成功',
1215
- icon: 'none',
1216
- duration: 3000,
1217
- });
1218
- return Promise.resolve({
1219
- errMsg: '数据写入成功',
1220
- });
1221
- } else {
1222
- uni.showToast({
1223
- title: 'NFC 标签不允许写入',
1224
- icon: 'none',
1225
- duration: 3000,
1226
- });
1227
- return Promise.reject({
1228
- errMsg: 'NFC 标签不允许写入',
1229
- });
1230
- }
1231
- } catch (error) {
1232
- uni.showToast({
1233
- title: '数据写入失败',
1234
- icon: 'none',
1235
- duration: 3000,
1236
- });
1237
- console.error('数据写入失败:', error);
1238
- return Promise.reject({
1239
- errMsg: '数据写入失败',
1240
- error,
1241
- });
1242
- }
918
+ return new NdefMessage([wifiRecord]);
919
+ });
1243
920
  }
1244
921
 
1245
922
  /**
@@ -1249,101 +926,13 @@ export default function useNFCAndroid(
1249
926
  * @return {*}
1250
927
  */
1251
928
  function writeNdefVcard(cardInfo: NFC.VCardContactInfo) {
1252
- if (!isNdef.value) {
1253
- uni.showToast({
1254
- title: 'NFC 标签不支持 NDEF',
1255
- icon: 'none',
1256
- });
1257
- return Promise.reject({
1258
- errMsg: 'NFC 标签不支持 NDEF',
1259
- });
1260
- }
1261
-
1262
- if (!nfcInstance) {
1263
- uni.showToast({
1264
- title: '请先建立连接',
1265
- icon: 'none',
1266
- });
1267
- return Promise.reject({
1268
- errMsg: '请先建立连接',
1269
- });
1270
- }
1271
-
1272
- if (!isConnected.value) {
1273
- uni.showToast({
1274
- title: '请先建立连接',
1275
- icon: 'none',
1276
- });
1277
- return Promise.reject({
1278
- errMsg: '请先建立连接',
1279
- });
1280
- }
1281
-
1282
- uni.showToast({
1283
- title: '开始写入数据,请勿断开连接',
1284
- icon: 'none',
1285
- duration: 3000,
1286
- });
1287
-
1288
- try {
929
+ return executeNdefWrite(() => {
1289
930
  const mimeType = 'text/vcard';
1290
-
1291
931
  const cardString = formatCardInfo(cardInfo);
1292
-
1293
932
  const cardData = stringToBytes(cardString);
1294
-
1295
933
  const cardRecord = NdefRecord.createMime(mimeType, cardData);
1296
-
1297
- // 创建 NDEF 消息
1298
- const message = new NdefMessage([cardRecord]);
1299
-
1300
- // 检查是否支持写入
1301
- if (maxTransceiveLength < message.getByteArrayLength()) {
1302
- uni.showToast({
1303
- title: '写入数据超出标签容量',
1304
- icon: 'none',
1305
- duration: 3000,
1306
- });
1307
- nfcInstance.close();
1308
- return Promise.reject({
1309
- errMsg: '写入数据超出标签容量',
1310
- });
1311
- }
1312
-
1313
- if (nfcInstance.isWritable()) {
1314
- nfcInstance.writeNdefMessage(message);
1315
- nfcInstance.close();
1316
-
1317
- uni.showToast({
1318
- title: '数据写入成功',
1319
- icon: 'none',
1320
- duration: 3000,
1321
- });
1322
- return Promise.resolve({
1323
- errMsg: '数据写入成功',
1324
- });
1325
- } else {
1326
- uni.showToast({
1327
- title: 'NFC 标签不允许写入',
1328
- icon: 'none',
1329
- duration: 3000,
1330
- });
1331
- return Promise.reject({
1332
- errMsg: 'NFC 标签不允许写入',
1333
- });
1334
- }
1335
- } catch (error) {
1336
- uni.showToast({
1337
- title: '数据写入失败',
1338
- icon: 'none',
1339
- duration: 3000,
1340
- });
1341
- console.error('数据写入失败:', error);
1342
- return Promise.reject({
1343
- errMsg: '数据写入失败',
1344
- error,
1345
- });
1346
- }
934
+ return new NdefMessage([cardRecord]);
935
+ });
1347
936
  }
1348
937
 
1349
938
  onMounted(() => {
@@ -1367,8 +956,8 @@ export default function useNFCAndroid(
1367
956
  connect,
1368
957
  close,
1369
958
  write,
1370
- writeNdf,
1371
- writeNdfText,
959
+ writeNdef,
960
+ writeNdefText,
1372
961
  writeNdefUri,
1373
962
  writeNdefAndroidApp,
1374
963
  writeNdefWifi,