1

When my app or service is running, I can offer a Bluetooth profile (e.g. SPP, DUN, or my own UUID) using BluetoothAdapter.listenUsingRfcommWithServiceRecord(...).

Is it possible to instead have this Bluetooth profile visible on the device all the time, even when my app or service is not running, and have Android launch my app or service automatically when someone connects to this Bluetooth profile?

I could envision something like this inside AndroidManifest.xml:

<receiver android:name="com.example.MyBluetoothReceiver" android:exported="true">
    <intent-filter>
        <action android:name="android.bluetooth.adapter.action.RFCOMM_ACCEPT" />
        <data android:name="My SPP Service"
              android:uuid="00001101-0000-1000-8000-00805F9B34FB" />
    </intent-filter>
</receiver>

The goal would be for the Android framework to recognize this receiver purely based on AndroidManifest.xml, automatically offer the Bluetooth profile as specified in the <data ...>-tag whenever the Bluetooth radio is turned on, and then instantiate and call my intent receiver whenever an actual connection is made. This would be similar to the way custom URL handlers work by registering an ACTION_VIEW intent with CATEGORY_BROWSABLE, which the browser then calls to launch my app, even if my app is not currently running.

The closest I could find so far are notifications for general Bluetooth status changes (radio on/off, device paired), e.g. here: Android Broadcast Receiver bluetooth events catching, or the BluetoothAdapter's ACTION_CONNECTION_STATE_CHANGED intent, which is called for connection to an existing remote profile, rather than a newly offered local one.

Community
  • 1
  • 1
Markus A.
  • 12,349
  • 8
  • 52
  • 116

1 Answers1

0

Ya its possible in your onReceive() need to add below code to launch the app.

PackageManager pm = context.getPackageManager();
Intent launchIntent = pm.getLaunchIntentForPackage("com.example.blutoothapp");
launchIntent.putExtra("some_data", "value");
context.startActivity(launchIntent);

Hope this will help.

Maheshwar Ligade
  • 6,709
  • 4
  • 42
  • 59
  • Thanks for your answer, but I'm a little confused by it. It looks like this is just code that allows one to launch another. I guess I wasn't being clear in my question. I am trying to find out if it is possible to have the Android framework itself open the Bluetooth service record for me (without any of my apps running) and then launch my app automatically if someone connects to that Bluetooth service. Just like what happens with AOA, where the Android framework detects the accessory connection and then launches the correct app (or even prompts it to be installed if it's not). – Markus A. May 06 '16 at 15:22
  • @Markus i cleared your question but there you have to follow the same way register the receiver when receiver fired launch the app, there is no special service for your requirement I don't know – Maheshwar Ligade May 06 '16 at 15:26
  • So, you're saying, I will definitely need to have some sort of service or app running to be able to register a Bluetooth profile, and there is no way to have the Android framework do it for me while my app or service are not running? – Markus A. May 06 '16 at 20:23
  • ya I think, so just register a receiver in manifest which receives the blutoothapp single & when it receives launch the app – Maheshwar Ligade May 07 '16 at 02:14