Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep bluetooth connection in background?

i am writing a bluetooth app communicating with a bluetooth module. Actually it works very well. But i want the connection to stay established also while the app is in background and other apps are used, so that another activity like incoming sms or something else can trigger my app in background to send messages to my device.

Until now i am very confused how to do this. Can anyone give me advice?

I also checked this: Background Bluetooth App - Threading? but it doesn't help me.

Here is my code so far: http://pastebin.com/C7Uynuan

Side information: there is a connect button, which establishs the connection and then there are 3 other buttons sending different messages to my device. In OnResume i reconnect to my device, but this should be not necessary when having a stable connection.

Thanks,

progNewfag

EDIT: Now i am pretty sure that i need to use an IntentService, but not sure how.

like image 886
progNewbie Avatar asked Aug 12 '14 15:08

progNewbie


People also ask

Why is my Bluetooth not staying connected?

For Android phones, go to Settings > System > Advanced> Reset Options > Reset Wi-fi, mobile & Bluetooth. For iOS and iPadOS device, you'll have to unpair all of your devices (go to Setting > Bluetooth, select the info icon and choose Forget This Device for each device) then restart your phone or tablet.

How do I stop my android from disconnecting from Bluetooth?

Why Does My Bluetooth Keep Disconnecting From My Android? Many apps are designed to block Bluetooth; however, clearing the cache can help solve the issue. To reset the apps for Android phones, go to Settings > System > Advanced > Reset Options > Reset Wi-fi, Bluetooth, mobile.

How do I make my Android phone automatically connect to Bluetooth?

Return to the Settings, search for Bluetooth auto-connect and enable it. Then swipe down from the status bar to open the notification panel and touch and hold the Bluetooth icon to enter the Bluetooth settings screen. Make sure the name of the car kit is in the list of Paired devices and tap on it to reconnect.


1 Answers

You Have to learn the service first

Here is the Example of Service

Create a new Class and Name it for Exmaple: MyService

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;

public class MyService extends Service {
    public MyService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        return Null;
    }

    @Override
    public void onCreate() {
        Toast.makeText(this, "The new Service was Created", Toast.LENGTH_LONG).show();

    }

    @Override
    public void onStart(Intent intent, int startId) {
        // For time consuming an long tasks you can launch a new thread here...
        // Do your Bluetooth Work Here
        Toast.makeText(this, " Service Started", Toast.LENGTH_LONG).show();

        }

        @Override
        public void onDestroy() {
            Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();

        }
    }

Now in your main activity you can start the service through this code

 startService(new Intent(this, MyService.class));

For Stopping the service put this code in MainActivity

stopService(new Intent(this, MyService.class));

See this Post

Connection between Activity and Service

Also See this link

http://www.javacodegeeks.com/2014/01/android-service-tutorial.html

http://examples.javacodegeeks.com/android/core/service/android-service-example/

EDIT:

Example: Communication between Activity and Service using Messaging

http://www.intertech.com/Blog/using-localbroadcastmanager-in-service-to-activity-communications/

like image 177
Jamil Avatar answered Oct 02 '22 03:10

Jamil