• 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
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.
 
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
 
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 ;)
 
Well anyway, I think you got the general idea. Feel free to come back here if you need further help.
 
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 :(
 
I would strive to make your code clean. You'll thank yourself for it in the future.
This stuff is quite tricky, but just think of the sense of achievement when you get it working.
 
I will clean everything up, I have to. I'm intern and at the end of it my professors will have a look at my report and my code. I won't have a good grade if the code is messed up and uncomfortable to read.

Have you ever worked with the android.media.midi package?
 
I already visited those pages, I never start a project without checking documentations first ;)
Everything is setup for the midi protocol, all I have to do now is to receive data and treat it. The data come as raw bytes
 
Back
Top Bottom