如何在android中的后台运行蓝牙连接?

Bee*_*abt 5 java android bluetooth

我想编写一个应用程序,该程序通过蓝牙连接从微控制器获取数字作为输入,并在图中为用户表示它们。我的第一个问题是如何在后台与设备建立蓝牙连接(不中断用户)?

现在,如果用户单击“测量”按钮,则下一页将显示该图,并且“开始”和“停止”按钮将启用蓝牙。在这里,我想在用户出现的进度条下建立一个连接。

可能吗?在后台建立连接时,有人可以给我举个例子吗?

hat*_*rar 2

先别走太远 你必须学习服务 这是服务示例

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

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

    }
}
Run Code Online (Sandbox Code Playgroud)

要开始活动,只需在您的主要活动中写入此行

startService(new Intent(this, MyService.class));
Run Code Online (Sandbox Code Playgroud)

停止写这个

stopService(new Intent(this, MyService.class));
Run Code Online (Sandbox Code Playgroud)

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