【问题标题】:How to Listen from bluetooth socket in android background service?如何在android后台服务中监听蓝牙套接字?
【发布时间】:2015-08-24 16:02:35
【问题描述】:

所以我尝试在 Android 应用程序中提供服务,这是我在 android 上的第一个应用程序(也是在 Java 上),所以我有很多问题。但主要问题是“架构”我的后台服务。

我需要通过蓝牙设置与设备(我的自定义设备)的连接。我有自己的协议(仍在进行中),因此我的后台服务需要以下内容:

  1. 发送了一些东西
  2. 始终收听 InputStream! (一直)

如何在我的服务中创建这个? 我需要在我的服务中创建两个线程?

我已经尝试了很多关于如何做的“想法”?而且我有非常大的“脏”代码……每次运行它都会崩溃。

public class MyService extends Service {

public BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
public BluetoothSocket socket;
public String readMessage;
public int BTC_STATE;
ExecutorService es;


public void onCreate() {
    super.onCreate();
    es = Executors.newFixedThreadPool(2);
}


}

 public void onDestroy() {
    super.onDestroy();

}
// ЗАПУСК СЕРВИСА
public int onStartCommand(Intent intent, int flags, int startId) {
    int time = intent.getIntExtra(DashboardActivity.PARAM_TIME, 1);
    int task = intent.getIntExtra(DashboardActivity.PARAM_TASK, 0);
    String strMAC = intent.getStringExtra(DashboardActivity.PARAM_MAC);
    String strMESS = intent.getStringExtra(DashboardActivity.PARAM_MESSAGE);
    MyRun mr = new MyRun(startId, time, task,strMAC,strMESS);
    es.execute(mr);

    return super.onStartCommand(intent, flags, startId);
}

public IBinder onBind(Intent arg0) {
    return null;
}

class MyRun implements Runnable {

    int time;
    int startId;
    int task;
    int target;
    String strMAC;
    String strMESS;

    public MyRun(int startId, int time, int task,String strMAC,String strMESS) {
        this.time = time;
        this.startId = startId;
        this.task = task;
        this.target = 1;
        this.strMAC = strMAC;
        this.strMESS = strMESS;

    }

    public void run() {
        Intent intent = new Intent(DashboardActivity.BROADCAST_ACTION);
//here, depending on task id i sent reqwest. But where i need to create listning method?


}

在 run() 中我有不同的任务...但是我需要在哪里为我的 BTE 套接字 InPutStream 创建列表方法?

【问题讨论】:

    标签: java android sockets bluetooth


    【解决方案1】:

    所以不,我有 3 项服务任务: 1)连接 2)开始收听我的 BTE 连接的 inputStream 3)发送数据

     try {
                            InputStream is = socket.getInputStream();
    
    
                            byte[] buffer = new byte[1];
                            int bytes;
                            //StringBuilder readMessage = new StringBuilder();
                            String readMess2;
                            while (1!=2) {
                                try {
    
                                    bytes = is.read(buffer);
                                    String readed = new String(buffer, 0, bytes);
    
                                    //readMessage.append(readed);
                                    readMess2=readed;
    
                                    intent.putExtra(DashboardActivity.PARAM_STATUS, DashboardActivity.STATUS_FINISH);
                                    intent.putExtra(DashboardActivity.PARAM_RESULT, "Listning: " + readMess2);
                                    sendBroadcast(intent);
    
                                } catch (IOException e) {
    
                                    BTC_STATE='0';
                                    intent.putExtra(DashboardActivity.PARAM_STATUS, DashboardActivity.STATUS_FINISH);
                                    intent.putExtra(DashboardActivity.PARAM_RESULT, "Ошибка уха!");
                                    sendBroadcast(intent);
                                    serviceNotifer("ADPTTO Service", "УХО ОШИБКА " + BTC_STATE);
                                    break;
                                }
                            }
    
    
                            intent.putExtra(DashboardActivity.PARAM_STATUS, DashboardActivity.STATUS_FINISH);
                            intent.putExtra(DashboardActivity.PARAM_RESULT, "STOP УХО WHILE =)");
                            sendBroadcast(intent);
                            serviceNotifer("ADPTTO Service", "УХО WHILE OUT " + BTC_STATE);
    
    
                        } catch (IOException e) {
                            BTC_STATE='0';
                            intent.putExtra(DashboardActivity.PARAM_STATUS, DashboardActivity.STATUS_FINISH);
                            intent.putExtra(DashboardActivity.PARAM_RESULT, "GET ERROR");
                            sendBroadcast(intent);
                            serviceNotifer("ADPTTO Service", "УХО ОШИБКА 2 + " + BTC_STATE);
                        }
    

    此代码有效.. 但只是一段时间(1-2 分钟)。我可以一直听谁的?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-07-26
      • 2022-11-03
      • 2012-08-08
      • 2015-08-03
      • 2014-03-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多