• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

how to connect to a Bluetooth device on App launch?

nissay

Newbie
May 21, 2018
10
2
Hello everyone,

I would like to create an app that connects to a MIDI device via Bluetooth connection.

I already did it thanks to the MidiBtlePairing. But in this example, we have two activities, one for starting the scan and the other one to select the device that is around.

But I would like the connection to be done automatically, without having to start the scan or to select the device. I don't want to perform any action, I just have to launch the app and the device is paired. My device has a specified name and is the only one around, so I would use ScanFilter in my code.

I'm kind of lost because all the examples that i find on the Internet have 2 activities and make the user perform actions. I'm trying to arrange pieces of the bluetooth and bluetooth.le classes in order to make it work but without success for now :(.

Let me know if you have an idea of how to do it, thank you for your help!
 
You should be able to take all the code related to device scanning, from the DeviceScanActivity, and relocate it in the MainActivity class. The device scanning code can be called from onCreate(), which is the entry point for any Activity.
This does however mean you need to look at the code and understand how it works.
 
  • Like
Reactions: nissay
Upvote 0
Taking all the code from DeviceScanActivity is not really efficient because all I need are the scanning and the pairing functions. The DeviceScanActivity shows a list of devices available.

Puttin the scanning code in onCreate() is already a part of the plan, it's the fisrt thing I thought about when I started coding.

And yes, I will spend more and more time reading code and understanding how the program behaves.

Thank you for your answer
 
Upvote 0
Taking all the code from DeviceScanActivity is not really efficient because all I need are the scanning and the pairing functions. The DeviceScanActivity shows a list of devices available.

Yes, you will of course only need to transplant certain bits of code pertaining to device scanning. That's why I said, "take all the code related to device scanning", not ALL the code from the DeviceScanActivity ;)
 
Upvote 0
Giving some news.
Hurray, I succeeded. I would never work with Bluetooth on Android again, it's such a horrible feature to work with, I clearly didn't have fun while programming (whereas I always do). I coded in a very dirty way, I used a lot of hacks in order to avoid getting errors and working inside a non-activity class doesn't make it easier.

Next challenge : receive Midi messages. I have to parse Midi raw byte arrays and I won't have more fun than with Bluetooth.

Wish me luck :(
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones