Bluetooth ANE is a native extension to use Bluetooth capabilities on Android devices. The extension allows the application to send and receive data (both as ByteArray), as well as scan for available devices (discovery).

There are several events which the application can listen, for example when device becomes HIDDEN after being discoverable (VISIBLE). When a device connects to the application, the generated event contains some information about the device, such as its name and Bluetooth MAC address.


// Note this code below is not tested at all, but written as example. If you tend to try this code and find some error, please let me know
// Mail and Google+: (use G+)
// Twitter: @danelkirch 


// It is important to call isSupported, otherwise the default adapter is not found
if ( Bluetooth.isSupported ) {
	// Initiating bluetooth device with generic UUIDs
	var bluetooth:Bluetooth = Bluetooth.currentAdapter();
	bluetooth.addEventListener( BluetoothScanEvent.BLUETOOTH_DISCOVERY_STARTED, bluetoothScanEventHandler);
	bluetooth.addEventListener( BluetoothScanEvent.BLUETOOTH_DEVICE_FOUND, bluetoothScanEventHandler);
	bluetooth.addEventListener( BluetoothScanEvent.BLUETOOTH_DISCOVERY_FINISHED, bluetoothScanEventHandler);
//All of the visible devices will be stored here
var availableDevices:Vector. = new Vector.();

// In the end, this example shows how to connect to a device, that does not accept the generic UUID
var arduinoDevice:BluetoothDevice;
var connectToArduinoAddress:String = "00:18:E4:0C:68:0E";
var stringToSend:String = "Hello awesome bluetoothdevice!";

function bluetoothScanEventHandler( b:BluetoothScanEvent ):void {
	switch (b.type) {
		case BluetoothScanEvent.BLUETOOTH_DEVICE_FOUND:
			var bondstate:String = "";

			// These are pure android codes
			switch( b.device.bondState ) {
				case 10: bondstate = "no-bond";
				case 11: bondstate = "bonding";  // Not likely to happen in BLUETOOTH_DEVICE_FOUND event but giving it as example
				case 12: bondstate = "bonded";
			trace( "Device found:",, "{", b.device.address, "}", bondstate);
			availableDevices.push( b.device );
			trace( "Scan finished!","\n","Connectiong to "+connectToArduinoAddress+" in vector ..." );
			var arduinoDevice:BluetoothDevice = null; 
			// As in my case, one of the available devices was a Bluetooth Shield for Arduino so i will find that BluetoothDevice object and connect to it. 
			// Notice that we can set a new UUID for that specific device, since those generic UUIDs for decure and insecure will not work on my SeeedBTSlave device 
				function( device:BluetoothDevice, index:int, vector:Vector. ){ 
					if( device.address == connectToArduinoAddress ) 
						arduinoDevice = device;
			 } ); 
			// Arduino was found by its MAC address so lets listen for some events and connect to it. 
			if ( arduinoDevice != null ) { 
				arduinoDevice.addEventListener( BluetoothDeviceEvent.BLUETOOTH_DEVICE_CONNECTED, deviceEventHandler); 
				arduinoDevice.addEventListener( BluetoothDeviceEvent.BLUETOOTH_DEVICE_DISCONNECTED, deviceEventHandler); 
				arduinoDevice.addEventListener( BluetoothDeviceEvent.BLUETOOTH_DEVICE_CONNECT_ERROR, deviceEventHandler); 
				// If you were to use this ANE as a chat, use the following eventlistener to receive data. 
				// In this demo i have not yet thought of how to read bytearray from arduino, so arduino, if listening for BLUETOOTH_RECEIVE_DATA event, 
				// will show bytes were received, but these are not handled. If you figure it out, let me know! 
				//arduinoDevice.addEventListener( BluetoothDataEvent.BLUETOOTH_RECEIVE_DATA, gotData);
			trace( "Scan started..." );
function deviceEventHandler( b:BluetoothDeviceEvent ):void { 
	switch( b.type ) { 
		case BluetoothDeviceEvent.BLUETOOTH_DEVICE_CONNECTED: 
			trace( "Connected to:", ); 
			// Create ByteArray of what is being sent. 
			var ba:ByteArray = new ByteArray(); 
			ba.writeUTFBytes( stringToSend ); 
			// Remember to set position to 0. 
			ba.position = 0; 
			// Now send it to device - See Arduino code over here 
			trace( "Device is dis-connected!" );
			trace( "Some error occured when connecting :(" );
private function gotData( b:BluetoothDataEvent ):void { 
	var ba:ByteArray = as ByteArray;
	ba.position = 0;
	var str:String = ba.readUTFBytes( ba.bytesAvailable ) as String;
	trace("Received from bluetooth device: ",str);
Air Native Extension, Android . URL.