简体   繁体   中英

android bluetooth chat app (device loses connection immediately)

I have been going through several threads on here and didn't come across an answer to the issue I am running into.

My setup: I have a Mac pc that I am using as a virtual serial port to communicate with my android Nexus S phone. Running the bluetooth chat app on the phone and using it as a client to talk to the virt comm I set up.

Initially I tried the bluetooth chat app with 2 android phones to confirm it works, which it does. I can send texts back and forth.

My Use case: I have a device that reads RFid tags and sends the data to an android phone to collect the info.

I am using my PC to represent my device for now.

++++++++++++++++++ Ok to the problem,

I try to connect to the pc from my phone and initially I get a "connecting...." status bar update and after 15secs or so I get a toast message saying "I am connected to the pc" but immediately after I get "device lost connection" toast. Then the status bar goes to "not connected"

When I step through with the debugger, it seems to fail at the following portion of the bluetooth chat app. Specifically this line ( bytes = mmInStream.read(buffer); )

    public void run() {
        Log.i(TAG, "BEGIN mConnectedThread");
        byte[] buffer = new byte[1024];
        int bytes;

        // Keep listening to the InputStream while connected
        while (true) {
            try {
                // Read from the InputStream
                bytes = mmInStream.read(buffer);

                // Send the obtained bytes to the UI Activity
                mHandler.obtainMessage(BluetoothChat.MESSAGE_READ, bytes, -1, buffer)
                        .sendToTarget();
            } catch (IOException e) {
                Log.e(TAG, "disconnected", e);
                connectionLost();
                break;
            }
        }
    }

When I look in logcat, the i/o exception is " software caused connection abort " for the read() on inputstream.

Questions: Does this have to do with my virtual port not setup right? I have the terminal up and waiting to receive input on /dev/tty.Nexus.... using the screen command @ 9600 baud

Otherwise, I thought maybe the socket which the inputstream connects to is unavailable somehow. I printed that to log and it seems like it was not NULL. Every time I step through though it dies at the ConnectThread not in the ConnectedThread .

The following portion of code: specifically this line ( mmSocket.connect(); )

        public void run() {
        Log.i(TAG, "BEGIN mConnectThread");
        setName("ConnectThread");

        // Always cancel discovery because it will slow down a connection
        mAdapter.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();
        } catch (IOException e) {
            connectionFailed();
            // Close the socket
            try {
                mmSocket.close();
            } catch (IOException e2) {
                Log.e(TAG, "unable to close() socket during connection failure", e2);
            }
            // Start the service over to restart listening mode
            BluetoothChatService.this.start();
            return;
        }

        // Reset the ConnectThread because we're done
        synchronized (BluetoothChatService.this) {
            mConnectThread = null;
        }

        // Start the connected thread
        connected(mmSocket, mmDevice);
    }

I wonder if the socket variable is losing scope due to multi-threading and the socket is being passed around?

Thanks

How you set up the virtual serial post on your Mac PC. Since you have tried to run the app on 2 phones and it's working, I think the problem is on the PC.

I have posted an entry about Android and Java Bluetooth here . Hope it will help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM