我在Air上创建一个应用程序,用于Android与WiFly设备通信。 由于android不支持Ad-Hoc,我决定让android成为热点并让WiFly加入它。 我正在尝试像http://dka.web-republic.de/2012/08/connecting-arduino-and-android-wirelessly-via-wifi-using-the-rn-xv-wifly-module/那样做 但是使用Adobe Air。
我遇到的部分是,在WiFly加入热点之后,空气无法检测到它。我正在使用服务器套接字来监听连接,但事件未被触发。在Daniel的示例中,他获得了连接到热点的所有设备的客户端列表。这可能在空中吗?
感谢您的帮助。我的代码如下。
package
{
import flash.events.DataEvent;
import flash.events.Event;
import flash.events.ServerSocketConnectEvent
import flash.events.EventDispatcher;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
import flash.events.SecurityErrorEvent;
import flash.net.Socket;
import flash.net.ServerSocket;
import flash.utils.ByteArray;
public class SerialPort extends EventDispatcher
{
private var _host :String;
private var _socket :Socket;
private var _serverSocket:ServerSocket;
private var _port :int;
private var _startChar :String;
private var _stopChar :String;
private var _receivedstr :String = "";
private var _maxLength :int = 128;
private var _tmpstr :String;
private var _findReturn :int;
private var _nlPattern :RegExp = /\n/g;
public function SerialPort( host:String = "127.0.0.1", port:int = 5331, sC:String = "", eC:String ="\r")
{
super();
_host = host;
_port = port;
_startChar = sC;
_stopChar = eC;
_socket = new Socket();
_serverSocket = new ServerSocket;
if(ServerSocket.isSupported)
{
dispatchEvent(new Event("SERVER_SUPPORTED") );
}
_socket.addEventListener( Event.CLOSE, onClose );
_socket.addEventListener( Event.CONNECT, onConnect );
_socket.addEventListener( IOErrorEvent.IO_ERROR, onIOErrorEvent );
_socket.addEventListener( SecurityErrorEvent.SECURITY_ERROR, onSecurityError );
_socket.addEventListener( ProgressEvent.SOCKET_DATA, onSocketData );
}
public function get connected():Boolean
{
return _socket.connected;
}
public function close():void
{
_socket.close();
}
public function bind():void
{
if( _serverSocket.bound )
{
_serverSocket.close();
_serverSocket = new ServerSocket();
}
trace(_port, _host);
_serverSocket.bind(_port,_host );
_serverSocket.addEventListener( ServerSocketConnectEvent.CONNECT, onConnect );
_serverSocket.listen();
trace( "Bound to: " + _serverSocket.localAddress + ":" + _serverSocket.localPort );
}
/*
public function connect():void
{
_socket.connect( _host, _port );
trace( "connecting" );
}
*/
public function writeString( value:String ):void
{
// end send every message with a carriage return (used by Messenger library for Arduino)
//trace(value);
//value += "\r";
_socket.writeUTFBytes( value );
_socket.writeUTFBytes( "\r" );
_socket.flush();
}
private function onClose( event:Event ):void
{
trace( "onClose" );
dispatchEvent( event );
}
private function onConnect( event:ServerSocketConnectEvent ):void
{
_socket = event.socket;
_socket.addEventListener( ProgressEvent.SOCKET_DATA, onSocketData );
trace( "Connection from " + _socket.remoteAddress + ":" + _socket.remotePort );
trace( "onConnect" );
dispatchEvent(new Event("WIFI_CONNECTED") );
}
private function onIOErrorEvent( event:IOErrorEvent ):void
{
trace( "onIOErrorEvent" );
dispatchEvent( event );
}
private function onSecurityError( event:SecurityErrorEvent ):void
{
trace( "onSecurityError" );
dispatchEvent( event );
}
private function onSocketData( event:ProgressEvent ):void
{
// read incoming data from the buffer and add it to _receivedstr
_receivedstr += _socket.readUTFBytes(_socket.bytesAvailable);
// find the stop character (default carriage return) in the string
_findReturn = _receivedstr.indexOf(_stopChar, 0);
// when found an event is dispatched and empty the _receivedstr
if(_findReturn>0)
{
// check if message is complete by checking the first character
// if start character is not empty or the same as the startcharacter dispatch the data
if(_startChar=="")
{ dispatchData(0);
}
else if (_receivedstr.charAt(0) == _startChar)
{ dispatchData(1);
}
}
// sometimes carriage returns are not detected good
// this line resets the _receivedstr when the string gets too long
if(_receivedstr.length>_maxLength) _receivedstr = "";
//trace("length of string : "+_receivedstr.length);
}
private function dispatchData(startpos:int)
{
// make the substring out of the received data (to remove start character is necessary)
_tmpstr = _receivedstr.substring(startpos,_findReturn);
// remove newline (\n) characters
_tmpstr = _tmpstr.replace(_nlPattern, "");
dispatchEvent( new DataEvent( DataEvent.DATA, false, false, _tmpstr) );
_receivedstr = "";
}
}
}