Arduino-整个HC-06套接字问题的Android数据传输

时间:2018-09-17 13:43:57

标签: android arduino android-bluetooth arduino-uno

即使是程序员, 对android studio来说是相对较新的,这是我第一次处理蓝牙,接收器,广播等内容。.我按照一些教程创建了我的监控应用程序,该应用程序应该读取arduino的HC-06模块发送的数据,但是我有一个带有连接线程的泵,它不断捕获socket.close()而不是尝试socket.connect(),我不知道为什么会这样。 这是我的ConnectThread.run()的代码:

.addClass('active')

引发的异常:

public ConnectThread(BluetoothDevice device, UUID uuid) {
        Log.d(TAG, "ConnectThread: started.");
        mmDevice = device;
        deviceUUID = uuid;
    }

    public void run(){
        BluetoothSocket tmp = null;
        Log.i(TAG, "RUN mConnectThread ");

        // Get a BluetoothSocket for a connection with the
        // given BluetoothDevice
        try {
            Log.d(TAG, "ConnectThread: Trying to create InsecureRfcommSocket 
using UUID: "
                    +MY_UUID_INSECURE );
            tmp = mmDevice.createRfcommSocketToServiceRecord(deviceUUID);
        } catch (IOException e) {
            Log.e(TAG, "ConnectThread: Could not create InsecureRfcommSocket 
" + e.getMessage());
        }

        mmSocket = tmp;

        // Always cancel discovery because it will slow down a connection
        mBluetoothAdapter.cancelDiscovery();

        // Make a connection to the BluetoothSocket

        try {
            // This is a blocking call and will only return on a
            // successful connection or an exception
            mmSocket.connect();

            Log.d(TAG, "run: ConnectThread connected.");
        } catch (IOException e) {
            // Close the socket
            try {
                mmSocket.close();
                Log.d(TAG, "run: Closed Socket.");
            } catch (IOException e1) {
                Log.e(TAG, "mConnectThread: run: Unable to close connection 
in socket " + e1.getMessage());
            }
            Log.d(TAG, "run: ConnectThread: Could not connect to UUID: " + 
MY_UUID_INSECURE );
        }


        connected(mmSocket,mmDevice);
    }

但是,即使关闭了套接字,connectedThread仍然可以继续工作;如果这是它的工作方式,则为idk,但这是ConnectedThread.run():

D/BluetoothSocket: close() this: android.bluetooth.BluetoothSocket@3390c6f, mSocketState: INIT
D/BluetoothConnectionServ: run: Closed Socket.
run: ConnectThread: Could not connect to UUID: 00001101-0000-1000-8000-00805f9b34fb
connected: Starting.

及其例外:

public void run(){
        byte[] buffer = new byte[1024];  // buffer store for the stream

        int bytes; // bytes returned from read()

        // Keep listening to the InputStream until an exception occurs
        while (true) {
            // Read from the InputStream
            try {
                bytes = mmInStream.read(buffer);
                String incomingMessage = new String(buffer, 0, bytes);
                Log.d(TAG, "InputStream: " + incomingMessage);

                Intent incomingMessageIntent = new Intent("incomingMessage");
                incomingMessageIntent.putExtra("theMessage",incomingMessage);
                LocalBroadcastManager.getInstance(mContext).sendBroadcast(incomingMessageIntent);

            } catch (IOException e) {
                Log.e(TAG, "write: Error reading Input Stream. " + e.getMessage() );
                break;
            }
        }
    }

0 个答案:

没有答案