Android软件导致连接中止和异步任务

时间:2014-11-30 05:51:52

标签: android bluetooth

我使用bluetooth_access异步任务来建立连接,我需要继续使用我建立的蓝牙插槽的输入流和输出流。

我遇到的问题是当我点击button1或button2时,它有时(并非所有时间)都会导致软件导致连接中止。单击时,它会被out.write(字节)触发。

public class MainActivity extends Activity implements OnClickListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button1 = (Button)this.findViewById(R.id.button1);
        button2 = (Button)this.findViewById(R.id.button2);
        button3 = (Button)this.findViewById(R.id.button3);
        button1.setEnabled(false);
        button2.setEnabled(false);
        button3.setEnabled(false);

        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);


         try {
                for (int i = 0; i < 3; i++) {
                    test();
                }

} catch (Exception e) {
                e.printStackTrace();
            }
    }

    private boolean connected = false;
    private BluetoothSocket sock;
    private InputStream in;
    private OutputStream out;
    private Button button1;
    private Button button2;
    private Button button3;
    private TextView data_t;
    private   BufferedReader in_read;

    public void test() throws Exception {
        if (connected) {
            return;
        }

        new bluetooth_access().execute("");

             }


    @Override
    public void onClick(View v){
        // TODO Auto-generated method stub
         if(v.equals(button1)){
             String s = "turn left";
             byte[] bytes=s.getBytes();


                try {
                    out.write(bytes);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


            }

         if(v.equals(button2)){
             String s = "turn right";
           byte[] bytes=s.getBytes();

             try {
                out.write(bytes);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


                //new post_data().execute("");

         }

         if(v.equals(button3)){
             String s = "read ADC";
               byte[] bytes=s.getBytes();

                 try {
                    out.write(bytes);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

         }

    }


private class bluetooth_access extends AsyncTask<String, Void, String> {


  @Override
  protected String doInBackground(String... params) {
      BluetoothDevice Pi = BluetoothAdapter.getDefaultAdapter().
              getRemoteDevice("00:15:83:0C:BF:EB");

          Method m=null;
        try {
            m = Pi.getClass().getMethod("createRfcommSocket",
                new Class[] { int.class });
        } catch (NoSuchMethodException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        try {
            sock = (BluetoothSocket)m.invoke(Pi, Integer.valueOf(1));
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


          try {
            sock.connect();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
          Log.d("PiTest", "++++ Connected");

          return null;
  }        

  @Override
  protected void onPostExecute(String result) {    
    ;
      TextView text = (TextView) findViewById(R.id.textView5);

      text.setText("Connected  through Bluetooth");

    button1.setEnabled(true);
        button2.setEnabled(true);
        button3.setEnabled(true);

        //  original 
      try {
        in = sock.getInputStream();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
      try {
        out=sock.getOutputStream();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

  }

  @Override
  protected void onPreExecute() {
  }

  @Override
  protected void onProgressUpdate(Void... values) {
  }
}

1 个答案:

答案 0 :(得分:0)

  • 您正在尝试从主UI线程(来自onClick)写入套接字流,我会考虑从asynctask本身或其他线程(如果需要)进行所有套接字通信。

  • 您的connected变量未设置,不确定是否是故意的,它可能导致多次执行您的蓝牙asynctask。