00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
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
00037 private StringBuffer url;
00038 private StreamConnectionNotifier notifier;
00039
00040
00041 private ServiceRecord record;
00042
00043
00044
00045 private LocalDevice localDevice;
00046
00047
00048 private boolean closed;
00049
00075
00076 public Listener(StringBuffer url) throws BtInitException {
00077 this.url = url;
00078
00079 try{
00080
00081
00082 localDevice = LocalDevice.getLocalDevice();
00083
00084
00085 if (!localDevice.setDiscoverable(DiscoveryAgent.GIAC)) {
00086 throw new BtInitException("Can't set discoverable mode...");
00087 }
00088
00089
00090 notifier = (StreamConnectionNotifier) Connector.open(url.toString());
00091
00092
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
00106 System.out.println("sono il server ed ho indirizzo " + localDevice.getBluetoothAddress() );
00107
00108
00109 record = localDevice.getRecord(notifier);
00110
00111
00112 System.out.println("sono il server e questo è l'indirizzo per connettersi"+ record.getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false));
00113
00114
00115
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
00136 ;
00137 }
00138
00139 String remoteDeviceName ="";
00140 try{
00141 remoteDeviceName = RemoteDevice.getRemoteDevice(conn).getFriendlyName(true);
00142 }catch (IOException e){
00143 ;
00144 }
00145
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 }