×

Discussion Board

Results 1 to 7 of 7
  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    9

    Bluetooth client problem on games!

    Hi guys, do you all start your client thread after the device and service discovery done or vice versa?
    i suspect something with my codes which cause the client doesn't work properly. my player has set of sequence when the game started. it works fine for the server. However when client start its thread after device and service discovery the sequence is not running at all.

    here is some of mycodes..please help..thanks.

    main midlet:
    Public class main_midlet extends MIDLet implements CommandListener{
    ......

    //when choose on client, client class initiliaze and both client ad game thread starts
    if (Command =="client") {
    client=new BlueClient();
    games = new Game(client); // pass client value to let gamecanvas knows it is client
    }else if (Command=="server'){
    server=new BlueServer();
    games = new Game(server); // pass client value to let gamecanvas knows it is server

    }

    gamecanvas:

    public class Game implements Runnable{

    ......


    private BlueServer server;

    public Gmae(BlueServer server){
    .....
    this.server=server;
    }

    private BlueClient client;

    public Game(BlueClient client){
    .....
    this.client=client;
    }

    public void run(){
    // start game thread and update game screen/action including player's action
    ........
    .......

    }

    Bluetooth client:

    public class BlueClient implements DiscoveryListener, Runnable {
    ....

    //constructor and start discovery
    public BlueClient()
    {
    local = LocalDevice.getLocalDevice();
    local.setDiscoverable( DiscoveryAgent.GIAC);
    agent = local.getDiscoveryAgent();
    agent.startInquiry(DiscoveryAgent.GIAC, this);
    }

    //and it starts here

    public void deviceDiscovered( RemoteDevice btDevice, DeviceClass cod){
    .......

    }


    public void inquiryCompleted(int dicType ){
    ..........

    }

    public void servicesDiscovered( int transID, ServiceRecord[] servRecord){
    ...........
    }

    //search done at last...
    public void serviceSearchCompleted( int transID, int respCode ){

    //and thread starts here

    Thread clientThread = new Thread( this );
    clientThread.start();

    }

    //thread goes here
    public void run(){

    try {
    conn = (L2CAPConnection) Connector.open(serviceUrl);
    }catch (Exception ex) { System.out.println("Not Connected" );}

    }




    Blueserver:

    Public class BlueServer implements Runnable{


    public class Blueserver(){

    try{
    local = LocalDevice.getLocalDevice();
    local.setDiscoverable(DiscoveryAgent.GIAC);
    agent = local.getDiscoveryAgent();
    }catch ( BluetoothStateException e ){e.printStackTrace();}


    url = "btl2cap://localhost:" + service_UUID + ";name=" + serviceName;

    Thread Serverthread = new Thread( this );
    Serverthread.start();
    )


    public void run(){

    try{

    server = (L2CAPConnectionNotifier)Connector.open( url );
    conn = server.acceptAndOpen();

    }catch ( Exception e ){e.printStackTrace();}
    }


    i'd tried put the whole device/service discovery process into the thread, the data
    can pass around and it reflected within the client. However because of the client thread it
    affected the game thread and it wont run the player game sequence as i mentioned earlier on.
    The server works fine when i triggered it at the first place. Player's sequence works properly
    without any error...

    Another question i need to ask is,does client thread only will run when there is any data coming in..?

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    3

    Re: Bluetooth client problem on games!

    Hi wmchan78,

    Usually a thread the device discovery and service search is done and after a connection is obtained, two threads are spawned which reads and writes to the stream.

    You can have ther client thread implementation in two ways.. one by blocking and other by checking availability

    ie you can use either one of the following

    blocking read like
    while ((i=is.read())!=-1) {...

    nonblocking read block of data like
    if ((l=is.available())>0) is.read();


    I suggest you call

    discoveryAgent.startInquiry() and discoveryAgent.searchServices() in a thread

    After calling discoveryAgent.startInquiry() put an infinite loop in which the inquiry status is being checked and devices found are added to a vector. When (inquiryStatus == DiscoveryListener.INQUIRY_COMPLETED) quit the loop and proceed to the searchServices

    Try moving the agent.startInquiry(DiscoveryAgent.GIAC, this); in the constructor of the BlueToothClient to the thread.

    After that do what i have mentioned above...

    Hope this helps

    Madnok

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    9

    Re: Bluetooth client problem on games!

    hi madnokk,

    i'd tried to put everything in the client thread. but still, the client game canvas only display its update when ever there is incoming data from server...

    dont really understand on your blocking part...but do you have any url related to this topic..?
    Last edited by wmchan78; 2006-03-18 at 01:04.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    9

    Re: Bluetooth client problem on games!

    hi,....
    can anyone tell me any guide please....

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    9

    Re: Bluetooth client problem on games!

    I just found out that JSR82 L2cap has one very tricky stuff. Unlike RFCOMM, it will stop the thread (in my case, my game thread) during reading/receiving process.
    Hence, i'd use the L2CAPConnection.ready() to overcome my problem.
    Before this, my game thread stops as :

    try{
    // String clientdata = client.readData(); -->> here stops the game thread!!!!!!
    if (clientdata!=null){
    if (clientdata.equals("up")){
    player.moveUp();
    }
    ............................//follow by other instruction
    }
    }catch(Exception ex ){}

    And now, i've put L2CAPConnection.ready() to verify if there is any incoming data ready to be received.
    if yes, then:

    try{
    //if there is any data coming in from server
    if(client.conn.ready()){
    //read player one instruction
    try{
    String clientdata = client.readData();
    .......................//follow by other instruction...
    }catch(Exception ex ){}

  6. #6
    Registered User
    Join Date
    Feb 2006
    Posts
    3

    Re: Bluetooth client problem on games!

    Hi wmchan78,

    Did u get over the getConnectionURL part where you encountered problem?

    I guess you have....

    Madnok

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    9

    Re: Bluetooth client problem on games!

    yup madnokk....got it solved by getting the service record assigned in one variable and use it during getConnectionURL ....
    thanks ya.. :-)

Similar Threads

  1. Bluetooth problem
    By dentis_1980 in forum Bluetooth Technology
    Replies: 6
    Last Post: 2007-07-26, 14:02
  2. Bluetooth Socket Connection Problem
    By deniz6682 in forum Mobile Java Networking & Messaging & Security
    Replies: 7
    Last Post: 2006-03-08, 08:07
  3. Bluetooth problem in Symbian C++ code
    By vardhman in forum Bluetooth Technology
    Replies: 0
    Last Post: 2005-01-02, 08:17
  4. 6230 global problems with Bluetooth
    By asterixmlm in forum General Development Questions
    Replies: 1
    Last Post: 2004-12-10, 17:12
  5. Replies: 5
    Last Post: 2003-02-07, 11:25

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×