/Users/detro/workspace-nb/JAEBI-BlueToothJ2MEClient/src/ClientMidlet.java

Vai alla documentazione di questo file.
00001 /*
00002  * ClientMidlet.java
00003  *
00004  * Created on 9 giugno 2005, 17.33
00005  */
00006 
00007 import java.io.DataInputStream;
00008 import java.io.DataOutputStream;
00009 import java.io.IOException;
00010 import java.util.Enumeration;
00011 import java.util.Hashtable;
00012 import java.util.Vector;
00013 import javax.bluetooth.DataElement;
00014 import javax.bluetooth.RemoteDevice;
00015 import javax.bluetooth.ServiceRecord;
00016 import javax.bluetooth.UUID;
00017 import javax.microedition.io.StreamConnection;
00018 import javax.microedition.lcdui.AlertType;
00019 import javax.microedition.lcdui.Image;
00020 import javax.microedition.midlet.*;
00021 import org.jaebi.midlet.bt.*;
00022 import org.jaebi.midlet.bt.btException.BtInitException;
00023 import org.jaebi.midlet.bt.btException.ConnectionException;
00024 import org.jaebi.midlet.bt.responseHandling.AccountingResponseHandler;
00025 import org.jaebi.midlet.bt.responseHandling.XResponseHandler;
00026 import org.jaebi.midlet.bt.discoveryHandling.DiscoveryReport;
00027 import org.jaebi.midlet.bt.discoveryHandling.DiscoveryEventHandler;
00028 import org.jaebi.midlet.bt.discoveryHandling.ServiceSearchError;
00029 import org.jaebi.midlet.bt.discoveryHandling.DiscoveryError;
00030 
00031 
00036 public class ClientMidlet extends MIDlet implements DiscoveryEventHandler, XResponseHandler, AccountingResponseHandler, javax.microedition.lcdui.CommandListener, Runnable{
00037     
00038     private BtClient client;
00039     private UUID UUIDset[];
00040     private int attrset[] = null;
00041     private Image serviceSearchInProgress = null;
00042     private Image serviceSearchDone = null;
00043     private Image serviceSearchError = null;
00044     
00045     
00046     //TODO invece di usare lengthhashtable usare i vector:quando si falsel'add nella list si falsel'add anche nel vector, così lengthdevice sono memorizzate sia in List che in Vector con lo stesso indice
00047     /*
00048      * hashtable degli indici del componente List deviceFounded memorizzati in
00049      * chiave della RemoteDevice cui si riferiscono
00050      * -utile nell'implementazione dell'handeler servicesFound()
00051      */
00052     private Hashtable deviceListIndex;
00053     
00054     /*
00055      * hashtable di vettori di ServiceRecord, memorizzati in chiave degli indici
00056      * di deviceFounded. Ogni elemento rappresenta i servizi
00057      * disponibili sulla device corrispondente all'indice in device founded in
00058      * chiave del quale è memorizzato in questa hashtable
00059      */
00060     private Hashtable services;
00061     
00069     private Hashtable serviceSearchErrorMsg;
00070     
00071     private static final UUID JAEBI_MIDLET_UUID = new UUID(0x12345);
00072     
00073     private StreamConnection conn;
00074     private DataInputStream streamFromServer;
00075     private DataOutputStream streamToServer;
00076     
00077     /*
00078      *__________________________________________________________________________
00079      *
00080      * Dichiarazione dei request Processor
00081      *__________________________________________________________________________
00082      *
00083      */
00084     private LoginRequestProcessor loginRequestProcessor;
00085     private SelectXRequestProcessor selectProcessor;
00086     
00088     public ClientMidlet() {
00089         this.UUIDset = new UUID[2];
00090         this.attrset = new int[1];
00091         
00092         //Consideriamo servizi del profilo btssp (vedi javadoc per classe UUID)
00093         UUIDset[0] = new UUID(0x1101);
00094         
00095         
00096         //e in particolare al servizio JAEBI midlet
00097         UUIDset[1] = JAEBI_MIDLET_UUID;
00098         
00099         //degli attributi ci interssa solo il nome del servizio
00100         attrset[0] = 0x100;
00101         
00102         try{
00103             client = new BtClient(UUIDset, attrset, this);
00104         } catch (BtInitException e){
00105             
00106             //visualizzo l'alert di errore con un opportuno messaggio
00107             get_alertError().setTitle("Errore irreversibile");
00108             get_alertError().setString(e.getMessage());
00109             javax.microedition.lcdui.Display.getDisplay(this).setCurrent(get_alertError());
00110             e.printStackTrace();
00111         }
00112         
00113         this.deviceListIndex = new Hashtable();
00114         this.services = new Hashtable();
00115         this.serviceSearchErrorMsg = new Hashtable();  
00116         this.loginRequestProcessor = new LoginRequestProcessor();
00117         this.selectProcessor = new SelectXRequestProcessor();
00118         
00119         
00120         
00121         
00122     }
00123     
00124 // --- This section is auto-generated by NetBeans IDE. Do not modify or you may lose your changes.//<editor-fold id="MVDMethods" defaultstate="collapsed" desc="This section is auto-generated by NetBeans IDE.">//GEN-BEGIN:MVDMethods
00128     private void initialize() {
00129 // For adding user code into this block, select "Design" item in the inspector and invoke property editor on Action property in Properties window.
00130         javax.microedition.lcdui.Display.getDisplay(this).setCurrent(get_alertSplashScreen());
00131     }
00132     
00138     public void commandAction(javax.microedition.lcdui.Command command, javax.microedition.lcdui.Displayable displayable) {
00139         if (displayable == alertDiscoveryStart) {
00140             if (command == okCommandDiscoveryStart) {
00141 // For adding user code into this block, select "Design | Screens | alertDiscoveryStart [Alert] | Assigned Commands | okCommandDiscoveryStart" item in the inspector and invoke property editor on Action property in Properties window.
00142                 client.startDiscovery();
00143                 setupServiceSearchIndicator();
00144                 javax.microedition.lcdui.Display.getDisplay(this).setCurrent(get_deviceFounded());
00145             }
00146         } else if (displayable == alertError) {
00147             if (command == exitBluetoothInitError) {
00148 // For adding user code into this block, select "Design | Screens | alertError [Alert] | Assigned Commands | exitBluetoothInitError" item in the inspector and invoke property editor on Action property in Properties window.
00149                 javax.microedition.lcdui.Display.getDisplay(this).setCurrent(null);
00150                 destroyApp(true);
00151                 notifyDestroyed();
00152             }
00153         } else if (displayable == alertLoginCorrect) {
00154             if (command == okCommandLogincorrect) {
00155 // For adding user code into this block, select "Design | Screens | alertLoginCorrect [Alert] | Assigned Commands | okCommandLogincorrect" item in the inspector and invoke property editor on Action property in Properties window.
00156                 javax.microedition.lcdui.Display.getDisplay(this).setCurrent(get_userChoice());
00157             }
00158         } else if (displayable == alertLoginIncorrect) {
00159             if (command == okCommandLoginCorrect) {
00160 // For adding user code into this block, select "Design | Screens | alertLoginIncorrect [Alert] | Assigned Commands | okCommandLoginCorrect" item in the inspector and invoke property editor on Action property in Properties window.
00161                 javax.microedition.lcdui.Display.getDisplay(this).setCurrent(get_loginScreen());
00162             }
00163         } else if (displayable == deviceFounded) {
00164             if (command == ViewServices) {
00165 // For adding user code into this block, select "Design | Screens | deviceFounded [List] | Assigned Commands | ViewServices" item in the inspector and invoke property editor on Action property in Properties window.
00166                 get_servicesList().deleteAll();
00167                 
00168 //recupero l'indice della device di cui si vuole visualizzare i servizi
00169                 Integer index = new Integer( get_deviceFounded().getSelectedIndex());
00170                 
00171 //controllo se ci sono stati errori durante la ricerca dei servizi su questa device
00172                 if (serviceSearchErrorMsg.containsKey(index)){
00173                     
00174                     //visualizzo l'alert di errore con un opportuno messaggio
00175                     get_alertService().setTitle("Errore");
00176                     get_alertService().setString((String)serviceSearchErrorMsg.get(index));
00177                     javax.microedition.lcdui.Display.getDisplay(this).setCurrent(get_alertService());
00178                 } else{
00179                     //controllo se la ricerca dei servizi su questa device è terminata
00180                     if (!services.containsKey(index)){
00181                         
00182                         //visualizzo l'alert di errore con un opportuno messaggio
00183                         get_alertService().setTitle("Attenzione");
00184                         get_alertService().setString("La ricerca di servizi per questa device non è ancora terminata");
00185                         javax.microedition.lcdui.Display.getDisplay(this).setCurrent(get_alertService());
00186                     } else{
00187         /*______________________________
00188          *visualizzo i servizi
00189          *______________________________
00190          */
00191                         get_servicesList().getTicker().setString("Elenco servizi disponibili per questa periferica");
00192                         Vector servicesOnDevice = (Vector)services.get(index);
00193                         
00194                         for (int i=0; i<servicesOnDevice.size(); i++){
00195                             //recupero il servizio ed il relativo nome
00196                             ServiceRecord service = (ServiceRecord)servicesOnDevice.elementAt(i);
00197                             String serviceName = "Servizio senza nome " + i;
00198                             
00199             /*
00200              * per il perchè si esegua questa operazione per recuperare il nome
00201              * del servizio si rimanda al javadoc per javax.bluetooth.ServiceRecord
00202              */
00203                             DataElement nameElement = service.getAttributeValue(256);
00204                             
00205             /*
00206              ** Una cosa è poco chiara. l'offset per recuperare il service name è 0 e
00207              * l'offset è 256. nonostante 256 faccia parte dell'attrlist con cui si
00208              *istanzia BtClient, l'attributo col nome del servizio non viene ritornato
00209              */
00210                             if( (nameElement != null) && nameElement.getDataType() == DataElement.STRING){
00211                                 serviceName = (String)(nameElement.getValue());
00212                             }
00213                             
00214                             get_servicesList().append(serviceName,null);
00215                         }
00216                         javax.microedition.lcdui.Display.getDisplay(this).setCurrent(get_servicesList());
00217                     }
00218                     
00219                 }
00220             } else if (command == stopCommand1) {
00221 // For adding user code into this block, select "Design | Screens | deviceFounded [List] | Assigned Commands | stopCommand1" item in the inspector and invoke property editor on Action property in Properties window.
00222 //client.cancelDiscovery();
00223                 System.out.println("funziona");
00224             }
00225         } else if (displayable == loginScreen) {
00226             if (command == okCommandLoginScreen) {
00227 // For adding user code into this block, select "Design | Screens | loginScreen [TextBox] | Assigned Commands | okCommandLoginScreen" item in the inspector and invoke property editor on Action property in Properties window.
00228                 this.loginRequestProcessor.setNickName(get_loginScreen().getString());
00229                 this.loginRequestProcessor.processRequest(this,this.streamFromServer, this.streamToServer);
00230 //TODO debug
00231                 System.out.println("Client login screen: ha fatto quello che dovrebbe fare");
00232             }
00233         } else if (displayable == selectResultbox) {
00234             if (command == backCommandSelectResultBox) {
00235 // For adding user code into this block, select "Design | Screens | selectResultbox [TextBox] | Assigned Commands | backCommandSelectResultBox" item in the inspector and invoke property editor on Action property in Properties window.
00236                 javax.microedition.lcdui.Display.getDisplay(this).setCurrent(get_tableNameBox());
00237             }
00238         } else if (displayable == servicesList) {
00239             if (command == backToDeviceFounded) {
00240 // For adding user code into this block, select "Design | Screens | servicesList [List] | Assigned Commands | backToDeviceFounded" item in the inspector and invoke property editor on Action property in Properties window.
00241                 javax.microedition.lcdui.Display.getDisplay(this).setCurrent(get_deviceFounded());
00242             } else if (command == connect) {
00243 // For adding user code into this block, select "Design | Screens | servicesList [List] | Assigned Commands | connect" item in the inspector and invoke property editor on Action property in Properties window.
00244                 new Thread(this).start();
00245                 javax.microedition.lcdui.Display.getDisplay(this).setCurrent(get_loginScreen());
00246             }
00247         } else if (displayable == tableNameBox) {
00248             if (command == okCommandTableNameBox) {
00249 // For adding user code into this block, select "Design | Screens | tableNameBox [TextBox] | Assigned Commands | okCommandTableNameBox" item in the inspector and invoke property editor on Action property in Properties window.
00250                 selectProcessor.setTableName(get_tableNameBox().getString());
00251                 selectProcessor.processRequest(this, this.streamFromServer, this.streamToServer);
00252             }
00253         } else if (displayable == userChoice) {
00254             if (command == okCommandUserChoice) {
00255 // For adding user code into this block, select "Design | Screens | userChoice [List] | Assigned Commands | okCommandUserChoice" item in the inspector and invoke property editor on Action property in Properties window.
00256                 
00257                 int index = get_userChoice().getSelectedIndex();
00258                 if (index == 0){
00259                     javax.microedition.lcdui.Display.getDisplay(this).setCurrent(get_tableNameBox());
00260                 }
00261                 
00262                 if (index == 1){
00263                     //TODO esegui logout
00264                 }
00265             }
00266         }
00267     }
00268     
00273     private javax.microedition.lcdui.List get_deviceFounded() {
00274         if (deviceFounded == null) {
00275             deviceFounded = new javax.microedition.lcdui.List("Inserire user name", javax.microedition.lcdui.Choice.IMPLICIT, new java.lang.String[0], new javax.microedition.lcdui.Image[0]);
00276             deviceFounded.addCommand(get_stopCommand1());
00277             deviceFounded.addCommand(get_ViewServices());
00278             deviceFounded.setCommandListener(this);
00279             deviceFounded.setTicker(get_tickerDeviceSearch());
00280             deviceFounded.setSelectedFlags(new boolean[0]);
00281             deviceFounded.setSelectCommand(get_ViewServices());
00282         }
00283         return deviceFounded;
00284     }
00285     
00290     private javax.microedition.lcdui.Alert get_alertDiscoveryStart() {
00291         if (alertDiscoveryStart == null) {
00292             alertDiscoveryStart = new javax.microedition.lcdui.Alert("JAEBI Midlet Bluetooth client\n", "Cominciare ora la ricerca delle device Bluetooth?", null, javax.microedition.lcdui.AlertType.INFO);
00293             alertDiscoveryStart.addCommand(get_okCommandDiscoveryStart());
00294             alertDiscoveryStart.setCommandListener(this);
00295             alertDiscoveryStart.setTimeout(-2);
00296         }
00297         return alertDiscoveryStart;
00298     }
00299     
00304     private javax.microedition.lcdui.Alert get_alertError() {
00305         if (alertError == null) {
00306             alertError = new javax.microedition.lcdui.Alert("Errore irreversibile", "<Enter Text>", null, javax.microedition.lcdui.AlertType.INFO);
00307             alertError.addCommand(get_exitBluetoothInitError());
00308             alertError.setCommandListener(this);
00309             alertError.setTimeout(-2);
00310         }
00311         return alertError;
00312     }
00313     
00318     private javax.microedition.lcdui.Command get_okCommandDiscoveryStart() {
00319         if (okCommandDiscoveryStart == null) {
00320             okCommandDiscoveryStart = new javax.microedition.lcdui.Command("Ok", javax.microedition.lcdui.Command.OK, 1);
00321         }
00322         return okCommandDiscoveryStart;
00323     }
00324     
00329     private javax.microedition.lcdui.Command get_exitBluetoothInitError() {
00330         if (exitBluetoothInitError == null) {
00331             exitBluetoothInitError = new javax.microedition.lcdui.Command("Exit", javax.microedition.lcdui.Command.EXIT, 1);
00332         }
00333         return exitBluetoothInitError;
00334     }
00335     
00340     private javax.microedition.lcdui.Ticker get_tickerDeviceSearch() {
00341         if (tickerDeviceSearch == null) {
00342             tickerDeviceSearch = new javax.microedition.lcdui.Ticker("Ricerca delle device in corso");
00343         }
00344         return tickerDeviceSearch;
00345     }
00346     
00351     private javax.microedition.lcdui.Command get_stopCommand1() {
00352         if (stopCommand1 == null) {
00353             stopCommand1 = new javax.microedition.lcdui.Command("Stop", javax.microedition.lcdui.Command.STOP, 1);
00354         }
00355         return stopCommand1;
00356     }
00357     
00362     private javax.microedition.lcdui.Command get_ViewServices() {
00363         if (ViewServices == null) {
00364             ViewServices = new javax.microedition.lcdui.Command("Servizi", "Visualizza i servizi disponibili su questa device", javax.microedition.lcdui.Command.ITEM, 1);
00365         }
00366         return ViewServices;
00367     }
00368     
00373     private javax.microedition.lcdui.List get_servicesList() {
00374         if (servicesList == null) {
00375             servicesList = new javax.microedition.lcdui.List("Inserire lo username", javax.microedition.lcdui.Choice.EXCLUSIVE, new java.lang.String[0], new javax.microedition.lcdui.Image[0]);
00376             servicesList.addCommand(get_backToDeviceFounded());
00377             servicesList.addCommand(get_connect());
00378             servicesList.setCommandListener(this);
00379             servicesList.setTicker(get_tickerServiceList());
00380             servicesList.setSelectedFlags(new boolean[0]);
00381             servicesList.setSelectCommand(get_connect());
00382         }
00383         return servicesList;
00384     }
00385     
00390     private javax.microedition.lcdui.Command get_backToDeviceFounded() {
00391         if (backToDeviceFounded == null) {
00392             backToDeviceFounded = new javax.microedition.lcdui.Command("Back", javax.microedition.lcdui.Command.BACK, 1);
00393         }
00394         return backToDeviceFounded;
00395     }
00396     
00401     private javax.microedition.lcdui.Alert get_alertSplashScreen() {
00402         if (alertSplashScreen == null) {
00403             alertSplashScreen = new javax.microedition.lcdui.Alert("", "", get_splashScreen(), javax.microedition.lcdui.AlertType.INFO);
00404             alertSplashScreen.setTimeout(3000);
00405             javax.microedition.lcdui.Display.getDisplay(this).setCurrent(get_alertDiscoveryStart());
00406         }
00407         return alertSplashScreen;
00408     }
00409     
00414     private javax.microedition.lcdui.Image get_splashScreen() {
00415         if (splashScreen == null) {
00416             try {
00417                 splashScreen = javax.microedition.lcdui.Image.createImage("/images/jaebi LogoFinal.jpg");
00418             } catch (java.io.IOException exception) {
00419             }
00420         }
00421         return splashScreen;
00422     }
00423     
00428     private javax.microedition.lcdui.Alert get_alertService() {
00429         if (alertService == null) {
00430             alertService = new javax.microedition.lcdui.Alert("alert service", "<Enter Text>", null, javax.microedition.lcdui.AlertType.WARNING);
00431             alertService.setTimeout(-2);
00432         }
00433         return alertService;
00434     }
00435     
00440     private javax.microedition.lcdui.Ticker get_tickerServiceList() {
00441         if (tickerServiceList == null) {
00442             tickerServiceList = new javax.microedition.lcdui.Ticker("<Enter Ticker Text>");
00443         }
00444         return tickerServiceList;
00445     }
00446     
00451     private javax.microedition.lcdui.Command get_connect() {
00452         if (connect == null) {
00453             connect = new javax.microedition.lcdui.Command("Connetti", javax.microedition.lcdui.Command.ITEM, 1);
00454         }
00455         return connect;
00456     }
00457     
00462     private javax.microedition.lcdui.TextBox get_loginScreen() {
00463         if (loginScreen == null) {
00464             loginScreen = new javax.microedition.lcdui.TextBox("Inserisci lo user name", "", 120, 0x0);
00465             loginScreen.addCommand(get_okCommandLoginScreen());
00466             loginScreen.setCommandListener(this);
00467         }
00468         return loginScreen;
00469     }
00470     
00475     private javax.microedition.lcdui.Command get_okCommandLoginScreen() {
00476         if (okCommandLoginScreen == null) {
00477             okCommandLoginScreen = new javax.microedition.lcdui.Command("Ok", javax.microedition.lcdui.Command.OK, 1);
00478         }
00479         return okCommandLoginScreen;
00480     }
00481     
00486     private javax.microedition.lcdui.Alert get_alertLoginIncorrect() {
00487         if (alertLoginIncorrect == null) {
00488             alertLoginIncorrect = new javax.microedition.lcdui.Alert("Errore", "<Enter Text>", null, javax.microedition.lcdui.AlertType.ERROR);
00489             alertLoginIncorrect.addCommand(get_okCommandLoginCorrect());
00490             alertLoginIncorrect.setCommandListener(this);
00491             alertLoginIncorrect.setTimeout(-2);
00492         }
00493         return alertLoginIncorrect;
00494     }
00495     
00500     private javax.microedition.lcdui.Command get_okCommandLoginCorrect() {
00501         if (okCommandLoginCorrect == null) {
00502             okCommandLoginCorrect = new javax.microedition.lcdui.Command("Ok", javax.microedition.lcdui.Command.OK, 1);
00503         }
00504         return okCommandLoginCorrect;
00505     }
00506     
00511     private javax.microedition.lcdui.Command get_updateValue() {
00512         if (updateValue == null) {
00513             updateValue = new javax.microedition.lcdui.Command("Modifica", javax.microedition.lcdui.Command.SCREEN, 1);
00514         }
00515         return updateValue;
00516     }
00517     
00522     private javax.microedition.lcdui.Alert get_alertLoginCorrect() {
00523         if (alertLoginCorrect == null) {
00524             alertLoginCorrect = new javax.microedition.lcdui.Alert(null, "<Enter Text>", null, javax.microedition.lcdui.AlertType.INFO);
00525             alertLoginCorrect.addCommand(get_okCommandLogincorrect());
00526             alertLoginCorrect.setCommandListener(this);
00527             alertLoginCorrect.setTimeout(-2);
00528         }
00529         return alertLoginCorrect;
00530     }
00531     
00536     private javax.microedition.lcdui.Command get_okCommandLogincorrect() {
00537         if (okCommandLogincorrect == null) {
00538             okCommandLogincorrect = new javax.microedition.lcdui.Command("Ok", javax.microedition.lcdui.Command.OK, 1);
00539         }
00540         return okCommandLogincorrect;
00541     }
00542     
00547     private javax.microedition.lcdui.List get_userChoice() {
00548         if (userChoice == null) {
00549             userChoice = new javax.microedition.lcdui.List("Seleziona l\'operazione da eseguire", javax.microedition.lcdui.Choice.IMPLICIT, new java.lang.String[0], new javax.microedition.lcdui.Image[0]);
00550             userChoice.addCommand(get_okCommandUserChoice());
00551             userChoice.setCommandListener(this);
00552             userChoice.setSelectedFlags(new boolean[0]);
00553             get_userChoice().append("Select", null);
00554             get_userChoice().append("Logout", null);
00555         }
00556         return userChoice;
00557     }
00558     
00563     private javax.microedition.lcdui.Command get_okCommandUserChoice() {
00564         if (okCommandUserChoice == null) {
00565             okCommandUserChoice = new javax.microedition.lcdui.Command("Ok", javax.microedition.lcdui.Command.OK, 1);
00566         }
00567         return okCommandUserChoice;
00568     }
00569     
00574     private javax.microedition.lcdui.TextBox get_tableNameBox() {
00575         if (tableNameBox == null) {
00576             tableNameBox = new javax.microedition.lcdui.TextBox("Inserisci il nome della tabella", null, 120, 0x0);
00577             tableNameBox.addCommand(get_okCommandTableNameBox());
00578             tableNameBox.setCommandListener(this);
00579         }
00580         return tableNameBox;
00581     }
00582     
00587     private javax.microedition.lcdui.Command get_okCommandTableNameBox() {
00588         if (okCommandTableNameBox == null) {
00589             okCommandTableNameBox = new javax.microedition.lcdui.Command("Ok", javax.microedition.lcdui.Command.OK, 1);
00590         }
00591         return okCommandTableNameBox;
00592     }
00593     
00598     private javax.microedition.lcdui.TextBox get_selectResultbox() {
00599         if (selectResultbox == null) {
00600             selectResultbox = new javax.microedition.lcdui.TextBox(null, null, 120, 0x0);
00601             selectResultbox.addCommand(get_backCommandSelectResultBox());
00602             selectResultbox.setCommandListener(this);
00603         }
00604         return selectResultbox;
00605     }
00606     
00611     private javax.microedition.lcdui.Command get_backCommandSelectResultBox() {
00612         if (backCommandSelectResultBox == null) {
00613             backCommandSelectResultBox = new javax.microedition.lcdui.Command("Back", javax.microedition.lcdui.Command.BACK, 1);
00614         }
00615         return backCommandSelectResultBox;
00616     }
00617     
00618     javax.microedition.lcdui.List deviceFounded;
00619     javax.microedition.lcdui.Alert alertDiscoveryStart;
00620     javax.microedition.lcdui.Alert alertError;
00621     javax.microedition.lcdui.Command okCommandDiscoveryStart;
00622     javax.microedition.lcdui.Command exitBluetoothInitError;
00623     javax.microedition.lcdui.Ticker tickerDeviceSearch;
00624     javax.microedition.lcdui.Command stopCommand1;
00625     javax.microedition.lcdui.Command ViewServices;
00626     javax.microedition.lcdui.List servicesList;
00627     javax.microedition.lcdui.Command backToDeviceFounded;
00628     javax.microedition.lcdui.Alert alertSplashScreen;
00629     javax.microedition.lcdui.Image splashScreen;
00630     javax.microedition.lcdui.Alert alertService;
00631     javax.microedition.lcdui.Ticker tickerServiceList;
00632     javax.microedition.lcdui.Command connect;
00633     javax.microedition.lcdui.TextBox loginScreen;
00634     javax.microedition.lcdui.Command okCommandLoginScreen;
00635     javax.microedition.lcdui.Alert alertLoginIncorrect;
00636     javax.microedition.lcdui.Command okCommandLoginCorrect;
00637     javax.microedition.lcdui.Command updateValue;
00638     javax.microedition.lcdui.Alert alertLoginCorrect;
00639     javax.microedition.lcdui.Command okCommandLogincorrect;
00640     javax.microedition.lcdui.List userChoice;
00641     javax.microedition.lcdui.Command okCommandUserChoice;
00642     javax.microedition.lcdui.TextBox tableNameBox;
00643     javax.microedition.lcdui.Command okCommandTableNameBox;
00644     javax.microedition.lcdui.TextBox selectResultbox;
00645     javax.microedition.lcdui.Command backCommandSelectResultBox;
00646 // --- This is the end of auto-generated section.//</editor-fold>//GEN-END:MVDMethods
00647     
00648     public void startApp() {
00649         initialize();
00650         
00651     }
00652     
00653     public void pauseApp() {
00654     }
00655     
00656     public void destroyApp(boolean unconditional) {
00657     }
00658     
00659     
00665     public void run(){
00666        connect(); 
00667     }
00668     
00669     
00670     private void connect(){
00671         /*
00672          * recupero l'indice del servizio con cui è memorizzato nel vector a sua volta
00673          * presente in services
00674          * NB: i servizi vengono caricati nella list nello stesso ordine in cui sono
00675          * presenti nel Vector
00676          */
00677         int serviceIndex = get_servicesList().getSelectedIndex();
00678         String url ="";
00679         
00680         /*
00681          * recupero il valore chiave con cui è memorizzato il vettore di servizi
00682          * relativo alla device cui ci si vuole connetterre
00683          */
00684         Integer key = new Integer(get_deviceFounded().getSelectedIndex());
00685         Vector serviceVector = (Vector)services.get(key);
00686         ServiceRecord service = (ServiceRecord)serviceVector.elementAt(serviceIndex);
00687         
00688         try{
00689             conn = client.getConnection(service, ServiceRecord.NOAUTHENTICATE_NOENCRYPT,  false);
00690             this.streamFromServer = conn.openDataInputStream();
00691             this.streamToServer = conn.openDataOutputStream();
00692         }
00693         catch (ConnectionException e){
00694             /*
00695              * Non è possibile instaurare la connessione. visualizo l'opportuno alert
00696              */
00697             get_alertService().setTitle("Errore");
00698             get_alertService().setString(e.getMessage());
00699             javax.microedition.lcdui.Display.getDisplay(this).setCurrent(get_alertService());
00700         }
00701         
00702          catch (IOException e){
00703             /*
00704              * Non è possibile instaurare la connessione. visualizo l'opportuno alert
00705              */
00706             get_alertService().setTitle("Errore");
00707             get_alertService().setString(e.getMessage());
00708             javax.microedition.lcdui.Display.getDisplay(this).setCurrent(get_alertService());
00709         }
00710         
00711         
00712         //TODO se la connessione va a buon fine bisogna svuotare le hashtable per ottimizzare memoria
00713         //TODO la prima cosa che devo fare è inviare la login alla midlet server
00714         
00715     }
00716     
00717     
00718     private void setupServiceSearchIndicator(){
00719         try{
00720             serviceSearchInProgress = Image.createImage("/images/searching.png");
00721         } catch(IOException e){
00722             //TODO si può creare un off screen image (?) come in example.bluetooth.demo.ServerMIDlet.setupIndicatorImage()
00723             ;
00724         }
00725         
00726         try{
00727             serviceSearchDone = Image.createImage("/images/searchDone.png");
00728         } catch(IOException e){
00729             //TODO si può creare un off screen image (?) come in example.bluetooth.demo.ServerMIDlet.setupIndicatorImage()
00730             ;
00731         }
00732         
00733         try{
00734             serviceSearchError = Image.createImage("/images/noServiceFound.png");
00735         } catch(IOException e){
00736             //TODO si può creare un off screen image (?) come in example.bluetooth.demo.ServerMIDlet.setupIndicatorImage()
00737             ;
00738         }
00739         
00740     }
00741     
00751     public void discoveryStarted(){
00752         System.out.println("discovery partito");
00753  
00754     }
00755     
00756     
00757     public void discoveryError(DiscoveryError error){
00758         //visualizzo l'alert di errore con un opportuno messaggio
00759         get_alertError().setString(error.getMsg());
00760         javax.microedition.lcdui.Display.getDisplay(this).setCurrent(get_alertError());
00761         
00762     }
00763     
00764     public void serviceSearchStarted(Enumeration devices){
00765         System.out.println("Ricerca servizi iniziata");
00766         
00767         get_tickerDeviceSearch().setString("Ricerca servizi sulle device trovate in corso");
00768         
00769         while(devices.hasMoreElements()){
00770             RemoteDevice rd = (RemoteDevice)devices.nextElement();
00771             String deviceName;
00772             try{
00773                 deviceName = rd.getFriendlyName(true);
00774             } catch(IOException e){
00775                 deviceName = rd.getBluetoothAddress();
00776             }
00777             
00778             //aggiorno il componente deviceFounded
00779             int index = get_deviceFounded().append(deviceName, serviceSearchInProgress);
00780             
00781             //..e l'hashtable deviceListIndex
00782             deviceListIndex.put(rd, new Integer(index));
00783         }
00784         
00785     }
00786     
00787     public void serviceSearchError(ServiceSearchError error){
00788         System.out.println("Errore durante la ricerca dei servizi " + error.getMsg());
00789         
00790         //aggiorno la remote device dall'error
00791         RemoteDevice rd = error.getRemoteDevice();
00792         
00793         /*
00794          * Se la condizione di questo if non è vera, significa che è finita una
00795          * ricerca con id non previsto
00796          */
00797         if (rd != null){
00798             //recupero l'indice in deviceFounded corrispondente a rd
00799             Integer index = (Integer)deviceListIndex.get(rd);
00800             
00801             //aggiorno l'immagine
00802             String stringPart = get_deviceFounded().getString(index.intValue());
00803             get_deviceFounded().set(index.intValue(), stringPart, serviceSearchError);
00804             
00805             //aggiorno l'hashtable delgi errori di ricerca servizi
00806             serviceSearchErrorMsg.put(index, error.getMsg());
00807         }
00808         
00809         
00810         
00811     }
00812     
00813     
00814     public void servicesFound(Vector services, RemoteDevice rd){
00815         System.out.println("Servizio trovato");
00816         
00817         //recupero l'indice in deviceFounded corrispondente a rd
00818         Integer index = (Integer)deviceListIndex.get(rd);
00819         
00820         //aggiorno l'immagine
00821         String stringPart = get_deviceFounded().getString(index.intValue());
00822         get_deviceFounded().set(index.intValue(), stringPart, serviceSearchDone);
00823         
00824         
00825         //aggiorno l'hashtable di servizi
00826         this.services.put(index, services);
00827     }
00828     
00829     
00830     public void discoveryCompleted(DiscoveryReport report){
00831         
00832         String msg = "Discovery completato";
00833         System.out.println(msg);
00834         if(report.deviceCount() == 0){
00835             msg += "nessuna periferica trovata";
00836             System.out.println("nessuna periferica trovata");
00837         }
00838         get_tickerDeviceSearch().setString(msg);
00839         
00840     }
00841     
00850     public void connectionError(){
00851        
00852         try{
00853             this.conn.close();
00854         }
00855         
00856         catch(IOException e){
00857             ;
00858         }
00859         
00860         finally{
00861             this.conn = null;
00862         }
00863      
00864     }
00865     
00866     public void handleMessageResponse(String message){
00867        get_selectResultbox().setString(message);
00868     }
00869     
00870     public void handleRowResponse(Vector rows){
00871         //TODO implementami
00872     }
00873     
00874     public void loginIncorrect(String msg){
00875         
00876         /*
00877          *la login non è andata a buon fine. visualizzo un opportuno dialog 
00878          */ 
00879         get_alertLoginIncorrect().setString(msg);
00880         javax.microedition.lcdui.Display.getDisplay(this).setCurrent(get_alertLoginIncorrect());
00881     }
00882     
00883     public void loginCorrect(String msg){
00884         /*
00885          *la login  è andata a buon fine. visualizzo un opportuno dialog 
00886          */ 
00887         get_alertLoginCorrect().setString(msg);
00888         javax.microedition.lcdui.Display.getDisplay(this).setCurrent(get_alertLoginCorrect());
00889         
00890     }
00891 }

Generato il Thu Jun 23 00:02:59 2005 per JAEBI - BlueTooth J2ME Midlet Client da  doxygen 1.4.3