BluetoothModule Class
Item Index
Methods
Properties
Constants
Methods
cancelDiscovery
()
Boolean
Cancels the current device discovery process.
Because discovery is a heavyweight procedure for the Bluetooth adapter, this method should always be called before attempting to connect to a remote device. Discovery is run as a system service, so an application should always call cancel discovery even if it did not directly request a discovery, just to be sure.
Returns:
If Bluetooth state is not STATE_ON, this function will return false.
checkBluetoothAddress
-
address
Validates a Bluetooth address, such as "00:43:A8:23:10:F0". Returns true if the address is valid, false otherwise.
Alphabetic characters must be uppercase to be valid.
Parameters:
-
address
Stringa string representing the address, such as "00:43:A8:23:10:F0".
Returns:
True if the address is valid, false otherwise.
createBluetoothSocket
-
params
This method is not needed as of Titanium 3.1.1.GA and is deprecated as of version 1.0.1 of the module. Please use the createSocket method instead.
Use this method to create a socket to a remote device until
TIMOB-13097 is fixed
in Titanium.
Parameters:
-
params
ObjectAn object with the following properties:
-
remoteDevice
BluetoothDeviceThe device this socket will connect to.
-
uuid
StringUUID of the service this socket will connect to.
-
[secure=false]
Boolean optionalDefines whether the connection will be secure or insecure.
-
Returns:
createServerSocket
-
params
Creates a server socket that listens for incoming connections.
Parameters:
-
params
ObjectAn object with the following properties:
-
name
StringName of the service for SDP record.
-
uuid
StringThe UUID of the service for SDP record.
-
[secure=false]
Boolean optionalSpecifies whether the connection will be secured.
-
Returns:
disable
()
Boolean
Turns off Bluetooth functionality on the device. Android guidelines dictate that you only use this function in response to explicit user action to turn off Bluetooth.
This is an asynchronous call; it will return immediately. It returns true to indicate that shutdown of the Bluetooth adapter has begun, or false on immediate error. Listen for the stateChanged event to be notified of actual state changes.
See also the STATE_OFF, STATE_ON, STATE_TURNING_OFF, and STATE_TURNING_ON constants.
Returns:
Returns true to indicate that shutdown of the Bluetooth adapter has begun, or false on immediate error.
enable
()
Boolean
Turns on Bluetooth functionality on the device. Android guidelines dictate that you only use this function in response to explicit user action to turn on Bluetooth.
This is an asynchronous call; it will return immediately. It returns true to indicate that startup of the Bluetooth adapter has begun, or false on immediate error. Listen for the stateChanged event to be notified of actual state changes.
See also the STATE_OFF, STATE_ON, STATE_TURNING_OFF, and STATE_TURNING_ON constants.
Returns:
Returns true to indicate that startup of the Bluetooth adapter has begun, or false on immediate error.
ensureDiscoverable
()
Void
Displays a system activity that asks the user for permission to make the device discoverable by other devices, and to turn on Bluetooth if it is not currently enabled.
Returns:
getAddress
()
String
Returns the hardware address of the local Bluetooth adapter as a string. For example, "00:11:22:AA:BB:CC".
Returns:
The hardware address of the local Bluetooth adapter.
getName
()
String
Returns the friendly Bluetooth name of the local Bluetooth adapter.
Returns:
The friendly Bluetooth name of the local Bluetooth adapter. May return undefined if an error occurs.
getPairedDevices
()
BluetoothDevice[]
Returns the BluetoothDevice objects that are paired to the local adapter.
Returns:
getRemoteDevice
-
address
Returns a BluetoothDevice object for the given Bluetooth hardware address.
Parameters:
-
address
String
Returns:
getScanMode
()
Number
final
Returns the current Bluetooth scan mode of the local Bluetooth adapter.
See also the SCAN_MODE_* constants.
Returns:
getState
()
Number
Returns the current state of the local Bluetooth adapter.
See also the STATE_OFF, STATE_ON, STATE_TURNING_OFF, and STATE_TURNING_ON constants.
Returns:
isDiscovering
()
Boolean
Returns true if the local Bluetooth adapter is currently in the device discovery process.
Device discovery is a heavyweight procedure. New connections to remote Bluetooth devices should not be attempted while discovery is in progress, and existing connections will experience limited bandwidth and high latency. Use cancelDiscovery() to cancel an ongoing discovery.
Returns:
true if the local Bluetooth adapter is currently in the device discovery process.
isEnabled
()
Boolean
Determines whether the Bluetooth adapter is enabled and ready for use.
Returns:
Returns true if Bluetooth is currently enabled.
isSupported
()
Boolean
Determines whether Bluetooth is supported on the device.
Returns:
Returns true if Bluetooth is supported on the device, false otherwise.
setName
-
name
Sets the friendly Bluetooth name of the local Bluetooth adapter.
The Bluetooth adapter must be turned on before you can set the name. After turning on Bluetooth, wait for the adapter to reach STATE_ON state by listening to the stateChanged event.
Parameters:
-
name
StringA valid Bluetooth name. Valid Bluetooth names are a maximum of 248 bytes using UTF-8 encoding, although many remote devices can only display the first 40 characters, and some may be limited to just 20.
Returns:
startDiscovery
()
Boolean
Starts the remote device discovery process.
The discovery process usually involves an inquiry scan of about 12 seconds, followed by a page scan of each new device to retrieve its Bluetooth name.
This is an asynchronous call; it will return immediately. It returns true to indicate success, or false on immediate error. Listen for the discoveryStarted, deviceFound, and BluetoothModule/discoverFinished:event events to be notified of the progress of the discovery process.
Returns:
Returns true to indicate success, or false on immediate error.
Properties
address
String
read-only
The hardware address of this BluetoothDevice, for example "00:11:22:AA:BB:CC".
name
String
The friendly Bluetooth name of the local Bluetooth adapter.
pairedDevices
BluetoothDevice[]
read-only
An array of the BluetoothDevice objects that are paired to the local adapter.
scanMode
Number
The current Bluetooth scan mode of the local Bluetooth adapter.
See also the SCAN_MODE_* constants.
state
Number
read-only
The current state of the local Bluetooth adapter.
See also the STATE_OFF, STATE_ON, STATE_TURNING_OFF, and STATE_TURNING_ON constants.
Constants
DEVICE_BONDED
Number
The value returned by the getBondState method or bondState property when the device is bonded (paired) with the local Bluetooth adapter.
DEVICE_BONDING
Number
The value returned by the getBondState method or bondState property when bonding (pairing) between the device and the local Bluetooth adapter is in progress.
DEVICE_NOT_BONDED
Number
The value returned by the getBondState method or bondState property when the device is not bonded (paired) with the local Bluetooth adapter.
SCAN_MODE_CONNECTABLE
Number
The value returned by the getScanMode method or scanMode property when inquiry scan is disabled but page scan is enabled on the local Bluetooth adapter. This means remote devices can connect to this device but cannot discover it.
SCAN_MODE_CONNECTABLE_DISCOVERABLE
Number
The value returned by the getScanMode method or scanMode property when both inquiry scan and page scan are enabled on the local Bluetooth adapter. This means remote devices can both discover and connect to this device.
SCAN_MODE_NONE
Number
The value returned by the getScanMode method or scanMode property when both inquiry scan and page scan are disabled on the local Bluetooth adapter. This means remote devices can neither discover nor connect to this device.
Events
deviceFound
Fired when the local Bluetooth adapter discovers a device.
Event Payload:
-
device
BluetoothDeviceThe discovered Bluetooth device.
-
RSSI
NumberThe RSSI value at the time of device discovery. This field was added in version 1.0.1.
discoveryFinished
Fired when the local Bluetooth adapter finished discovery.
discoveryStarted
Fired when the local Bluetooth adapter started discovery.
fetchedUUIDs
Returns the results of service discovery started via the fetchUUIDs method.
Event Payload:
-
device
BluetoothDeviceThe Bluetooth device in question.
-
UUIDs
Array of StringsThe service UUIDs of the device.
stateChanged
Fired when the state of the local Bluetooth adapter changes.