sinat_32311469
2015-12-17 14:41
采纳率: 0%
浏览 2.8k

#安卓 自己写了一个可以连发数据的蓝牙APP可是运行时候崩溃了

本人菜鸟 先用Ontouch方法 + while循环 + thread休眠写了一个每隔100ms 按钮连续发送数据的蓝牙APP
但是在使用过程中 配对 以及连接蓝牙之后 可以发送大概5分钟 APP就崩溃了
崩溃是指:蓝牙还和我的手机连着 但是整个APP 按按钮没反应 也发送不了数据
但是其他菜单栏可以点开 关掉APP重开,APP就打不开了 只能关掉蓝牙设备 然后再打开APP

之后学习了handler用法 试着写了一段 整个可以使用的过程是长了一点 但是最后还是会崩溃和没有反应
以下是代码 就是几个按钮和几个 Ontouch的监听器和一个Handler的复写
求大神指导一下怎么解决!定有重谢!

 public class MainActivity extends Activity {
    private static final String TAG = "THINBTCLIENT";

    private static final boolean D = true;

    private BluetoothAdapter mBluetoothAdapter = null;

    private BluetoothSocket btSocket = null;

    private OutputStream outStream = null;
    String message;
    Button mButtonA;
    Button mButtonB;
    Button mButtonC;
    Button mButtonD;
    Button mButtonE;
    Button mButtonF;
    Button mButtonG;
    Button mButtonH;
    Button mButtonI;
    Button mButtonJ;
    Button mButtonK;
    ImageView Image;

    private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");


    private static String address = "20:15:08:05:81:54"; // <==要连接的蓝牙设备MAC地址

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

         mButtonA=(Button)findViewById(R.id.btnA);
         mButtonA.setOnTouchListener(new SendMsgListener());

         mButtonB=(Button)findViewById(R.id.btnB);
         mButtonB.setOnTouchListener(new SendMsgListener());


         mButtonC=(Button)findViewById(R.id.btnC);
         mButtonC.setOnTouchListener(new SendMsgListener());

         mButtonD=(Button)findViewById(R.id.btnD);
         mButtonD.setOnTouchListener(new SendMsgListener());


         mButtonE=(Button)findViewById(R.id.btnE);
         mButtonE.setOnTouchListener(new SendMsgListener());

         mButtonF=(Button)findViewById(R.id.btnF);
         mButtonF.setOnTouchListener(new SendMsgListener());

         mButtonG=(Button)findViewById(R.id.btnG);
         mButtonG.setOnTouchListener(new SendMsgListener());


         mButtonH=(Button)findViewById(R.id.btnH);
         mButtonH.setOnClickListener(new UpDown());

         mButtonI=(Button)findViewById(R.id.btnI);
         mButtonI.setOnClickListener(new UpDown());

         mButtonJ=(Button)findViewById(R.id.btnJ);
         mButtonJ.setOnTouchListener(new SendMsgListener());

         mButtonK=(Button)findViewById(R.id.btnK);
         mButtonK.setOnTouchListener(new SendMsgListener());


            if (D)
                Log.e(TAG, "+++ ON CREATE +++");
                mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

            if (mBluetoothAdapter == null) { 
                //Toast.makeText(this, "蓝牙设备不可用,请打开蓝牙!", Toast.LENGTH_LONG).show();
                Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                startActivity(intent);
                    //finish();
                    return;
            }


            if (!mBluetoothAdapter.isEnabled()) {
                    //Toast.makeText(this,  "请打开蓝牙并重新运行程序!", Toast.LENGTH_LONG).show();
                    Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                    startActivity(intent);
                    //finish();
                    return;

            }


            if (D)
            Log.e(TAG, "+++ DONE IN ON CREATE, GOT LOCAL BT ADAPTER +++");

    }

    public void onStart() {

        super.onStart();

        if (D) Log.e(TAG, "++ ON START ++");
}


@Override

public void onResume() {

        super.onResume();
        if (D) {
            Log.e(TAG, "+ ON RESUME +");
         Log.e(TAG, "+ ABOUT TO ATTEMPT CLIENT CONNECT +");

        }
        DisplayToast("正在尝试连接智能小车,请稍后····");
        BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);

        try {

           btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);

        } catch (IOException e) {

             DisplayToast("套接字创建失败!");
        }
        DisplayToast("成功连接智能小车!可以开始操控了~~~");
        mBluetoothAdapter.cancelDiscovery();
        try {

                btSocket.connect();
                DisplayToast("连接成功建立,数据连接打开!");


        } catch (IOException e) {

                try {
                    btSocket.close();

                } catch (IOException e2) {


                        DisplayToast("连接没有建立,无法关闭套接字!");
                }

        }


        // Create a data stream so we can talk to server.

        if (D)
            Log.e(TAG, "+ ABOUT TO SAY SOMETHING TO SERVER +");


}


@Override

public void onPause() {

        super.onPause();


        if (D)
            Log.e(TAG, "- ON PAUSE -");
        if (outStream != null) {
                try {
                        outStream.flush();
                } catch (IOException e) {
                        Log.e(TAG, "ON PAUSE: Couldn't flush output stream.", e);
                }

        }


        try {
                btSocket.close();
        } catch (IOException e2) {

                DisplayToast("套接字关闭失败!");
        }

}


@Override

public void onStop() {

        super.onStop();

        if (D)Log.e(TAG, "-- ON STOP --");

}


@Override

public void onDestroy() {

        super.onDestroy();

        if (D) Log.e(TAG, "--- ON DESTROY ---");

}
public void DisplayToast(String str)
{
    Toast toast=Toast.makeText(this, str, Toast.LENGTH_LONG);
    toast.setGravity(Gravity.TOP, 0, 220);
    toast.show();

}





    class SendMsgListener implements OnTouchListener{

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO 自动生成的方法存根
            switch (v.getId()) {
            case R.id.btnA: 
                message = "A";
                break;
            case R.id.btnB: 
                message = "B";
                break;
            case R.id.btnC: 
                message = "C";
                break;
            case R.id.btnD: 
                message = "D";
                break;
            case R.id.btnE: 
                message = "E";
                break;
            case R.id.btnF: 
                message = "F";
                break;      
                case R.id.btnG: 
                    message = "G";
                    break;
                case R.id.btnH: 
                    message = "H";
                    break;
                case R.id.btnI: 
                    message = "I";
                    break;
                case R.id.btnJ: 
                    message = "J";
                break;
                case R.id.btnK: 
                    message = "K";
                break;
            default:
                break;
            }

            if (event.getAction()==MotionEvent.ACTION_DOWN) {
                handler.post(SendMsg);
            }
            else if (event.getAction() == MotionEvent.ACTION_UP) {
                handler.removeCallbacks(SendMsg);
            }

            return false;
        }

    }

    Handler handler = new Handler();

    Runnable SendMsg = new Runnable() {
        public void run() {
             byte[] msgBuffer;

                try {
                    outStream = btSocket.getOutputStream();
                    } catch (IOException e1) {
                    // TODO 自动生成的 catch 块
                    e1.printStackTrace();
                    }
                msgBuffer = message.getBytes();

                try {
                        outStream.write(msgBuffer);
                    } catch (IOException e) {
                        // TODO 自动生成的 catch 块
                        e.printStackTrace();
                    }

                handler.postDelayed(SendMsg, 100);
        }
    };


    class UpDown implements OnClickListener{

        @Override
        public void onClick(View v) {
            // TODO 自动生成的方法存根
            if(v.getId()==R.id.btnI){
                message= "I";
            }
            else if (v.getId()==R.id.btnH) {
                message= "H";
            }

            byte[] msgBuffer;

            try {
                outStream = btSocket.getOutputStream();
                } catch (IOException e1) {
                // TODO 自动生成的 catch 块
                e1.printStackTrace();
                }
            msgBuffer = message.getBytes();

            try {
                    outStream.write(msgBuffer);
                } catch (IOException e) {
                    // TODO 自动生成的 catch 块
                    e.printStackTrace();
                }
        }

    }
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • 为惠惠献上PHP 2015-12-17 15:36

    每隔100ms发送数据的代码在哪里?

    点赞 打赏 评论
  • sinat_32311469 2015-12-18 00:35

    求大神来帮忙解答啊,重谢

    点赞 打赏 评论