Search code examples
javaandroidbluetoothconnectivity

How should I manage Bluetooth connections in Android?


Q. What are your best practices in managing bluetooth connectivity?

I've read the android bluetooth guide & many bluetooth connectivity tutorials. Not helpful with encapsulation-design nor best practices.

  • When should I open/close the connection?
  • Is the "connection" with a single bluetooth device called a "socket" connection?
  • Can a single connection send data while listening? (...or between listening states).

I've never coded connectivity with external devices before. It took two weeks for me to wrap my head around the code that scans for near-by bluetooth devices and throw them into a ListView. Listeners, Broadcasts, and Adapters!

My project will be printing 1-40 receipts every 15 minutes on a bluetooth receipt printer. At the moment, security is not an issue. On the same connection, it will also be receiving data (sending & receiving simultaneously does not appear to be necessary but would be useful). I'm not yet sure how the devices are configured on this single dongle device but I would guess the devices are connected via USB controller to the dongle.

So far, I have 1 object to manage a single I/O connection. Staticly I open an activity to select a connection (to later save the label, mac, and pin in the database). Based on tutorials, I have "open", "listen", "send", and "close" methods. What confuses me is "how" to use these functions. Can I leave a connection open all day (10hrs) and use it every 3mins? Should I open/close the connection when sending or requesting data? Where would I detect the need to reconnect?


Solution

  • sorry for the short answer, but from my practice with the Bluetooth API, I have found that this video describe the things very good (totally personal opinion...)

    Video 1

    In addition this is useful when you do NOT have any previous experience

    Tutorial

    And as last check out this question in stackoverflow it has a bunch of good references and examples!!

    Again sorry for the shortage, but I believe that if you check these out at least most of your questions and concerns will become answered!

    :)


    EDIT


    So, let me be a bit more descriptive and share some of my experience.

    I have written an App that communicates with BLE device that has 3 functions

    • double sided event driven button (push the button on phone -> event is fired to the device; push the button on the BLE device -> event is fired to the phone)

    • send request from phone -> BLE device answers with current battery percentage

    • continuously reading strength signal (as aprox. distance) between the phone and the BLE device


    So far so good, now the things is that the basic approach is:

    1. Search for BLE devices (bluetooth search or "discovery" of nearby bluetooth devices)

      • Here you will need android permissions!
    2. Choose the device you want to connect to

      • To differ the devices (maybe there are a lot around you :) ) you can use BLE device's name or UUID or ... best - use the name ;)
    3. After both devices connect to each other you can then start the Gatt communication. The approach with state machine is a little too much overkill for me. But anyway the communication is done through bytes (in my case...)

    4. In one of the videos/resources there was something specific and VERY HELPFUL at least for me! To be honest I don't remember it exactly, but the idea was that before any communication it's RECOMMENDED to read/get all the options from the BLE device or something similar...

      • Maybe it was something like discoverOptions() or something like that
    5. Great thing will be to know your device "communication codes" or at least I call them that way.

      • Check this link for example: Link ** Now you can see there are tables with the USEFUL INFO! E.g. if you want to read the battery level you navigate to this page and find that in order to read the battery, the service name is UUID XXXXX and you need to send 0x01 to the BLE device and it will "answer" to your call with some data which is again in bytes.

    I really hope that this is somehow helpful!

    PLEASE NOTE This is strictly coming from my experience and there could be some mismatches or wrong terms, but that's how I personally see the things and because my project was long ago, I don't remember most of the things exactly.