cordova-plugin-networking-bluetooth
This plugin provides Bluetooth RFCOMM connectivity for peer to peer networking between Android devices, with an API inspired by Mobile Chrome Apps Bluetooth Socket.
For an explaination of the rationale behind this plugin, see this blog post.
Installation
cordova plugin add cordova-plugin-networking-bluetooth
Supported Platforms
- Android
Namespace and API
All the functions and events described in this plugin reside in the networking.bluetooth
namespace.
All the functions are asynchronous and have 2 callbacks as their last 2 parameters, the first being the success callback, and the second being the error callback.
All the events have the following methods:
Event
Adapter information
To obtain the state of the Bluetooth adapter, use the getAdapterState
method:
networkingbluetooth;
The onAdapterStateChanged
event is sent whenever the adapter state changes.
This can be used, for example, to determine when the adapter is enabled or disabled.
var enabled = false;networkingbluetooth; networkingbluetoothonAdapterStateChanged;
To enable the adapter, either the requestEnable
or the enable
functions can be used,
the difference being that the requestEnable
function is recommended, as it nicely prompts
the user before enabling the adapter.
To disable the adapter, use the disable
function.
networkingbluetooth;
Device information and discovery
To get a list of the devices known to the Bluetooth adapter, use the getDevices
method:
networkingbluetooth;
To begin discovery of nearby devices, use the startDiscovery
method.
Discovery can be resource intensive so you should call stopDiscovery
when done.
You should call startDiscovery
whenever your app needs to discover nearby devices.
Do not make the call conditional on the discovering
property of the adapterInfo.
Information about each newly discovered device is received using the onDeviceAdded
event.
Example:
var device_names = {};var { device_namesdeviceaddress = devicename;}; // Add listener to receive newly found devicesnetworkingbluetoothonDeviceAdded; // With the listener in place, get the list of known devicesnetworkingbluetooth; // Now begin the discovery process.networkingbluetooth;
To make the device discoverable, use the requestDiscoverable
function, that will
prompt the user to make the device discoverable for a limited amount of time (120 seconds on Android).
networkingbluetooth;
Connecting to a socket
In order to make a connection to a device you need two things. The address of the device you wish to connect to, and the UUID of the service itself.
Example:
var uuid = '94f39d29-7d6d-437d-973b-fba39e49d4ee'; networkingbluetooth;
Keep a handle to the socketId
so that you can later send data to this socket.
Receiving from and sending to a socket
Receiving data from and sending to a socket uses ArrayBuffer objects.
To send data you have in arrayBuffer
use send
:
networkingbluetooth;
In contrast to the method to send data, data is received in an event (onReceive
).
networkingbluetoothonReceive;
Receiving socket errors and disconnection
To be notified of socket errors, including disconnection, add a listener to the onReceiveError
event.
networkingbluetoothonReceiveError;
Disconnecting from a socket
To hang up the connection and disconnect the socket use close
.
networkingbluetooth;
Listening on a socket
var uuid = '94f39d29-7d6d-437d-973b-fba39e49d4ee';networkingbluetooth;
Accepting client connections
Client connections are accepted and passed to your application through the onAccept
event.
networkingbluetoothonAccept;
Stop accepting client connections
To stop accepting client connections and unpublish the service use close
.
networkingbluetooth;