Source Code for SPPSimpleServerUI.java

001 package net.benhui.btgallery.spp;
002 
003 import javax.microedition.lcdui.*;
004 import javax.bluetooth.*;
005 import javax.microedition.io.*;
006 import java.io.*;
007 import net.benhui.btgallery.*;
008 
009 /**
010  *
011  <p>Title: Example Serial Port Profile Server.</p>
012  <p>Description: This example server only one SPP server connection
013  * and wait for a client to connect. Once a client connection accept, it read
014  * one string off the connection stream and display this string on the list GUI
015  * and wait for the next client connection again.</p>
016  <p>Description: Important area: run() </p>
017  @author Ben Hui (www.benhui.net)
018  @version 1.0
019  *
020  * LICENSE:
021  * This code is licensed under GPL. (See http://www.gnu.org/copyleft/gpl.html)
022  */
023 public class SPPSimpleServerUI extends List implements Runnable
024 {
025   // Bluetooth singleton object
026   LocalDevice device;
027   DiscoveryAgent agent;
028 
029   // control flag for run loop
030   // set true to exit loop
031   public boolean done = false;
032 
033   // our BT server connection
034   public StreamConnectionNotifier server;
035 
036   public SPPSimpleServerUI()
037   {
038     super("SPP Server", List.IMPLICIT );
039 
040     addCommandnew Command("Exit", Command.SCREEN, ) );
041 
042     setCommandListenerSPPServerMain.instance );
043   }
044 
045   /**
046    * An utility function that show a alert box that display an exception message.
047    @param e
048    @param next_screen
049    */
050   public void showExceptionAlertException e, Screen next_screen )
051   {
052     Alert alert = new Alert"Problem""Exception: "+e.getClass().getName()+" "+e.getMessage(), null, AlertType.ERROR );
053     alert.setTimeoutAlert.FOREVER );
054     SPPServerMain.display.setCurrentalert, next_screen );
055   }
056 
057   public void run_server()
058   {
059     try
060     {
061       //
062       // initialize the JABWT stack
063       device = LocalDevice.getLocalDevice()// obtain reference to singleton
064       device.setDiscoverable(DiscoveryAgent.GIAC)// set Discover mode to LIAC
065 
066       // start a thread to serve the server connection.
067       // for simplicity of this demo, we only start one server thread
068       // see run() for the task of this thread
069       Thread t = new Threadthis );
070       t.start();
071 
072     catch BluetoothStateException e )
073     {
074       e.printStackTrace();
075     }
076 
077   }
078 
079   public void run()
080   {
081     // human friendly name of this service
082     String appName = "SSPServerExample";
083 
084     // unique UUID for this service. this can be defined by developers
085     UUID uuid = new UUID(0xABCD);
086 
087     // this is similar to 'server socket'
088     server = null;
089 
090     // connection to remote device. similar to 'client socket'
091     StreamConnection c = null;
092     try
093     {
094       // Create a server connection object, using a
095       // Serial Port Profile URL syntax and our specific UUID (0xABCD)
096       // and set the service name to appName
097       server =  (StreamConnectionNotifier)Connector.open(
098           "btspp://localhost:" + uuid.toString() +";name="+appName);
099 
100       // Retrieve the service record template
101       ServiceRecord rec = device.getRecordserver );
102 
103       // populate BluetoothProfileDescriptionList using SerialPort version 1
104       DataElement e1 = new DataElementDataElement.DATSEQ );
105       DataElement e2 = new DataElementDataElement.DATSEQ );
106       e2.addElementnew DataElementDataElement.UUID, new UUID(0x1101) ))// add SerialPort
107       e2.addElementnew DataElementDataElement.INT_8, 1) )// add Version 1
108       e1.addElemente2 );
109       rec.setAttributeValue0x0009, e1 )// add BluetoothProfileDescriptionList
110 
111       // Print the service record, which already contains
112       // some default values
113       Util.printServiceRecordrec );
114 
115 
116 
117     catch (Exception e)
118     {
119       e.printStackTrace();
120       showExceptionAlerte, this );
121       return;
122 
123     }
124 
125     while!done)
126     {
127       try {
128         ///////////////////////////////
129         log("local service waiting for client connection");
130 
131         append("[Waiting for Msg]"null );
132 
133         //
134         // start accepting client connection.
135         // This method will block until a client
136         // connected
137         c = server.acceptAndOpen();
138 
139         //
140         // retrieve the remote device object
141         RemoteDevice rdev = RemoteDevice.getRemoteDevice);
142 
143         // obtain an input stream to the remote service
144         DataInputStream in = c.openDataInputStream();
145 
146         // read in a string from the string
147         String s = in.readUTF();
148         log("read in data '"+s+"'");
149 
150         // display this string on GUI
151         appends, null );
152 
153         // close current connection, wait for the next one
154         c.close();
155 
156 
157       catch (Exception e)
158       {
159         e.printStackTrace();
160         showExceptionAlert(e, this );
161         return;
162 
163       }
164 
165     }
166 
167   }
168 
169   public void logString s )
170   {
171     System.out.println);
172   }
173 
174 }
Java2html
Benhui.net news
Feb 14, 2004 - New downloads from our Bluetooth section. BlueChat example application with full source code. Learn more! Bluetooth more! [more]
Feb 14, 2004 - Found out what SonyEricsson P900 has to offer for J2ME developers. Review our P900 developer review. [more]
Feb 14, 2004 - Check out Ben's latest Java Bluetooth development article at JDJ Feb issue. Get it off the shelf while it last. [more]
Dec 15, 2003 - Our Bluetooth section open! Check out our Bluetooth Browser, Code gallery, and tons of links. [more]
Nov 08, 2003 - Join us to chat with Michael Yuan. Learn more about Enterprise J2ME. [more]
Nov 08, 2003 - 35 more links added to our popular J2ME Master Links [more]
Nov 08, 2003 - MIDP 2.0 phone list is updated to include the latest and greatest phones. And with links, too! Check it out. [more]
Sept 21, 2003 - Add MIDP 2.0 phone list section. [more]
Aug 28, 2003 - Spy your mobile device even more with our Mobile Speed tool. [more]
Aug 22, 2003 - Spy your mobile device with our new Mobile Echo tool. [more]
Aug 22, 2003 - A new forum section is added to our links database. Questions? Dunno where to start? Start from here!
Aug 22, 2003 - Another 20 links added to our database. You have links to suggest? email us!

Aug 01, 2003 - the new benhui.net goes live! Fully updated with lots of J2ME links, UML diagrams, and special features.

Aug 01, 2003 - check out our latest featured phone - Nokia 3650 [more]

  

Featured Phone

 

Featured People

  

Featured Site

 

 

 

Copyright (c) 2003. BenHui.Net. All rights reserved.

All trademarks are owned by their respective companies.

Feedback to webmaster | Questions? | Privacy | Terms of Use