00001
00002
00003
00004
00005
00006
00007 import java.io.IOException;
00008 import java.rmi.RemoteException;
00009 import java.util.Vector;
00010 import javax.bluetooth.RemoteDevice;
00011 import javax.bluetooth.UUID;
00012 import javax.microedition.io.StreamConnection;
00013 import javax.microedition.midlet.*;
00014 import org.jaebi.midlet.bt.btException.BtInitException;
00015 import org.jaebi.midlet.bt.*;
00016
00017
00022 public class ServerMidlet extends MIDlet implements javax.microedition.lcdui.CommandListener, ConnectionEventHandler {
00023
00024 private StringBuffer url;
00025 private BtServer btServer;
00026
00027 private String serviceURL = "http://192.168.1.11:8080/JAEBI/JAEBIWS";
00028
00029
00030
00031 private static final UUID JAEBI_MIDLET_UUID = new UUID(0x12345);
00032
00033
00034
00035
00036
00037
00038 private Vector connected;
00039
00040
00041
00042
00043
00044
00045
00046 private Vector connections;
00047
00056 private Vector connectionProcessors;
00057
00059 public ServerMidlet() {
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069 url = new StringBuffer("btspp://");
00070
00071
00072 url.append("localhost").append(':');
00073
00074
00075 url.append(JAEBI_MIDLET_UUID.toString());
00076
00077
00078 url.append(";name=Midlet_Server");
00079
00080
00081
00082 url.append(";authorize=true");
00083
00084
00085 System.out.println("UUID=" + JAEBI_MIDLET_UUID.toString());
00086
00087 this.connected = new Vector();
00088 this.connections = new Vector();
00089 this.connectionProcessors = new Vector();
00090
00091 }
00092
00093
00097 private void initialize() {
00098
00099 javax.microedition.lcdui.Display.getDisplay(this).setCurrent(get_alertSplashScreen());
00100 }
00101
00107 public void commandAction(javax.microedition.lcdui.Command command, javax.microedition.lcdui.Displayable displayable) {
00108 if (displayable == connectedDevice) {
00109 if (command == info) {
00110
00111 javax.microedition.lcdui.Display.getDisplay(this).setCurrent(get_deviceInfo(), get_connectedDevice());
00112 }
00113 } else if (displayable == initAlert) {
00114 if (command == exitCommand1) {
00115
00116 javax.microedition.lcdui.Display.getDisplay(this).setCurrent(null);
00117 destroyApp(true);
00118 notifyDestroyed();
00119 }
00120 }
00121 }
00122
00127 private javax.microedition.lcdui.Alert get_initAlert() {
00128 if (initAlert == null) {
00129 initAlert = new javax.microedition.lcdui.Alert(null, "Si \u00E8 verificato un errore durante l\'inizializzazione della periferica bluetooth", null, javax.microedition.lcdui.AlertType.INFO);
00130 initAlert.addCommand(get_exitCommand1());
00131 initAlert.setCommandListener(this);
00132 initAlert.setTimeout(-2);
00133 }
00134 return initAlert;
00135 }
00136
00141 private javax.microedition.lcdui.Command get_exitCommand1() {
00142 if (exitCommand1 == null) {
00143 exitCommand1 = new javax.microedition.lcdui.Command("Exit", javax.microedition.lcdui.Command.EXIT, 1);
00144 }
00145 return exitCommand1;
00146 }
00147
00152 private javax.microedition.lcdui.Ticker get_ticker1() {
00153 if (ticker1 == null) {
00154 ticker1 = new javax.microedition.lcdui.Ticker("In attesa di connesioni da parte di altre device");
00155 }
00156 return ticker1;
00157 }
00158
00163 private javax.microedition.lcdui.Alert get_alertSplashScreen() {
00164 if (alertSplashScreen == null) {
00165 alertSplashScreen = new javax.microedition.lcdui.Alert(null, "", get_logo(), javax.microedition.lcdui.AlertType.INFO);
00166 alertSplashScreen.setTimeout(3000);
00167 try{
00168 btServer = new BtServer(this.url, this);
00169 }catch(BtInitException e){
00170 javax.microedition.lcdui.Display.getDisplay(this).setCurrent(get_initAlert());
00171 e.printStackTrace();
00172 }
00173 javax.microedition.lcdui.Display.getDisplay(this).setCurrent(get_connectedDevice());
00174 }
00175 return alertSplashScreen;
00176 }
00177
00182 private javax.microedition.lcdui.Image get_logo() {
00183 if (logo == null) {
00184 try {
00185 logo = javax.microedition.lcdui.Image.createImage("/images/jaebi LogoFinal.jpg");
00186 } catch (java.io.IOException exception) {
00187 }
00188 }
00189 return logo;
00190 }
00191
00196 private javax.microedition.lcdui.List get_connectedDevice() {
00197 if (connectedDevice == null) {
00198 connectedDevice = new javax.microedition.lcdui.List(null, javax.microedition.lcdui.Choice.IMPLICIT, new java.lang.String[0], new javax.microedition.lcdui.Image[0]);
00199 connectedDevice.addCommand(get_Disconnect());
00200 connectedDevice.addCommand(get_info());
00201 connectedDevice.setCommandListener(this);
00202 connectedDevice.setTicker(get_tickerConnectedDevice());
00203 connectedDevice.setSelectedFlags(new boolean[0]);
00204 connectedDevice.setSelectCommand(get_Disconnect());
00205 }
00206 return connectedDevice;
00207 }
00208
00213 private javax.microedition.lcdui.Command get_Disconnect() {
00214 if (Disconnect == null) {
00215 Disconnect = new javax.microedition.lcdui.Command("Disconnect", javax.microedition.lcdui.Command.ITEM, 1);
00216 }
00217 return Disconnect;
00218 }
00219
00224 private javax.microedition.lcdui.Command get_info() {
00225 if (info == null) {
00226 info = new javax.microedition.lcdui.Command("Info", javax.microedition.lcdui.Command.SCREEN, 1);
00227 }
00228 return info;
00229 }
00230
00235 private javax.microedition.lcdui.Alert get_deviceInfo() {
00236 if (deviceInfo == null) {
00237 deviceInfo = new javax.microedition.lcdui.Alert(null, "<Enter Text>", null, javax.microedition.lcdui.AlertType.INFO);
00238 deviceInfo.setTimeout(-2);
00239 }
00240 return deviceInfo;
00241 }
00242
00247 private javax.microedition.lcdui.Ticker get_tickerConnectedDevice() {
00248 if (tickerConnectedDevice == null) {
00249 tickerConnectedDevice = new javax.microedition.lcdui.Ticker("Al momento ci sono 0 connessioni attive");
00250 }
00251 return tickerConnectedDevice;
00252 }
00253
00254 javax.microedition.lcdui.Alert initAlert;
00255 javax.microedition.lcdui.Command exitCommand1;
00256 javax.microedition.lcdui.Ticker ticker1;
00257 javax.microedition.lcdui.Alert alertSplashScreen;
00258 javax.microedition.lcdui.Image logo;
00259 javax.microedition.lcdui.List connectedDevice;
00260 javax.microedition.lcdui.Command Disconnect;
00261 javax.microedition.lcdui.Command info;
00262 javax.microedition.lcdui.Alert deviceInfo;
00263 javax.microedition.lcdui.Ticker tickerConnectedDevice;
00264
00265
00266 public void startApp() {
00267 initialize();
00268 }
00269
00270 public void pauseApp() {
00271 }
00272
00273 public void destroyApp(boolean unconditional) {
00274
00275 }
00276
00277
00278 public void connectionAdded(StreamConnection conn){
00282 String nameOrAddress;
00283 RemoteDevice rd = null;
00284 ConnectionProcessor cp = null;
00285
00286 try{
00287 rd = RemoteDevice.getRemoteDevice(conn);
00288 } catch (IOException ex){
00289
00290
00291
00292
00293
00294
00295 btServer.removeConnection(conn);
00296 return;
00297 }
00298
00299 try{
00300 nameOrAddress = rd.getFriendlyName(true);
00301 } catch(IOException e){
00302 nameOrAddress = rd.getBluetoothAddress();
00303 }
00304
00305
00306
00307
00308 get_connectedDevice().append(nameOrAddress, null);
00309
00310
00311
00312
00313 connected.addElement(rd);
00314
00315
00316
00317
00318 connections.addElement(conn);
00319
00320
00321
00322
00323 cp = new ConnectionProcessor(conn, this.btServer, this.serviceURL);
00324
00325
00326
00327
00328 connectionProcessors.addElement(cp);
00329
00330 get_tickerConnectedDevice().setString("Al momento ci sono " + connections.size() + "connessioni attive");
00331 }
00332
00333
00334 public void connectionRemoved(StreamConnection conn){
00335
00336
00337 int index = connections.indexOf(conn);
00338
00339
00340
00341
00342
00343
00344 if (index != -1){
00345
00346
00347 connections.removeElementAt(index);
00348
00349
00350
00351
00352 connectionProcessors.removeElementAt(index);
00353
00354
00355
00356
00357
00358 connected.removeElementAt(index);
00359 }
00360
00361 get_tickerConnectedDevice().setString("Al momento ci sono " + connections.size() + "connessioni attive");
00362 }
00363
00364 }