简体   繁体   English

android蓝牙聊天应用程序(设备立即失去连接)

[英]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. 我的设置:我有一台Mac PC,正在将其用作虚拟串行端口来与我的Android Nexus S手机通信。 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. 最初,我尝试使用2部android手机尝试蓝牙聊天应用程序,以确认其正常工作。 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. 我的用例:我有一个读取RFid标签并将数据发送到android手机以收集信息的设备。

I am using my PC to represent my device for now. 我现在使用PC代表我的设备。

++++++++++++++++++ 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. 我尝试通过手机连接到PC,最初我得到“正在连接...”状态栏的更新,大约15秒后,我收到一条吐司消息,说“我已连接到PC”,但是当我收到“设备断开连接”吐司。 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); ) 特别是这一行( 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. 当我查看logcat时,输入输出上的read()的I / O异常是“ software caused connection abort ”。

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 我已将终端启动,并等待使用/ 9600 baud屏幕命令在/dev/tty.Nexus ....上接收输入

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. 我将其打印到日志中,看来它不是NULL。 Every time I step through though it dies at the ConnectThread not in the ConnectedThread . 每次我逐步通过时都会死于ConnectThread而不是ConnectedThread中

The following portion of code: specifically this line ( mmSocket.connect(); ) 代码的以下部分:特别是这一行( 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. 如何在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. 由于您尝试在2部手机上运行该应用程序且该应用程序正常运行,因此我认为问题出在PC上。

I have posted an entry about Android and Java Bluetooth here . 我已经发布了关于Android和Java蓝牙条目这里 Hope it will help. 希望它会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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