Contents |
In this tutorial we will follow a step-by-step guide to create a chat by Bluetooth using Kuneri Lite. Kuneri Lite allows access to the S60 features (Bluetooth, GPS, Camera, …) that we haven’t access directly from Flash Lite. More details about Kuneri Lite.
First of all, you’ve to create a separate ActionScript file called KuneriLoader.as. This file will be responsible by all received and sent messages. Put the following code in it.
/* author: Flash Lite Effort - Embedded Systems and Pervasive Computing Lab. version: 0.1 modified: 10/07/2008 */ class KuneriLoader { private var loader:LoadVars; public function KuneriLoader(){ this.loader = new LoadVars(); } public function KLoad(url:String, handler){ loader.onLoad = function() { handler(this); } trace("LOADING: " + url); loader.load(url); } public function KError(msgError:String, klError:String){ trace("KuneriLite: " + klError + " " + msgError); } }
The KLoad function receives an url and a callback function as parameter, the callback function will be called after the resquest to the Kuneri Lite is done. (Remember that trace function result is not visible in device).
After this, create a Flash Lite file (.fla). In the first frame put a button that will to open a connection to Kuneri Lite, with ‘connect_btn’ as instance name. Also put a dynamic text with ‘info’ as instance name.
Create a layer (named ‘labels’) to put all labels and set the first frame name as ‘connect’. Create another layer (named ‘actions’) to put all actions. In the first frame of the ‘actions’ layer, paste the bellow code:
//Import the class KuneriLoader. import KuneriLoader; //Create a instance the KuneriLoader. var loader = new KuneriLoader(); var kuneriPath:String = "http://127.0.0.1:1001/Basic/"; _quality = "HIGH"; fscommand2("FullScreen", true); //Initially the bluetooth is started and response is returned to kuneriStarted. loader.KLoad(kuneriPath + "connect?klCommand=start&", kuneriStarted); //After the button click connect this function will call. connect_btn.onRelease = function():Void { //Connect the other device and the response will returned to kuneriConnected. loader.KLoad(kuneriPath + "connect?klCommand=connect&", kuneriConnected); info.text = "Waiting..."; } function kuneriConnected(res:LoadVars){ if (res.klError != 0) info.text = "Erro in connection."; } function kuneriStarted(res:LoadVars){ if (res.klError != 0){ info.text = "Erro while starting."; //Send again. loader.KLoad(kuneriPath + "connect?klCommand=start&", kuneriStarted); } else info.text = "Bluetooth started."; } function kuneriStatus(res:LoadVars){ if (res.klError != 0){ info.text = "Erro status."; } //Verify this status is connected with other device. else if (res.klStatus eq "connected"){ //Stop the update status. clearInterval(sts); gotoAndPlay("chat"); } } function getStatus():Void{ //Get status the kuneri Lite and response. loader.KLoad(kuneriPath + "connect?klCommand=status&", kuneriStatus); } //Each 700 milliseconds this status is updated. sts = setInterval(getStatus, 700); stop();
Yet in the ‘actions’ layer type the following code:
send_btn.onRelease = function():Void { if (msgText.text != ""){ //Sends your message to other user. loader.KLoad(kuneriPath + "connect?klCommand=send&klMessage=" + msgText.text + "&", kuneriSend); info.text = "Sending..."; } } function kuneriSend(res:LoadVars){ if (res.klError != 0) info.text = "Erro sending the message."; //After send the message, textChat will updated. else uptadeSendChat(); } function uptadeSendChat():Void { info.text = ""; //Update chatText. chatText.text += ">> " + msgText.text + "\n"; chatText.scroll = chatText.maxscroll; //Clear the msgText. msgText.text = ""; } update_btn.onRelease = function():Void { //Read the message sends from other user. loader.KLoad(kuneriPath + "connect?klCommand=read&", kuneriRead); info.text = "Reading..."; } function kuneriRead(res:LoadVars){ if (res.klError != 0) info.text = "Erro reading."; //After read the message, textChat will updated. else uptadeReadChat(res.klReceive); } function uptadeReadChat(msg:String):Void { info.text = ""; //Update the chatText. chatText.text += ">> " + msg + "\n"; chatText.scroll = chatText.maxscroll; } stop();
In the stage put other buttons with ‘update_btn’ and ’send_btn’ instance names, a TextInput component with ‘msgText’ instance name and two ‘Dynamic Text’ with ‘chatText’ and ‘info’ instance names respectively. Like showed in the Figure.
If you want to test your application, you must make a .sis file in the Kuneri Lite and install in the mobile phone. To get more informations about the creation of .sis files, click [1].
See more applications in Flash Lite Effort
Download source files.
--Felipe Sampaio 15 July 2008