Bluetooth BLE comms w/ CodenameOne

124 views
Skip to first unread message

timgalla...@gmail.com

unread,
Mar 6, 2017, 11:17:21 AM3/6/17
to CodenameOne Discussions
If you are experiencing an issue please mention the full platform your issue applies to:
IDE: NetBeans
Desktop OS: Mac
Simulator: none, due to Bluetooth usage which is not available on Simulator
Device: Android phone
Bluetooth device: pedometer

It seems BLE support is unreliable. I turn on my Bluetooth device that I'm trying to connect to (a pedometer), then on the phone I start scanning for the device, and sometimes it picks up, sometimes not. If it does pick up, I try to connect using the address, and I get a "Could not connect to device". (incidentally, if I use isConnected(), it throws an exception saying it was never connected.  I assumed it would just return false)
I'm trying to duplicate a native iOS app, which has no issue connecting to the pedometer every time.

I've been searching for a "best practices" on BLE comms, but can not find anything substantial.  The link the Cordova docs are cumbersome due to requiring translation into the Codename One lib.

Steve Hannah

unread,
Mar 6, 2017, 2:19:30 PM3/6/17
to codenameone...@googlegroups.com
Can you provide more information about the "It was never connected" error?  Did this error appear in a popup dialog?  Or did you read this in DDMS?  Did you get a full stack trace?  
Also, can you share any code snippets that I can try out here?

It has been a long time since I've looked at the Bluetooth module, but if you provide me with some more info, I can take a look.

Steve

--
You received this message because you are subscribed to the Google Groups "CodenameOne Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to codenameone-discussions+unsub...@googlegroups.com.
Visit this group at https://groups.google.com/group/codenameone-discussions.
To view this discussion on the web visit https://groups.google.com/d/msgid/codenameone-discussions/f8c69b92-2ef5-44ca-b593-170f89fede80%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Steve Hannah
Software Developer
Codename One

timgalla...@gmail.com

unread,
Mar 6, 2017, 8:25:12 PM3/6/17
to CodenameOne Discussions
To answer question about the error, if I call BlueTooth.isConnected() the error is: "Device was never connected"

For the rest of the issues...
I've been doing more experimenting and I'm finding that the methods for bluetooth are non-blocking, which I did not expect.

For example, I have a button on a page with an event method that calls 'connect()', a wrapper to BlueTooth.connect() with a common callback, that constructs an instance of ShoeSense for thevariable "shoeSense" (a pedometer).  In the code, if the call to connect() works, the callback sets the "shoeSense" variable before the button Event method checks for the variable being null.   There are ToastBar calls to show the order.

When this process is actually run, the button push calls the event method, calls connect(), connect calls the BlueTooth.connect(), shoeSense variable is checked for null in the event method which displays the ToastBar, then a moment later the connect callback is called and the ToastBar displays the "shoeSense initialized".  The only conclusion is that the BlueTooth methods are put on their own thread. 


    public void onButtonConnectActionEvent(com.codename1.ui.events.ActionEvent ev) {
        try {
            connect();
            if (this.shoeSense == null) {
                ToastBar.showErrorMessage("Connection issue: ShoeSense not initialized.");               
            }
        } catch (BleConnectionException ex) {
            ToastBar.showErrorMessage("Connection issue: " + ex.getMessage());
        }
    }

    private void connect()  throws BleConnectionException  {
        try {
            blueToothConnector.connect(connectionListener, scanResult.getAddress());
        } catch (IOException ex) {
            throw new BleConnectionException("Connection issue: " + ex.getMessage(), ex);
        }
    }

    ActionListener connectionListener = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent evt) {
            this.shoeSense = new ShoeSense();
            ToastBar.showErrorMessage("Shoe Sense initialized.");
        }
    };

Shai Almog

unread,
Mar 7, 2017, 1:27:18 AM3/7/17
to CodenameOne Discussions, timgalla...@gmail.com
That's correct, the API is asynchronous. I explained more here: http://stackoverflow.com/questions/42631301/bluetooth-ble-comms-w-codenameone/42641129#42641129

Chris Hales

unread,
Mar 10, 2017, 11:46:17 AM3/10/17
to CodenameOne Discussions, timgalla...@gmail.com
Any chance you would be willing to share your code for getting the name of your pedometer and then what what you are doing to connect. I can not seem to find documentation on how to do this. 

Shai Almog

unread,
Mar 11, 2017, 1:02:04 AM3/11/17
to CodenameOne Discussions, timgalla...@gmail.com
The bluetooth demo isn't quite ideal. Since we based our work on a Cordova plugin you might be able to find sample usages of that plugin and adapt them to Codename One.
Reply all
Reply to author
Forward
0 new messages