Tutorial: Work with Bluetooth devices

Work with Bluetooth devices

Bluetooth adapter

Set bluetooth adapter parameters:

// turn it on
stbBluetooth.enable = true;

// set it discoverable
stbBluetooth.visible = true;

// set custom name
stbBluetooth.name = 'My set top box';

Get bluetooth adapter address:

console.log('bluetooth adapter address:', stbBluetooth.address);

Set all event handlers before discovery process:

stbBluetooth.onDiscoveryStart = function ( config ) {
    console.log('onDiscoveryStart event:', config);
};

stbBluetooth.onDiscoveryStop = function () {
    console.log('onDiscoveryStop event');
};

stbBluetooth.onDeviceFound = function ( device ) {
    console.log('onDeviceFound event:', device);
};

stbBluetooth.onDeviceRemoved = function ( device ) {
    console.log('onDeviceRemoved event:', device);
};

It's also possible to use stbBluetooth.addEventListener and stbBluetooth.removeEventListener methods:

stbBluetooth.addEventListener('discoveryStart', function ( config ) {
    console.log('discoveryStart listener:', config);
});

stbBluetooth.addEventListener('discoveryStop', function () {
    console.log('discoveryStop listener');
});

stbBluetooth.addEventListener('deviceFound', function ( device ) {
    console.log('deviceFound listener:', device);
});

stbBluetooth.addEventListener('deviceRemoved', function ( device ) {
    console.log('deviceRemoved listener:', device);
});

Start searching for devices:

// scan for specific time - 1.28s * 20
stbBluetooth.startDiscovery({duration: 20});

Get all discovered devices list:

console.log('all discovered devices list:', stbBluetooth.devices);

Discovered device

Let's work with one of the found devices:

var device = stbBluetooth.devices[0];

console.log('founded device:', device);

Get device detailed information:

console.log('MAC address:', device.address);

console.log('discoverable name:', device.name);

console.log('BR/EDR or BLE:', device.type);

console.log('vendor identifier:', device.vendorId);

console.log('product identifier:', device.productId);

console.log('connection state:', device.active);

console.log('paired state:', device.paired);

console.log('available profiles:', device.profiles);

Set all event handlers:

device.onPinProvide = function ( pin ) {
    console.log('onPinProvide event:', pin);
};

device.onAuthentication = function ( status ) {
    console.log('onAuthentication event:', status);
};

device.onChange = function ( name, value ) {
    console.log('onChange event:', name, value);
};

It's also possible to use stbBluetoothDevice.addEventListener and stbBluetoothDevice.removeEventListener methods:

device.addEventListener('pinProvide', function ( pin ) {
    console.log('pinProvide listener:', pin);
});

device.addEventListener('authentication', function ( status ) {
    console.log('authentication listener:', status);
});

device.addEventListener('change', function ( name, value ) {
    console.log('change listener:', name, value);
});

Disconnect and unpair the device and remove it from the paired devices list:

device.forget();

Device profile

Let's work with one of the available profiles:

var profile = device.profiles.HID;

Get profile connection state:

console.log('profile connection state:', profile.active);

Set all event handlers:

profile.onOpen = function () {
    console.log('onOpen event');
};

profile.onClose = function () {
    console.log('onClose event');
};

It's also possible to use stbBluetoothProfile.addEventListener and stbBluetoothProfile.removeEventListener methods:

profile.addEventListener('open', function () {
    console.log('open listener');
});

profile.addEventListener('close', function () {
    console.log('close listener');
});

Open connection:

profile.open();

Close connection:

profile.close();