5

我已经下载了蓝牙聊天应用程序的 android 示例,以使用蓝牙在两个 android 设备之间发送文本。

我已经在两个安卓设备上安装并运行了这个应用程序。

我在该代码中遇到了很多问题

  1. 服务发现失败异常 -已修复
  2. java.io.IOException:软件导致连接中止 -已修复
  3. java.io.IOException:Peer 重置连接 -对此感到震惊

1.清除Service discovery failed异常:

对于服务发现失败异常,在蓝牙聊天服务中,我检查了 sdk 版本以及大于 Ginger Bread 的 sdk 版本,

我已经使用 Method 类来调用 RfCOMM 套接字连接,并且我的第一个异常在这种方法中得到了解决。

异常代码

 tmp = device.createRfcommSocketToServiceRecord(MY_UUID);

修复异常代码

try {
if (Build.VERSION.SDK_INT < 9) { 
try {
     tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
       } catch (IOException e1) {
            e1.printStackTrace();
       }
       } else {
   Method m = null;
  try {
  m = device.getClass().getMethod("createRfcommSocket",
        new Class[] { int.class });
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
                }
                try {
                    tmp = (BluetoothSocket) m.invoke(device, 1);
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
                }
            } catch (Exception e) {
                Log.e(TAG, "create() failed", e);
            }

2.清除java.io.IOException:软件导致连接中止

我已经检查了 InputStream 是否可用

异常代码

bytes = mmInStream.read(buffer);

修复异常代码

 if (mmInStream.available() > 0) {
                            // Read from the InputStream
                            bytes = mmInStream.read(buffer);

现在我的问题是当我尝试在连接的设备之间发送数据时,它会在写入输出流时抛出以下错误消息“Connection Reset By Peer”

异常代码:

public void write(byte[] buffer, int start, int end) {
mHandler.obtainMessage(BluetoothChat.MESSAGE_WRITE, -1, -1, buffer).sendToTarget();
    if (mmOutStream !=null) {
        try {
             mmOutStream.write(buffer);
        } catch (IOException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
        }
    }
    else{
        Log.e("OutputStream Null","");
    }
}

== 更新 ==

即使显示两个设备都已连接,accept 方法返回失败

**06-19 10:30:23.625: D/BluetoothChatService(2630): connected
06-19 10:30:23.625: D/BluetoothChatService(2630): cancel Thread[AcceptThread,5,main]
06-19 10:30:23.625: V/BluetoothSocket.cpp(2630): abortNative
06-19 10:30:23.625: V/BluetoothSocket.cpp(2630): ...asocket_abort(50) complete
06-19 10:30:23.625: V/BluetoothSocket.cpp(2630): ...accept(50, RFCOMM) = -1 (errno 125)
06-19 10:30:23.632: E/BluetoothChatService(2630): accept() failed
06-19 10:30:23.632: E/BluetoothChatService(2630): java.io.IOException: Operation Canceled
06-19 10:30:23.632: E/BluetoothChatService(2630):   at android.bluetooth.BluetoothSocket.acceptNative(Native Method)
4

3 回答 3

1

我自己对聊天示例有很多问题,所以我尝试了另一种方法。

首先,您必须使一个设备主机和另一个客户端,这与该示例非常有效。如果您没有运行此程序,我也可以为您提供该代码。

使用上面的类,您可以获得连接的套接字。使用它将它传递给此类,然后您可以使用 write 方法发送。并且传入的消息会在运行方法中自动解析(我在前面添加了一个消息 ID 和长度,这就是为什么那里发生了这么多事情的原因)

private class ConnectedThread extends Thread {

/** the connection socket */
private final BluetoothSocket mmSocket;

/** input stream for incoming messages */
private final InputStream mmInStream;

/** output stream for outgoing messages */
private final OutputStream mmOutStream;

/**
 * save the socket and get the streams
 * 
 * @param socket
 */
public ConnectedThread(BluetoothSocket socket) {
    mmSocket = socket;
    InputStream tmpIn = null;
    OutputStream tmpOut = null;

    // Get the input and output streams, using temp objects because
    // member streams are final
    try {
        tmpIn = socket.getInputStream();
        tmpOut = socket.getOutputStream();
    } catch (IOException e) {
        e.printStackTrace();
    }

    mmInStream = tmpIn;
    mmOutStream = tmpOut;
}

/**
 * reads incoming data and splits it into single messages
 */
public void run() {
    /** buffer for a single byte message */
    byte[] buffer = new byte[1024];

    /** number of bytes returned from read() */
    int bytes;

    // Keep listening to the InputStream until an exception occurs
    while (true) {
        try {
            // read overhead from the InputStream
            bytes = mmInStream.read(buffer, 0, LEN_SIZE + LEN_TYPE);

            // if no bytes are read, wait for a new message
            if (bytes == 0)
                continue;

            // get the size bytes and convert them to int
            byte[] size_arr = new byte[LEN_SIZE];
            for (int i = 0; i < LEN_SIZE; i++)
                size_arr[i] = buffer[i];
            int size = convertByteArrayToInt(size_arr, LEN_SIZE);

            // the type is 1 byte after the size
            byte type = buffer[LEN_SIZE];

            // array for the output data
            byte[] output = new byte[size + LEN_TYPE];
            output[0] = type;

            // current position, read until cPos == size
            int cPos = 0;
            while (cPos < size) {
                // either read the buffer lenght or the remaining bytes
                int read_len = Math.min(buffer.length, size - cPos);
                bytes = mmInStream.read(buffer, 0, read_len);

                // write the bytes to the output
                for (int i = 0; i < bytes; i++)
                    output[cPos + i + LEN_TYPE] = buffer[i];

                // increase the current position
                cPos += bytes;
            }

            // add the message to the queue
            mMessageData.add(output);

            // tell the service about the new message
            mHandler.obtainMessage(BluetoothService.CONNECTION_RECV_MSG, mConnectionAddress).sendToTarget();

        } catch (IOException e) {
            // tell the service about the disconnect
            mHandler.obtainMessage(BluetoothService.CONNECTION_LOST, mConnectionAddress).sendToTarget();
            e.printStackTrace();
            break;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

/**
 * writes a byte stream to the connection
 * 
 * @param bytes
 *            the byte stream
 */
public void write(byte[] bytes) {
    try {
        mmOutStream.write(bytes, 0, bytes.length);
        mmOutStream.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

/**
 * close the socket
 */
public void cancel() {
    try {
        mmSocket.close();
    } catch (IOException e) {
    }
}

}

这对我有用,我希望它也对你有用。如果您有任何问题,请随时提问:-)

于 2013-06-18T11:32:52.410 回答
0

我认为你的阅读是错误的。我有线

while (true) {
    try {
        // read overhead from the InputStream
        bytes = mmInStream.read(buffer, 0, LEN_SIZE + LEN_TYPE);

在我的代码中,它运行良好,没有 .available(),@gtRfnkN 也是如此。根据我对 Java 文档的解释,您不能将 .available() 与 InputStream 一起使用。请参阅:http://docs.oracle.com/javase/6/docs/api/java/io/InputStream.html#available()特别是它说“类 InputStream 总是返回零”的部分。

以防万一,这是我写的一个例子:

    public void write(byte[] buffer){
        try{
            //a delay of 20ms occurs after each flush...
            mmOutStream.write((byte)buffer[0]);
            mmOutStream.flush();
            try {
                Thread.sleep(20);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
于 2013-06-25T10:59:10.983 回答
0

你可以试试这个,虽然它可能不应该是最终修复。当我按照那个例子进行操作时,onResume()我的主要活动中有这个,我想你也有类似的东西:

@Override
public synchronized void onResume() {
    super.onResume();
    if(D) Log.e(TAG, "+ ON RESUME +");
}

尝试这个:

@Override
public synchronized void onResume() {
//    super.onResume();
    if(D) Log.e(TAG, "+ ON RESUME +");
}

这将阻止主要活动多次创建 AcceptThread。

于 2013-06-24T23:04:52.423 回答