Bluetooth Serial

This API enables serial communication over Bluetooth. It was written for communicating with Arduino.

Usage
bluetoothSerial.available(function (numBytes) {
    console.log("There are " + numBytes + " available to read.");
});
window.bluetoothSerial.available(function (numBytes) {
    console.log("There are " + numBytes + " available to read.");
});
window.bluetoothSerial.available(function (numBytes) {
    console.log("There are " + numBytes + " available to read.");
});
(<any>window).bluetoothSerial.available(function (numBytes) {
    console.log("There are " + numBytes + " available to read.");
});
window.bluetoothSerial.available(function (numBytes) {
    console.log("There are " + numBytes + " available to read.");
});

Classes

bluetoothSerial

connect
connect(macAddress_or_uuid: string): Promise<any>

Connect to a Bluetooth device. The function connect takes a MAC address of the remote device.


PARAMETERS
macAddress_or_uuid: string

RETURN
returns: Promise<any>
connectInsecure
connectInsecure(macAddress: string): Promise<any>

Function connectInsecure works like connect, but creates an insecure connection to a Bluetooth device.


PARAMETERS
macAddress: string

RETURN
returns: Promise<any>
disconnect
disconnect(): Promise<any>

Function disconnect disconnects the current connection.

RETURN
returns: Promise<any>
write
connectInsecure(macAddress: string): Promise<any>

Writes data to the serial port. Data can be an ArrayBuffer, string, array of integers, or a Uint8Array.

Internally string, integer array, and Uint8Array are converted to an ArrayBuffer. String conversion assume 8bit characters.


PARAMETERS
macAddress: string

RETURN
returns: Promise<any>
available
connectInsecure(macAddress: string): Promise<any>

Gets the number of bytes of data available.


PARAMETERS
macAddress: string

RETURN
returns: Promise<any>
read
read(delimiter: string): Promise<any>

Reads data from the buffer.


PARAMETERS
delimiter: string

RETURN
returns: Promise<any>
readUntil
readUntil(delimiter: string): Promise<any>

Reads data from the buffer until it reaches a delimiter.


PARAMETERS
delimiter: string

RETURN
returns: Promise<any>
subscribe
subscribe(delimiter: string): Promise<any>

Subscribe to be notified when data is received.


PARAMETERS
delimiter: string

RETURN
returns: Promise<any>
unsubscribe
unsubscribe(): Promise<any>

Unsubscribe from a subscription.

RETURN
returns: Promise<any>
subscribeRawData
unsubscribe(): Promise<any>

Subscribe to be notified when data is received.

RETURN
returns: Promise<any>
unsubscribeRawData
unsubscribeRawData(): Promise<any>

Subscribe to be notified when data is received.

RETURN
returns: Promise<any>
clear
clear(): Promise<any>

Clears data in the buffer.

RETURN
returns: Promise<any>
list
list(): Promise<ResultList>

Lists the paired Bluetooth devices.

RETURN
returns: Promise<ResultList>
isEnabled
isEnabled(): Promise<any>

Reports if bluetooth is enabled.

RETURN
returns: Promise<any>
isConnected
isConnected(): Promise<any>

Reports the connection status.

RETURN
returns: Promise<any>
readRSSI
readRSSI(): Promise<any>

Reads the RSSI from the connected peripheral.

RETURN
returns: Promise<any>
showBluetoothSettings
showBluetoothSettings(): Promise<any>

Show the Bluetooth settings on the device.

RETURN
returns: Promise<any>
enable
enable(): Promise<any>

Enable Bluetooth on the device.

RETURN
returns: Promise<any>
discoverUnpaired
discoverUnpaired(): Promise<ResultDiscoverUnpaired>

Discovers unpaired Bluetooth devices.

The discovery process takes a while to happen. You can register notify callback with setDeviceDiscoveredListener. You may also want to show a progress indicator while waiting for the discover proces to finish.

Calling connect on an unpaired Bluetooth device should begin the pairing process.

RETURN
returns: Promise<ResultDiscoverUnpaired>
PERMISSIONS

This method requires LOCATION Permission set on Fawi Office - publish settings.

setDeviceDiscoveredListener
setDeviceDiscoveredListener(): Promise<ResultSetDeviceDiscoveredListener>

Register a notify callback function to be called during bluetooth device discovery. For callback to work, discovery process must be started with discoverUnpaired. There can be only one registered callback.

RETURN
returns: Promise<ResultSetDeviceDiscoveredListener>
clearDeviceDiscoveredListener
clearDeviceDiscoveredListener(): Promise<any>

Clears notify callback function registered with SetDeviceDiscoveredListener.

RETURN
returns: Promise<any>
setName
setName(newName: string): Promise<any>

Sets the human readable device name that is broadcasted to other devices.


PARAMETERS
newName: string
RETURN
returns: Promise<any>
setDiscoverable
setDiscoverable(discoverableDuration: int): Promise<any>

Makes the device discoverable by other devices.


PARAMETERS
discoverableDuration: int
RETURN
returns: Promise<any>

setDiscoverable takes an int for the number of seconds device should be discoverable. A time of 0 will make it permanently discoverable.

Interfaces Used

resultList
[{
    "class": 276,
    "id": "10:BF:48:CB:00:00",
    "address": "10:BF:48:CB:00:00",
    "name": "Nexus 7"
}, {
    "class": 7936,
    "id": "00:06:66:4D:00:00",
    "address": "00:06:66:4D:00:00",
    "name": "RN42"
}]
resultDiscoverUnpaired
[{
    "class": 276,
    "id": "10:BF:48:CB:00:00",
    "address": "10:BF:48:CB:00:00",
    "name": "Nexus 7"
}, {
    "class": 7936,
    "id": "00:06:66:4D:00:00",
    "address": "00:06:66:4D:00:00",
    "name": "RN42"
}]
resultSetDeviceDiscoveredListener
{
    "class": 276,
    "id": "10:BF:48:CB:00:00",
    "address": "10:BF:48:CB:00:00",
    "name": "Nexus 7"
}