开发者

Blackberry Bluetooth Development Help

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 w开发者_JAVA技巧ithout 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.


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


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


I've just put around acceptAndOpen():

while(true) {
    ...
}

Now it's okay.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜