Using createBond () low-power Bluetooth

I am creating an application that communicates via Bluetooth Low Energy with a device, mainly to display data from the device on the phone. I need to implement a functionality where, if a device and a phone move to a range, they are automatically identified as a pair and will connect without user interaction. I looked at Bonding to accomplish this.

I am currently trying to take a device that is currently connected and create a connection with it when the user clicks the ListItem button. I call BluetoothDevice.createBond() reference to the connected BluetoothDevice variable. The Android developer says that for BluetoothDevice.createBond() false will be returned if there is an error, and that I have to register for ACTION_BOND_STATE_CHANGED to be notified when Bonding is up and running.

I have a few problems with createBond() where my questions are:

  • In most cases, createBond() returns false, indicating that it could not start the connection. What are the possible reasons for this? (My phone is running Android 4.4.2, API19, where createBond() is located, so this is not the case.)
  • Sometimes the binding seems to succeed because the code inside the receiver ACTION_BOND_STATE_CHANGED will be called. I can misunderstand what is connected with maintaining and using communication, but the BLE device does not automatically connect to the phone when it is in range, so I probably miss the code. Once connected, what else do I need to do?
  • In case createBond() not suitable for any reason (for example, I have to make the application work for API18 when createBond() introduced in API19), what alternatives exist?

Examples would be incredibly helpful. Finding information about contacting BLE was disappointing. Self-study of this material was difficult, especially without textbooks. In addition, some people say that this is not possible because it is โ€œJust Worksโ€, while others say that it is possible. BLE is still a big area of โ€‹โ€‹confusion for me, so for now, clarity with createBond() will be greatly appreciated.

+5
source share

All Articles