Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Bluetooth Connection with J2ME

807588Mar 19 2009
I'm trying to implement a connection between two phones that would allow them to play a game over Bluetooth. Each phone will be running the same connection code, and what I have done below is try and set up a "server" and a "client". The phone tries to connect itself as a client first, and if that fails, it tries to set itself up as the server. My question is, if the try is trying to find someone, and the catch has itself setup to be available to find, what am I missing in the code below? They both try to set themselves up as the server. Or more importantly, could someone please explain the process. This code is just me trying to throw some stuff together and make it work.

//Global variables
private DataOutputStream os = null;
private DataInputStream is = null;
private SocketConnection client = null;
private ServerSocketConnection server = null;
private StreamConnection conn = null;
    
private static final UUID game_ID = new UUID("1234", false);

try { //is the client and trying to connect to an available server

            DiscoveryAgent mDiscoveryAgent = LocalDevice.getLocalDevice().getDiscoveryAgent();
            String url = mDiscoveryAgent.selectService(game_ID,
                                   ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);

            conn = (StreamConnection) Connector.open(url);
            is = new DataInputStream(conn.openInputStream());


            os = client.openDataOutputStream();
            is = client.openDataInputStream();

            areYouServer = false; //declares that this implementation is the server
        } catch (Exception e) {
            //SERVER setup

            System.out.println("I want to be a server");
            try { //could not connect as the client, so it sets itself up as the server

                String url = "btspp://"+IP+":" + game_ID.toString() +";name=game";

                LocalDevice.getLocalDevice().setDiscoverable(DiscoveryAgent.GIAC);
                StreamConnectionNotifier service = (StreamConnectionNotifier) Connector.open(url);

                conn = (StreamConnection) service.acceptAndOpen();

                os = new DataOutputStream(conn.openOutputStream());

                os.flush(); // forces any buffered output bytes to be written out to the stream

                is = client.openDataInputStream();
                os = client.openDataOutputStream();

                areYouServer = true; //declares that this implementation is not the server (is the client)
            } catch (Exception f) {
                System.out.println("This is very bad");
                f.printStackTrace();
            //Do nothing (will hopefully never get here...)
            }
        } finally {
            Thread t = new Thread(this);
            t.start();
        }
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 16 2009
Added on Mar 19 2009
0 comments
205 views