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

现在,如果用户单击“测量”按钮,将显示下一页,其中包含图表和“开始”和“停止”按钮,并且将启用蓝牙.

在这里,我想在为用户显示的进度条下建立连接.

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

解决方法:

不要先走远你必须学习这项服务

这是服务示例

创建一个新类并为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();

}

}

要开始活动,只需在主要活动中写下此行

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

停止写这个

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

标签:java,android,bluetooth

来源: https://codeday.me/bug/20190702/1353305.html

更多推荐

java 安卓 蓝牙_java – 如何在android的后台运行蓝牙连接?