如何在后台保持蓝牙连接? [英] How to keep bluetooth connection in background?

查看:1268
本文介绍了如何在后台保持蓝牙连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个与蓝牙模块通信的蓝牙应用程序.实际上,它工作得很好.但是我希望在该应用程序处于后台并且使用其他应用程序时也保持连接的建立,以便其他活动(例如传入的短信或其他活动)可以触发我的应用程序在后台向我的设备发送消息.

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.

到目前为止,这是我的代码: http://pastebin.com/C7Uynuan

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

侧面信息:有一个连接按钮,用于建立连接,然后还有3个其他按钮向我的设备发送不同的消息. 在OnResume中,我重新连接到我的设备,但是在建立稳定连接后,则不必这样做.

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.

谢谢

progNewfag

progNewfag

现在我很确定我需要使用IntentService,但不确定如何使用.

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

推荐答案

您必须先学习该服务

以下是服务示例

创建一个新的类并将其命名为Exmaple:MyService

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));

要停止服务,请将此代码放入MainActivity

For Stopping the service put this code in MainActivity

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

查看此帖子

活动与服务之间的连接

也请参见此链接

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

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

示例:活动和服务之间通过消息传递进行通信

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

这篇关于如何在后台保持蓝牙连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆