/navigator.tapConnect

An attempt to create an NFC/BLE counterpart to the Web2Native Bridge

Primary LanguageJava

navigator.tapConnect

This is a highly experimental project. You better keep out for a while :-)

Overview

https://cyberphone.github.io/openkeystore/resources/docs/tapConnect.pdf

API

This API extends the navigator object by a single method
    tapConnect('Name of target application' [, optionalArgument])
which returns a JavaScript Promise to a port object.

The port object supports the following methods and events:

  • postMessage(message)
  • disconnect()
  • addMessageListener(function(message))
  • addConnectionListener(function(initialized))

optionalArgument and message must be a valid JSON-serializable JavaScript objects.

initialized is a boolean which is true when a "tap" has been initiated and false when the connection terminates.

An example which could be hosted in an ordinary (non-privileged) web page:

navigator.tapConnect('com.example.myapp').then(function(port) {

    port.addMessageListener(function(message) {
        // We got a message from the external application...
    });

    port.addConnectionListener(function(initialized) {
        if (initialized) {
           // Someone tapped!
        } else {
           // Exernal application disconnected
        }
    });

    port.postMessage({greeting:'External app, how are you doing?'});
    // Note: JavaScript serialization makes the above a genuine JSON object

    port.disonnect();  // Good-bye external application...

}, function(err) {
    console.debug(err);
});

The argument to tapConnect holds the name of the specifically adapted local application to invoke. The current scheme uses a Java-inspired dotted path which is supposed to be mapped to the external device's OS in an OS-specific way.