socket.connect()throws socket可能在Android中被关闭错误到蓝牙到Arduino连接

时间:2017-02-25 21:16:27

标签: android sockets bluetooth

我正在编写一个应用程序来控制arduino并从arduino传感器中读取。 写arduino工作正常,但arduino的读取功能不正常。

我有一个在onCreate方法中创建的处理程序,以及一个连续循环的专用读取线程。

然而,读取线程永远不会被调用,因为每当程序调用btSocket.connect()时它都会抛出错误

$ git diff

在此处显示的onclicklistner中调用socket.connect()

V/TAG: java.io.IOException: read failed, socket might closed or timeout, read ret: -1

我不明白它为什么不在那里工作,因为socket.connect()方法在代码的其他地方工作(否则我将如何写入蓝牙)。

在onCreate中调用此方法,似乎工作正常。

        btnRead.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            if (myBluetooth != null) {
                try {
                    btSocket.connect();
                    Log.v(str, "Socket Connected");
                    read = new readThread(btSocket);
                    read.start();
                    Log.v(str, "READ THREAD STARTED");
                } catch (IOException e) {
                    Log.v(str, "Catch on bt.connect");
                    Log.v(str, e.toString());
                    try {
                        btSocket.close();
                    } catch (IOException e2) {
                        //insert code to deal with this
                    }
                }
            }
        }
    });

}

那么为什么onclicklistener中没有连接套接字?

以下是完整的代码:

 private class ConnectBT extends AsyncTask<Void, Void, Void>  // UI thread
    {
    private boolean ConnectSuccess = true; //if it's here, it's almost connected

    @Override
    protected void onPreExecute()
    {
        progress = ProgressDialog.show(ledControl.this, "Connecting...", "Please wait!!!");  //show a progress dialog
    }

    @Override
    protected Void doInBackground(Void... devices) //while the progress dialog is shown, the connection is done in background
    {
        try
        {
            if (btSocket == null || !isBtConnected)
            {
                myBluetooth = BluetoothAdapter.getDefaultAdapter();//get the mobile bluetooth device
                BluetoothDevice device = myBluetooth.getRemoteDevice(address);//connects to the device's address and checks if it's available
                btSocket = device.createInsecureRfcommSocketToServiceRecord(myUUID);//create a RFCOMM (SPP) connection
                BluetoothAdapter.getDefaultAdapter().cancelDiscovery();
                btSocket.connect();//start connection
            }
        }
        catch (IOException e)
        {
            ConnectSuccess = false;//if the try failed, you can check the exception here
        }
        return null;
    }
    @Override
    protected void onPostExecute(Void result) //after the doInBackground, it checks if everything went fine
    {
        super.onPostExecute(result);

        if (!ConnectSuccess)
        {
            msg("Connection Failed. Is it a SPP Bluetooth? Try again.");
            finish();
        }
        else
        {
            msg("Connected.");
            isBtConnected = true;
        }
        progress.dismiss();
    }
}

}

0 个答案:

没有答案