如何在android后台服务中从蓝牙套接字中收听?

时间:2015-08-24 16:02:35

标签: java android sockets bluetooth

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

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

  1. 发送了一些内容
  2. 总是听InputStream! (所有时间)
  3. 我如何在服务中创建这些内容? 我需要在我的服务中创建两个线程吗?

    我尝试过很多"想法"至于如何?而且我非常庞大"脏"代码......每次运行它都会崩溃。

    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创建列表方法?

1 个答案:

答案 0 :(得分:0)

所以我没有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分钟)。谁能一直听?