forked from lughino/phonegap-nfc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
phonegap-nfc-blackberry.js
76 lines (65 loc) · 2.35 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
64
65
66
67
68
69
70
71
72
73
74
75
76
/*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),
dataString = "",
data,
query;
for (var i=0; i< byteArray.length; ++i) {
dataString += String.fromCharCode(byteArray[i]);
}
data = btoa(dataString);
query = {
"action": "bb.action.SHARE",
"type": "application/vnd.rim.nfc.ndef",
"target": "sys.NFCViewer",
"data": data
};
// previously we used the invoke plugin, but now it encodes improperly
// blackberry.invoke.invoke(query, success, failure);
// call native invoke directly
cordova.exec(success, failure, "com.blackberry.invoke", "invoke", {request: query});
};
// clobber existing unshare function
nfc.unshare = function(success, failure) {
"use strict";
blackberry.invoke.closeChildCard();
if (success) { // no idea if it worked. assume success.
success();
}
};
// clobber existing showSettings function
nfc.showSettings = function(success, failure) {
"use strict";
blackberry.invoke.invoke({ uri: "settings://nfc" }, success, failure);
}
// 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, "phonegap-nfc", "registerNdef", []);
success(); // assume success
};