蓝牙android ane Bluetooth ANE

地址 :http://as3breeze.com/bluetooth-ane/

 

Sources: BitBucket
Documentation: DOCS
Android Bluetooth ANE (477)

 

// 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 & Google+: prototive@gmail.com (use G+)
// Twitter: @danelkirch
import com.as3breeze.air.ane.android.*;

// 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);
bluetooth.scanForVisibleDevices();
}
//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.name, "{", b.device.address, "}", bondstate);
availableDevices.push( b.device );
break;
case BluetoothScanEvent.BLUETOOTH_DISCOVERY_FINISHED:
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
availableDevices.forEach(
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);
arduinoDevice.connect();
}
break;
case BluetoothScanEvent.BLUETOOTH_DISCOVERY_STARTED:
trace( "Scan started..." );
break;
}
}
function deviceEventHandler( b:BluetoothDeviceEvent ):void
{
switch( b.type ) {
case BluetoothDeviceEvent.BLUETOOTH_DEVICE_CONNECTED:
trace( "Connected to:", b.device.name );
// 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
b.device.sendData(ba);
break;
case BluetoothDeviceEvent.BLUETOOTH_DEVICE_DISCONNECTED:
trace( "Device is dis-connected!" );
break;
case BluetoothDeviceEvent.BLUETOOTH_DEVICE_CONNECT_ERROR:
trace( "Some error occured when connecting :(" );
break;
}
}
private function gotData( b:BluetoothDataEvent ):void
{
var ba:ByteArray = b.data as ByteArray;
ba.position = 0;
var str:String = ba.readUTFBytes( ba.bytesAvailable ) as String;
trace("Received from bluetooth device: ",str);
}

需要加入权限

]]>

Comments are closed.