views:

50

answers:

4

Hello everyone,

I've been stuck since several days on my bluetooth application. Its role is to receive data from Bluetooth devices, using Serial Port Protocol ... The application runs in the background of the phone and devices, when they need to send an information, turns on the bluetooth and try to send a data frame.

The problem is that several devices can connect using this application. And I don't manage to put the phone as a server so it could receive continuous data. This means that I can create a Connector.open ("btspp: localhost :....") with AcceptAndOpen (), but this function starts only once and I can not relaunch endlessly.


The solution would be to launch a AcceptAndOpen () When a bluetooth call is over ... restart the function, but I can not (I simply recreates the object that made the port opening)


I try to use without success: - PushRegistry ("btspp" is not compatible) - Connector.open () with the mac address of clients (but this is not self-blocking)

If someone could help me on how to solve this issue. Because I have searched thoroughly on the blackberry forums...

Thank you, Fabrice

PS: If you need source code, I can give you ... But I'm not sure this can help you to answer me.

A: 

I've just put around "acceptAndOpen()":

while(true) {...}

Now it's ok :)

Fabrice
A: 

Make sure after you get a connection from acceptAndOpen, you pass that connection into a new thread. This allows the current "server" thread to go back and accept another connection (assuming it's in a loop).

Marc Novakowski
A: 

Fabrice I'm actually trying to develop a bluetooth app to measure proximity for academic purposes;

Can you guide me on how to make the phone to be on a bluetooth standby mode all the time - so he can discover and be discovered by other devices.

The sole purpose of this development is to record the mac address and name of the devices that are discoverables and the upload that information to a server.

I will appreciate very much if you can guide me through this,

Thanks

RA Student

RA Student
A: 

Hi RA Student,

Sorry for my late answer.

I've create a class: BluetoothReceiver that's she is launch in background (link text). There is a part of my sourcecode for try to help you:

public void start() {
    try {
    _connector = (StreamConnectionNotifier) Connector.open("btspp://localhost:" + MY_UUID, Connector.READ);
    Runnable r = new Runnable() {
        public void run() {
            while(true) {
                try {
                    StreamConnection connection = _connector.acceptAndOpen();
                    onConnectionOpen(connection);
                }
                catch (IOException e) {
                    // Connection failed
                    break;
                }
            }
        }
    };
    Thread t = new Thread(r);
    t.start();
}
catch (IOException e) {
    // e.getMessage()
}

private void onConnectionOpen(StreamConnection connection) {
    RemoteDevice device = null;
    try {
        device = RemoteDevice.getRemoteDevice(connection);
    } catch (IOException e) {
        // e.getMessage()
    }
    if(device != null) {
        // Make your own process: read, write, pair, ...
    }
}

Regards, Fabrice

Fabrice