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..?

Reply With Quote

