【问题标题】:How can I run bluetooth connection in the background in android?如何在android的后台运行蓝牙连接?
【发布时间】:2016-08-07 22:03:36
【问题描述】:

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

现在,如果用户单击“测量”按钮,则会出现下一个页面,其中包含图表以及“开始”和“停止”按钮,并且蓝牙将被启用。 在这里,我想在用户出现的进度条下建立一个连接。

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

【问题讨论】:

    标签: java android bluetooth


    【解决方案1】:

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

    创建一个新类并将其命名为示例: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));
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-18
      • 1970-01-01
      • 2017-08-25
      • 2016-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多