Package | com.distriqt.extension.bluetooth |
Class | public class Bluetooth |
Inheritance | Bluetooth ![]() |
This class represents the bluetooth extension providing access to the bluetooth functionality on
Using the Bluetooth APIs, an Android application can perform the following:
For a bluetooth connection there must be a 'server' or a listener and a 'client' or connector. One device must create a listening connection to accept incoming connections and another device must connect to this device.
Each connection has a unique uuid that you should share between your application. A listener and a client must have the same uuid in order to initiate a connection.
The simplest way to handle this is to create a listener on both devices and only connect from the device initiating the connection.
AndroidDocumentation for the Android bluetooth implementation can be found here: http://developer.android.com/guide/topics/connectivity/bluetooth.html
Application descriptor additions are required to give your application permission to use the bluetooth functionality on the device. If you require the ability to access the enable function directly, you'll need to add the BLUETOOTH_ADMIN permission as well as the normal BLUETOOTH permission.
<uses-permission android:name="android.permission.BLUETOOTH"/> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
iOS is not possible to support to the level of connectivity we have created in this extension. The iOS SDK is simply too limiting and the required Bluetooth functionality is hidden in a private framework which will cause an application to be rejected if we used it.
The only option for Bluetooth on iOS is to use Bluetooth LE.
Property | Defined By | ||
---|---|---|---|
implementation : String [read-only]
The implementation currently in use. | Bluetooth | ||
isSupported : Boolean [static] [read-only]
Whether the current device supports the extensions functionality
| Bluetooth | ||
nativeVersion : String [read-only]
The native version string of the native extension. | Bluetooth | ||
service : Bluetooth [static] [read-only]
The singleton instance of the Bluetooth class. | Bluetooth | ||
version : String [read-only]
The version of this extension. | Bluetooth |
Method | Defined By | ||
---|---|---|---|
Constructor
You should not call this directly, but instead use the singleton access
| Bluetooth | ||
cancelScan():Boolean
Cancels the scan for discoverable devices
| Bluetooth | ||
close(uuid:String, secure:Boolean = true):Boolean
Close a listening connection with the specified uuid. | Bluetooth | ||
closeAll():Boolean
Close all listening connections and all device connections. | Bluetooth | ||
connect(device:BluetoothDevice, uuid:String, secure:Boolean = true):Boolean
Connect to a device. | Bluetooth | ||
disable():Boolean
Disable the bluetooth adapter
| Bluetooth | ||
disconnect(device:BluetoothDevice, uuid:String):Boolean
Disconnect from a connection
| Bluetooth | ||
dispose():void
Disposes the extension and releases any allocated resources. | Bluetooth | ||
enable():Boolean
Enable the bluetooth adapter. | Bluetooth | ||
enableWithUI():Boolean
Displays a dialog to ask the user to enable bluetooth on
the device. | Bluetooth | ||
getPairedDevices():Vector.<BluetoothDevice>
Retrieves the devices that have been paired with this device. | Bluetooth | ||
getScanMode():String
Retrieve the current scan mode of the bluetooth adapter
| Bluetooth | ||
getState():String
Retrieve the current state of the bluetooth adapter. | Bluetooth | ||
init(key:String):void Deprecated: You no longer need to use an application key [static]
Initialises the extension class for use with the provided key. | Bluetooth | ||
isDiscoverable():Boolean
Check if the device is discoverable. | Bluetooth | ||
isEnabled():Boolean
Whether the bluetooth device is enabled i.e. | Bluetooth | ||
listen(uuid:String, secure:Boolean = true):Boolean
Start a listening connection to receive a connection from
a client device. | Bluetooth | ||
readBytes(uuid:String):ByteArray
When data has been received you'll receive a CONNECTION_RECEIVED_BYTES
event from the extension. | Bluetooth | ||
setDeviceDiscoverable(discoverable:Boolean = true, seconds:int = 60):Boolean
Sets the device to be discoverable to other devices for a specified
amount of time. | Bluetooth | ||
showSettings():Boolean
Displays the bluetooth settings panel to the user. | Bluetooth | ||
startScan():Boolean
This function will start the scan (discovery) process to
find available devices. | Bluetooth | ||
writeBytes(uuid:String, data:ByteArray):Boolean
Write data to the open connection. | Bluetooth |
Constant | Defined By | ||
---|---|---|---|
EXT_CONTEXT_ID : String = com.distriqt.Bluetooth [static] | Bluetooth | ||
VERSION : String = 3.1.0 [static] | Bluetooth |
implementation | property |
implementation:String
[read-only]
The implementation currently in use. This should be one of the following depending on the platform in use and the functionality supported by this extension:
Android
iOS
default
unknown
public function get implementation():String
isSupported | property |
isSupported:Boolean
[read-only] Whether the current device supports the extensions functionality
public static function get isSupported():Boolean
nativeVersion | property |
nativeVersion:String
[read-only]
The native version string of the native extension.
public function get nativeVersion():String
service | property |
service:Bluetooth
[read-only] The singleton instance of the Bluetooth class.
public static function get service():Bluetooth
Error — If there was a problem creating or accessing the extension, or if the key is invalid
|
version | property |
version:String
[read-only]
The version of this extension.
This should be of the format, MAJOR.MINOR.BUILD
public function get version():String
Bluetooth | () | Constructor |
public function Bluetooth()
Constructor You should not call this directly, but instead use the singleton access
cancelScan | () | method |
public function cancelScan():Boolean
Cancels the scan for discoverable devices
ReturnsBoolean — true if the scan process was cancelled successfully
and false if the adapater is not available/supported
|
close | () | method |
public function close(uuid:String, secure:Boolean = true):Boolean
Close a listening connection with the specified uuid.
Parameters
uuid:String — The unique id for the connection to close
| |
secure:Boolean (default = true )
|
Boolean —
|
closeAll | () | method |
public function closeAll():Boolean
Close all listening connections and all device connections. This effectively stops all bluetooth communications.
ReturnsBoolean — true on success
|
connect | () | method |
public function connect(device:BluetoothDevice, uuid:String, secure:Boolean = true):Boolean
Connect to a device. You should listen for connection events to determine the outcome of the connect call. This forms the 'client' part of the bluetooth connection.
Parameters
device:BluetoothDevice — The device to connect to. It's important this object contains a valid address
| |
uuid:String — A unique id string to associate with this connection.
This must be the same uuid as used on the other
device in the listen call.
| |
secure:Boolean (default = true ) — Whether a secure socket connection should be used.
You should leave this as true unless you have some
particular need for an unencrypted connection.
|
Boolean — true if the connection attempt was started successfully
|
See also
disable | () | method |
public function disable():Boolean
Disable the bluetooth adapter
ReturnsBoolean — true to indicate adapter shutdown has begun, or false on immediate error
|
disconnect | () | method |
public function disconnect(device:BluetoothDevice, uuid:String):Boolean
Disconnect from a connection
Parameters
device:BluetoothDevice — The device to disconnect from
| |
uuid:String — The unique id for the connection to disconnect
|
Boolean — true if the disconnect was successful
|
dispose | () | method |
public function dispose():void
Disposes the extension and releases any allocated resources. Once this function
has been called, a call to init
is neccesary again before any of the
extensions functionality will work.
enable | () | method |
public function enable():Boolean
Enable the bluetooth adapter.
Note: You must display some user interface to allow them the choice to enable bluetooth. It is against application guidelines not to give the user a choice. You can use the enableWithUI function to provide you a default UI for this.
ReturnsBoolean — true to indicate adapter startup has begun, or false on immediate error
|
See also
enableWithUI | () | method |
public function enableWithUI():Boolean
Displays a dialog to ask the user to enable bluetooth on the device. You should check the supported and enabled states of the bluetooth before calling this function to decide if the call is neccessary.
ReturnsBoolean — true if the UI to enable bluetooth was shown and false if bluetooth is
not supported or if it is already enabled.
|
See also
getPairedDevices | () | method |
public function getPairedDevices():Vector.<BluetoothDevice>
Retrieves the devices that have been paired with this device.
ReturnsVector.<BluetoothDevice> — A Vector of the paired bluetooth devices
|
See also
getScanMode | () | method |
public function getScanMode():String
Retrieve the current scan mode of the bluetooth adapter
ReturnsString — The current scan mode
|
See also
getState | () | method |
public function getState():String
Retrieve the current state of the bluetooth adapter. See the STATE_strings in the BleutoothDevice class for the available states.
ReturnsString — The current state of the adapter
|
See also
init | () | method |
public static function init(key:String):void
Initialises the extension class for use with the provided key.
Parameters
key:String |
Error — If there was a problem creating or accessing the extension, or if the key is invalid
|
isDiscoverable | () | method |
public function isDiscoverable():Boolean
Check if the device is discoverable. This is very similar to checking if the scan mode is SCAN_MODE_CONNECTABLE_DISCOVERABLE.
ReturnsBoolean — true if the device is currently discoverable
|
See also
isEnabled | () | method |
public function isEnabled():Boolean
Whether the bluetooth device is enabled i.e. turned on and ready for use.
ReturnsBoolean — true if the device is enabled and false otherwise.
|
listen | () | method |
public function listen(uuid:String, secure:Boolean = true):Boolean
Start a listening connection to receive a connection from a client device. This forms the 'server' component to the connect command. A 'server' uses listen while a 'client' uses connect.
Both the client and server must use the same uuid and secure properties in setting up the connection.
Parameters
uuid:String — The unique id for this connection
| |
secure:Boolean (default = true ) — Whether the connection should only accept to secure connections
|
Boolean — true if the listen call was successful.
|
See also
readBytes | () | method |
public function readBytes(uuid:String):ByteArray
When data has been received you'll receive a CONNECTION_RECEIVED_BYTES event from the extension. You will then be able to read bytes from that connection using this function. The event will contain the uuid of the connection that received the data.
Parameters
uuid:String — The connection uuid to read from
|
ByteArray — The byte array data received via the connection
or null if there was an error such as no data being available
or an invalid uuid
|
See also
setDeviceDiscoverable | () | method |
public function setDeviceDiscoverable(discoverable:Boolean = true, seconds:int = 60):Boolean
Sets the device to be discoverable to other devices for a specified amount of time.
Parameters
discoverable:Boolean (default = true ) — Currently ignored (should always be true)
| |
seconds:int (default = 60 ) — The number of seconds to make the device discoverable
|
Boolean — true if the device discoverable request was made successfully
|
showSettings | () | method |
public function showSettings():Boolean
Displays the bluetooth settings panel to the user.
ReturnsBoolean — true if successfully initiated and false if
bluetooth isn't supported
|
startScan | () | method |
public function startScan():Boolean
This function will start the scan (discovery) process to find available devices. The process usually involves an inquiry scan of about 12 seconds, followed by a page scan of each found device to retrieve its Bluetooth name.
NotesPerforming device discovery is a heavy procedure for the Bluetooth adapter and will consume a lot of its resources. Once you have found a device to connect, be certain that you always stop discovery with #cancelScan() before attempting a connection.
Also, if you already hold a connection with a device, then performing discovery can significantly reduce the bandwidth available for the connection, so you should not perform discovery while connected.
ReturnsBoolean — true if the scan process was started successfully
and false if the adapater is not available/supported
|
See also
writeBytes | () | method |
public function writeBytes(uuid:String, data:ByteArray):Boolean
Write data to the open connection.
The data can be any valid byte array data.
The connection must have previously been successfully connected using the listen/connect functionality.
Parameters
uuid:String — The connection uuid to write the data to
| |
data:ByteArray — The byte array to send
|
Boolean — true if the data write was successful
|
See also
EXT_CONTEXT_ID | Constant |
public static const EXT_CONTEXT_ID:String = com.distriqt.Bluetooth
VERSION | Constant |
public static const VERSION:String = 3.1.0
This simple example shows how to initialise the extension and enable the bluetooth adapater.
... Bluetooth.init( DEV_KEY ); Bluetooth.service.addEventListener( BluetoothEvent.STATE_CHANGED, bluetooth_stateChangedHandler, false, 0, true ); if (!Bluetooth.service.isEnabled()) Bluetooth.service.enableWithUI(); ... private function bluetooth_stateChangedHandler( event:BluetoothEvent ):void { trace( "state changed:: "+event.data ); }
Retrieve the list of paired devices
Bluetooth.init( DEV_KEY ); var pairedDevices:Vector.<BluetoothDevice> = Bluetooth.service.getPairedDevices(); if (pairedDevices.length == 0) { trace( "No paired devices" ); } else { for each (var device:BluetoothDevice in pairedDevices) { trace( "Device: " + device.deviceName + "::"+device.address ); if (device.bluetoothClass != null) trace( " Class: " + device.bluetoothClass.deviceClass +"::"+device.bluetoothClass.majorDeviceClass ); } }
Make the bluetooth device discoverable for 30 seconds
Bluetooth.init( DEV_KEY ); Bluetooth.service.setDeviceDiscoverable( true, 30 );
Scan for available bluetooth devices
Bluetooth.init( DEV_KEY ); Bluetooth.service.addEventListener( BluetoothDeviceEvent.DEVICE_FOUND, bluetooth_deviceFoundHandler, false, 0, true ); if (Bluetooth.service.isEnabled()) { Bluetooth.service.startScan(); } ... private function bluetooth_deviceFoundHandler( event:BluetoothDeviceEvent ):void { trace( "device found:: '"+ event.device.deviceName + "' :: "+event.device.address ); if (event.device.bluetoothClass != null) trace( " Class: " + event.device.bluetoothClass.deviceClass + "::" + event.device.bluetoothClass.majorDeviceClass ); }
This example shows how to create a connection between two devices. The server or listener makes itself discoverable and then waits for a connection. Once a connection is received the server then sends a small string to the client. The client scans for available devices and when found, attempts to connect to the device.
Note: This example does not show any error handling which you should use in a real application.
// Server/Listener Device private static var UUID : String = "00000000-DDDD-1111-0000-000000000000"; public function setupBluetooth():void { Bluetooth.init( DEV_KEY ); Bluetooth.service.addEventListener( BluetoothConnectionEvent.CONNECTION_REMOTE, bluetooth_remoteConnectionHandler, false, 0, true ); Bluetooth.service.setDeviceDiscoverable(); var listenResult:Boolean = Bluetooth.service.listen( UUID ); } private function bluetooth_remoteConnectionHandler( event:BluetoothConnectionEvent ):void { var data:ByteArray = new ByteArray(); data.writeUTF( "thanks for connecting to me" ); var success:Boolean = Bluetooth.service.writeBytes( event.uuid, data ); trace( "send to client: success="+success ); }
// Client private static var UUID : String = "00000000-DDDD-1111-0000-000000000000"; public function connectToServer():void { Bluetooth.init( DEV_KEY ); Bluetooth.service.addEventListener( BluetoothDeviceEvent.DEVICE_FOUND, bluetooth_deviceFoundHandler, false, 0, true ); Bluetooth.service.addEventListener( BluetoothConnectionEvent.CONNECTION_RECEIVED_BYTES, bluetooth_receivedBytesHandler, false, 0, true ); Bluetooth.service.startScan(); } private function bluetooth_deviceFoundHandler( event:BluetoothDeviceEvent ):void { Bluetooth.service.connect( event.device, UUID ); } private function bluetooth_receivedBytesHandler( event:BluetoothConnectionEvent ):void { var read:ByteArray = Bluetooth.service.readBytes( event.uuid ); if (read == null) { trace( "error reading data?" ); } else { trace( "READ: " + read.readUTF() ); } }