From 76aa8a649447ee6e15d4e2cb74f139c80b971ac5 Mon Sep 17 00:00:00 2001 From: Ibrahim Hadeed Date: Sat, 27 Aug 2016 02:03:06 -0400 Subject: [PATCH] feat(nfc): add nfc support (#493) * feat(nfc): add nfc plugin addresses #412 * feat(nfc): add nfc support --- src/index.ts | 3 + src/plugins/nfc.ts | 161 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 164 insertions(+) create mode 100644 src/plugins/nfc.ts diff --git a/src/index.ts b/src/index.ts index 7f4de613b..2399d47bb 100644 --- a/src/index.ts +++ b/src/index.ts @@ -69,6 +69,7 @@ import { NativeStorage } from './plugins/nativestorage'; import { Market } from './plugins/market'; import { MediaPlugin } from './plugins/media'; import { Network } from './plugins/network'; +import { NFC } from './plugins/nfc'; import { OneSignal } from './plugins/onesignal'; import { PhotoViewer } from './plugins/photo-viewer'; import { ScreenOrientation } from './plugins/screen-orientation'; @@ -123,6 +124,7 @@ export * from './plugins/imageresizer'; export * from './plugins/inappbrowser'; export * from './plugins/launchnavigator'; export * from './plugins/localnotifications'; +export * from './plugins/nfc'; export * from './plugins/media'; export * from './plugins/media-capture'; export * from './plugins/native-page-transitions'; @@ -265,6 +267,7 @@ window['IonicNative'] = { NativePageTransitions: NativePageTransitions, NativeStorage: NativeStorage, Network: Network, + NFC: NFC, Printer: Printer, Push: Push, OneSignal: OneSignal, diff --git a/src/plugins/nfc.ts b/src/plugins/nfc.ts new file mode 100644 index 000000000..04d847c15 --- /dev/null +++ b/src/plugins/nfc.ts @@ -0,0 +1,161 @@ +import {Plugin, Cordova} from './plugin'; +import {Observable} from 'rxjs/Observable'; +/** + * @name NFC + * @description + * The NFC plugin allows you to read and write NFC tags. You can also beam to, and receive from, other NFC enabled devices. + * + * Use to + * - read data from NFC tags + * - write data to NFC tags + * - send data to other NFC enabled devices + * - receive data from NFC devices + * + * This plugin uses NDEF (NFC Data Exchange Format) for maximum compatibilty between NFC devices, tag types, and operating systems. + * + * @usage + * ``` + * import {NFC, Ndef} from 'ionic-native'; + * + * let message = Ndef.textRecord('Hello world'); + * NFC.share([message]).then(onSuccess).catch(onError); + * + * ``` + */ +@Plugin({ + plugin: 'phonegap-nfc', + pluginRef: 'nfc', + repo: 'https://github.com/chariotsolutions/phonegap-nfc' +}) +export class NFC { + /** + * Registers an event listener for any NDEF tag. + * @param onSuccess + * @param onFailure + * @return {Promise} + */ + @Cordova({ + observable: true, + successIndex: 0, + errorIndex: 3, + clearFunction: 'removeNdefListener', + clearWithArgs: true + }) + static addNdefListener(onSuccess?: Function, onFailure?: Function): Observable {return; } + + /** + * Registers an event listener for tags matching any tag type. + * @param mimeType + * @param onSuccess + * @param onFailure + * @return {Promise} + */ + @Cordova({ + observable: true, + successIndex: 1, + errorIndex: 4, + clearFunction: 'removeTagDiscoveredListener', + clearWithArgs: true + }) + static addTagDiscoveredListener(mimeType: string, onSuccess?: Function, onFailure?: Function): Observable {return; } + + /** + * Registers an event listener for NDEF tags matching a specified MIME type. + * @param onSuccess + * @param onFailure + * @return {Promise} + */ + @Cordova({ + observable: true, + successIndex: 0, + errorIndex: 3, + clearFunction: 'removeMimeTypeListener', + clearWithArgs: true + }) + static addMimeTypeListener(onSuccess?: Function, onFailure?: Function): Observable {return; } + + /** + * Registers an event listener for formatable NDEF tags. + * @param onSuccess + * @param onFailure + * @return {Promise} + */ + @Cordova({ + observable: true, + successIndex: 0, + errorIndex: 3 + }) + static addNdefFormatableListener(onSuccess?: Function, onFailure?: Function): Observable {return; } + + /** + * Qrites an NdefMessage to a NFC tag. + * @param message {any[]} + * @return {Promise} + */ + @Cordova() + static write(message: any[]): Promise {return; } + /** + * Makes a NFC tag read only. **Warning** this is permanent. + * @return {Promise} + */ + @Cordova() + static makeReadyOnly(): Promise {return; } + + /** + * Shares an NDEF Message via peer-to-peer. + * @param message An array of NDEF Records. + * @return {Promise} + */ + @Cordova() + static share(message: any[]): Promise {return; } + + /** + * Stop sharing NDEF data via peer-to-peer. + * @return {Promise} + */ + @Cordova() + static unshare(): Promise {return; } + + /** + * Erase a NDEF tag + */ + @Cordova() + static erase(): Promise {return; } + + /** + * Send a file to another device via NFC handover. + * @param uris A URI as a String, or an array of URIs. + * @return {Promise} + */ + @Cordova() + static handover(uris: string[]): Promise {return; } + + /** + * Stop sharing NDEF data via NFC handover. + * @return {Promise} + */ + @Cordova() + static stopHandover(): Promise {return; } + + /** + * Show the NFC settings on the device. + * @return {Promise} + */ + @Cordova() + static showSettings(): Promise {return; } + + /** + * Check if NFC is available and enabled on this device. + * @return {Promise} + */ + @Cordova() + static enabled(): Promise {return; } + +} + +export declare class Ndef { + uriRecord(uri: string): any; + textRecord(text: string): any; + mimeMediaRecord(mimeType: string, payload: string): any; + androidApplicationRecord(packageName: string): any; +}