/Users/detro/workspace-nb/JAEBI-BlueToothJ2MEClient/src/org/jaebi/midlet/bt/Listener.java

Vai alla documentazione di questo file.
00001 /*
00002  * Listener.java
00003  *
00004  * Created on 26 maggio 2005, 11.38
00005  *
00006  * Gestore di connessioni ad una bluetooth master device.
00007  * Implementa un thread che si mette in attesa di nuove connessioni bluetooth.
00008  * L'utilizzo di un thread è giustificato dal fatto che si vuole evitare che l'intera applicazione
00009  * che usa Listener si blocchi sul metodo acceptAndOpen() nel caso in cui non vi siano connessioni
00010  * TODO deve essere un sinlgeton!!
00011  *
00012  */
00013 
00014 package org.jaebi.midlet.bt;
00015 
00016 import java.io.IOException;
00017 import javax.bluetooth.BluetoothStateException;
00018 import javax.bluetooth.DataElement;
00019 import javax.bluetooth.DiscoveryAgent;
00020 import javax.bluetooth.LocalDevice;
00021 import javax.bluetooth.RemoteDevice;
00022 import javax.bluetooth.ServiceRecord;
00023 import javax.microedition.io.Connector;
00024 import javax.microedition.io.StreamConnection;
00025 import javax.microedition.io.StreamConnectionNotifier;
00026 import org.jaebi.midlet.bt.btException.BtInitException;
00027 
00028 
00034 public class Listener{
00035     
00036     // url del server bluetooth
00037     private StringBuffer url;
00038     private StreamConnectionNotifier notifier;
00039     
00040     //incapsula le informazioni riguardo il servizio offerto da questo server BT
00041     private ServiceRecord record;
00042     
00043     // riferimento alla local Device relativa alla periferica bluetooth su cui è in esecuzione il listner
00044     //TODO commentare meglio (cos'è una LocalDevice?vedi javadoc)
00045     private LocalDevice localDevice;
00046     
00047     //flag che indica se il server è disposto o meno ad accettare connessioni
00048     private boolean closed;
00049     
00075     //TODO overloadare questo costruttore per fargli prendere un DataElement (nel caso in cui qualcuno volesse settare degli attributi)
00076     public Listener(StringBuffer url) throws BtInitException {
00077         this.url = url;
00078         
00079         try{
00080             
00081             // memorizzo il riferimento alla local Device
00082             localDevice = LocalDevice.getLocalDevice();
00083             
00084             // imposto il discoverable mode per la local device
00085             if (!localDevice.setDiscoverable(DiscoveryAgent.GIAC)) {
00086                 throw new BtInitException("Can't set discoverable mode...");
00087             }
00088             
00089             //creo il service record
00090             notifier = (StreamConnectionNotifier) Connector.open(url.toString());
00091             
00092             //TODO debug 
00093             System.out.println("url che passo al notifier:" + url.toString());
00094         } 
00095         
00096         catch (BluetoothStateException e1) {
00097             throw new BtInitException("Impossibile inizializzare la periferica bluetooth");
00098             
00099         }
00100         
00101         catch(IOException e){
00102             throw new BtInitException("Impossibile inizializzare la periferica bluetooth");
00103         }
00104         
00105         //TODO debug
00106         System.out.println("sono il server ed ho indirizzo " + localDevice.getBluetoothAddress() );
00107         
00108         //recupero il Service record creato sopra
00109         record = localDevice.getRecord(notifier);
00110         
00111         //TODO bebug
00112         System.out.println("sono il server e questo è l'indirizzo per connettersi"+ record.getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false)); 
00113         
00114             
00115         //TODO debug
00116         int attr[];
00117         attr = record.getAttributeIDs();
00118         for (int i =0; i<attr.length; i++){
00119             System.out.println(attr[i]);
00120         }
00121         System.out.println((String)(record.getAttributeValue(256).getValue()));
00122                 
00123         closed = false;
00124     }
00125     
00130     public StreamConnection listen(){
00131         StreamConnection conn = null;
00132         try {
00133             conn = notifier.acceptAndOpen();
00134         } catch (IOException e) {
00135             // wrong client or interrupted - continue anyway
00136             ;
00137         }
00138         
00139         String remoteDeviceName ="";
00140         try{
00141             remoteDeviceName = RemoteDevice.getRemoteDevice(conn).getFriendlyName(true);
00142         }catch (IOException e){
00143            ;
00144         }
00145         //TODO debug
00146         System.out.println("Listner::Si è collegata la device con nome " + remoteDeviceName);
00147         return conn;
00148         
00149     }
00150     
00151     public boolean isClosed(){
00152         return closed;
00153     }
00154     
00155 }

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