将收到的消息发送回UI活动,但应用程序卡住了

时间:2015-12-26 16:04:13

标签: android bluetooth thread-safety

static class BluetoothInHandler extends Handler {
        private final WeakReference<Bluetooth_dataDisplay> mActivity;

        BluetoothInHandler(Bluetooth_dataDisplay activity) {
            mActivity = new WeakReference<>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            final Bluetooth_dataDisplay thizz = mActivity.get();
            if (thizz == null) return;
            if (msg.what == thizz.handlerState) {
               String readMessage = (String) msg.obj;
               thizz.myLabel.setText(readMessage);


           }
        }//end of handle message


 }//end of Bluetoothin handler


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.bluetooth_datadisplay);
        myLabel = (TextView)findViewById(R.id.label);

        mMyHandler = new BluetoothInHandler(this);



    }//end oncreate



@Override
    public void onResume() {
        super.onResume();  
        String MAC = getIntent().getStringExtra("MAC");
        mAdapter = BluetoothAdapter.getDefaultAdapter();
        BluetoothDevice bluetoothDevice = mAdapter.getRemoteDevice(MAC);
        ConnectingThread t = new ConnectingThread(bluetoothDevice);
        t.start();

    }

//Setting Up a Connecting Client
private class ConnectingThread extends Thread {
    OutputStream mmOutputStream;
    InputStream mmInputStream;
    StringBuilder recDataString = new StringBuilder();
   // private final BluetoothSocket bluetoothSocket;
    private final BluetoothDevice bluetoothDevice;
    final Handler handler = new Handler();
    public ConnectingThread(BluetoothDevice device) {

        BluetoothSocket temp = null;
        bluetoothDevice = device;


        // Get a BluetoothSocket to connect with the given BluetoothDevice
        try {

            temp = bluetoothDevice.createRfcommSocketToServiceRecord(uuid);
        } catch (IOException e) {
            e.printStackTrace();
        }
        bluetoothSocket = temp;
    }

    public void run() {
        // Cancel any discovery as it will slow down the connection

        mAdapter.cancelDiscovery();


        try {

            // This will block until it succeeds in connecting to the device
            // through the bluetoothSocket or throws an exception
            bluetoothSocket.connect();
            Log.e("bluetooth socket",".connect");
            mmOutputStream = bluetoothSocket.getOutputStream();
            mmInputStream = bluetoothSocket.getInputStream();

            //beginListenForData();
            Bluetooth_dataDisplay.this.runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    Toast.makeText(Bluetooth_dataDisplay.this, "Connected with Device!", Toast.LENGTH_SHORT).show();

                }
            });

          handler.post(new Runnable() {

               public void run() {

                   byte[] buffer = new byte[1024];
                   int bytes;

                   // Keep looping to listen for received messages
                   while (true) {
                       try {
                           bytes = mmInputStream.read(buffer);          
                           String readMessage = new String(buffer, 0, bytes);
                           // Send the obtained bytes to the UI Activity via handler
                           mMyHandler.obtainMessage(handlerState, bytes, -1, readMessage).sendToTarget();
                       } catch (IOException e) {
                           break;
                       }
                   }
               }
           });
            //beginListenForData();
            //Log.e("begin", "begindata");

        } catch (IOException connectException) {
            connectException.printStackTrace();

            try {
                bluetoothSocket.close();
                Log.e("click6", "blueclose");
            } catch (IOException closeException) {
                closeException.printStackTrace();
            }
        }

    }

尝试将数据从线程发送回UI活动但我的应用程序卡住了。 :(我不知道它有什么问题。请帮忙。没有错误也没有应用程序崩溃。我也尝试过runnableUIthread。它也没有用。所以我不知道是什么现在就做。

log.e(test,handler)

1 个答案:

答案 0 :(得分:0)

func updateSearchResultsForSearchController(searchController: UISearchController)