/
phonegap-nfc-blackberry.js
63 lines (54 loc) · 1.9 KB
/
phonegap-nfc-blackberry.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*jslint browser: true, unused: vars, quotmark: double */
/*global cordova, nfc, ndef, blackberry */
// blackberry requires the com.blackberry.invoke plugin installed
// you need to edit config.xml for your app and add an invoke-target
// <rim:invoke-target id="com.chariotsolutions.nfc.demo.reader.target">
// <type>APPLICATION</type>
// <filter>
// <action>bb.action.OPEN</action>
// <mime-type>application/vnd.rim.nfc.ndef</mime-type>
// <property value="ndef://0,ndef://1,ndef://2,ndef://3,ndef://4" var="uris" />
// </filter>
// </rim:invoke-target>
// clobber existing share function
nfc.share = function(ndefMessage, success, failure) {
"use strict";
var byteArray = ndef.encodeMessage(ndefMessage),
data = "",
query;
for (var i=0; i< byteArray.length; ++i) {
data += String.fromCharCode(byteArray[i]);
}
query = {
"action": "bb.action.SHARE",
"type": "application/vnd.rim.nfc.ndef",
"data": data
};
blackberry.invoke.invoke(query, success, failure);
};
// clobber existing unshare function
nfc.unshare = function(success, failure) {
"use strict";
blackberry.invoke.closeChildCard();
if (success) { // no idea if it worked. assume success.
success();
}
};
// takes an ndefMessage from the success callback and fires a javascript event
var proxy = function(ndefMessageAsString) {
"use strict";
var ndefMessage = JSON.parse(ndefMessageAsString);
cordova.fireDocumentEvent("ndef", {
type: "ndef",
tag: {
ndefMessage: ndefMessage
}
});
};
// clobber existing addNdefListener function
nfc.addNdefListener = function (callback, success, failure) {
"use strict";
document.addEventListener("ndef", callback, false);
cordova.exec(proxy, failure, "com.chariotsolutions.nfc.plugin", "registerNdef", []);
success(); // assume success
};