@yoda/bluetooth. BluetoothA2dp

new BluetoothA2dp(deviceName)

Use bluetooth.getAdapter(PROFILE.A2DP) instead of this constructor.

Parameters:
Name Type Description
deviceName string

The device name.

Extends

Methods

open(modeopt, optionsopt) → {boolean}

Turn on the bluetooth. It will starts a2dp-sink or a2dp-source according parameter mode.

You can listen following changed states:

  • protocol.RADIO_STATE.ON when bluetooth is opened successfully.
  • protocol.RADIO_STATE.ON_FAILED when bluetooth cannot be opened.
  • protocol.CONNECTION_STATE.AUTOCONNECT_FAILED when bluetooth opened but auto connect to history paired device failed.
Parameters:
Name Type Attributes Description
mode A2DP_MODE <optional>

Specify the bluetooth a2dp profile mode. Default will starts A2DP_MODE.SINK.

options object <optional>

The extra options.

Name Type Attributes Default Description
autoplay boolean <optional>
false

Whether after autoconnected, music should be played automatically.

Fires:
  • module:@yoda/bluetooth/BluetoothA2dp#event:radio_state_changed
  • module:@yoda/bluetooth/BluetoothA2dp#event:connection_state_changed
Returns:
Type:
boolean

true if send command success else false.

Example
var bluetooth = require('@yoda/bluetooth')
var protocol = bluetooth.protocol
var a2dp = bluetooth.getAdapter(protocol.PROFILE.A2DP)
a2dp.on('radio_state_changed', function (mode, state, extra) {
  console.log(`bluetooth mode: ${mode}, state: ${state}`)
})
a2dp.open(protocol.A2DP_MODE.SINK, {autoplay: true})

close() → {boolean}

Turn off the bluetooth.

You can listen following changed state events:

  • protocol.RADIO_STATE.OFF when bluetooth is closed.
Fires:
  • module:@yoda/bluetooth/BluetoothA2dp#event:radio_state_changed
Returns:
Type:
boolean

true if send command success else false.

connect(addr, name) → {boolean}

Connect bluetooth to remote device.

You can listen following changed states:

  • protocol.CONNECTION_STATE.CONNECTED when successfully connected to remote device.
  • protocol.CONNECTION_STATE.CONNECT_FAILED when cannot connect to remote device.
Parameters:
Name Type Description
addr string

Specify the remote bluetooth device's MAC address.

name string

Specify the remote bluetooth device's name.

Fires:
  • module:@yoda/bluetooth/BluetoothA2dp#event:connection_state_changed
Returns:
Type:
boolean

true if send command success else false.

disconnect() → {boolean}

Disconnect bluetooth from remote device.

You can listen following changed state:

  • protocol.CONNECTION_STATE.DISCONNECTED after disconnected from remote device.
Fires:
  • module:@yoda/bluetooth/BluetoothA2dp#event:connection_state_changed
Returns:
Type:
boolean

true if send command success else false.

mute() → {boolean}

Mute a2dp-sink music stream.

No state changed after this command execution.

Returns:
Type:
boolean

true if send command success else false.

unmute() → {boolean}

Unmute a2dp-sink music stream.

No state changed after this command execution.

Returns:
Type:
boolean

true if send command success else false.

syncVol(volopt) → {boolean}

Sync volume to remote device.

Parameters:
Name Type Attributes Description
vol string <optional>

the volume number to be synced.

Returns:
Type:
boolean

true if send command success else false.

play() → {boolean}

Play a2dp-sink music stream.

You can listen following changed state:

  • protocol.AUDIO_STATE.PLAYING after music play started.
Fires:
  • module:@yoda/bluetooth/BluetoothA2dp#event:audio_state_changed
Returns:
Type:
boolean

true if send command success else false.

pause() → {boolean}

Pause a2dp-sink music stream.

You can listen following changed state:

  • protocol.AUDIO_STATE.PAUSED after music play paused.
Fires:
  • module:@yoda/bluetooth/BluetoothA2dp#event:audio_state_changed
Returns:
Type:
boolean

true if send command success else false.

stop() → {boolean}

Stop a2dp-sink music stream.

You can listen following changed state:

  • protocol.AUDIO_STATE.STOPPED after music play stopped.
Fires:
  • module:@yoda/bluetooth/BluetoothA2dp#event:audio_state_changed
Returns:
Type:
boolean

true if send command success else false.

prev() → {boolean}

Play a2dp-sink previous song.

No state changed after this command execution.

Returns:
Type:
boolean

true if send command success else false.

next() → {boolean}

Play a2dp-sink next song.

No state changed after this command execution.

Returns:
Type:
boolean

true if send command success else false.

query() → {boolean}

Query playing song's information such as album, title, artist, etc.

You can listen following changed state:

  • protocol.AUDIO_STATE.QUERY_RESULT.
Fires:
  • module:@yoda/bluetooth/BluetoothA2dp#event:audio_state_changed
Returns:
Type:
boolean

true if send command success else false.

discovery() → {boolean}

Discovery around bluetooth devices.

You can listen following changed state:

  • protocol.DISCOVER_STATE.DEVICE_LIST_CHANGED while some bluetooth devices have been found.
Fires:
  • module:@yoda/bluetooth/BluetoothA2dp#event:discovery_state_changed
Returns:
Type:
boolean

true if send command success else false.

getMode() → {A2DP_MODE}

Get current running A2DP profile mode.

Returns:
Type:
A2DP_MODE
  • The current running A2DP profile mode.

getRadioState() → {RADIO_STATE}

Get a2dp radio state.

Returns:
Type:
RADIO_STATE
  • The current radio state.

getConnectionState() → {CONNECTION_STATE}

Get a2dp connection state.

Returns:
Type:
CONNECTION_STATE
  • The current connection state.

getAudioState() → {AUDIO_STATE}

Get a2dp-sink audio state.

Returns:
Type:
AUDIO_STATE
  • The current audio state.

getDiscoveryState() → {DISCOVERY_STATE}

Get a2dp discovery state.

Returns:
Type:
DISCOVERY_STATE
  • The currenct discovery state.

isOpened() → {boolean}

Get if bluetooth is opened.

Returns:
Type:
boolean
  • true if bluetooth is opened else false.

isConnected() → {boolean}

Get if this device is connected with remote device.

Returns:
Type:
boolean
  • true if blueooth is connected with remote device else false.

getConnectedDevice() → {BluetoothDevice|null}

Get connected bluetooth device.

Returns:
Type:
BluetoothDevice | Null
  • Current connected bluetooth device object or null if no connected device.

isPlaying() → {boolean}

Get if a2dp-sink music is playing.

Returns:
Type:
boolean
  • true if bluetooth music is playing else false.

isDiscoverable() → {boolean}

Get if this deivce is under discoverable.

Returns:
Type:
boolean
  • true if local device is under discoverable else false.

destroy()

Destroy bluetooth profile adapter, thus means bluetooth will always be turned OFF automatically.

Events

radio_state_changed

When bluetooth's radio state is changed, such as on/off.

connection_state_changed

When bluetooth's connection state is changed, such as connected/disconnected.

audio_state_changed

When bluetooth's audio stream state is changed, such as playing/paused.

discovery_state_changed

When bluetooth's discovery state is changed, such as undiscoverable or device list changed.