蓝牙对等连接重置

8

大家好,我正在尝试使用名为bluecove的第三方API和OBEX将文件从Android设备发送到特定的蓝牙设备。当连接头设置时,我可以连接设备,但是我遇到了“对等连接重置”的问题。

以下是我的代码片段。非常感谢您的任何帮助。

在我的活动中,我使用以下代码来处理发送文件:

BlueCoveImpl.setConfigObject(
                BlueCoveConfigProperties.PROPERTY_ANDROID_CONTEXT,
                this);
String deviceAddress = "0007ABB6D96E";
            int channel = 9;
            String obexURL = "btgoep://" + deviceAddress + ":"
                    + "0000110600001000800000805f9b34fb" + ";android=true";

            // String obexURL = "btgoep://" + deviceAddress + ":" + channel
            // + ";android=true";
            String fileToSend = "sdcard/DSCN9379.jpg";
            System.out.println("Connecting to " + obexURL);
            FileInputStream stream = new FileInputStream(fileToSend);
            File f = new File(fileToSend);
            int size = (int) f.length();
            byte file[] = new byte[size];
            stream.read(file);
            String filename = f.getName();
            System.out
                    .println("***************Now sending file to device*****************");
            SendFileTask task = new SendFileTask(MainActivity.this, obexURL,
                    file, filename);
            Thread thread = new Thread(task);
            thread.start();

我正在尝试连接头文件设置为以下内容的Bluecove连接类对象。

package com.example.bluecovesample;

import java.io.OutputStream;

import javax.microedition.io.Connection;
import javax.microedition.io.Connector;
import javax.obex.ClientSession;
import javax.obex.HeaderSet;
import javax.obex.Operation;
import javax.obex.ResponseCodes;

//import static write ;  
import android.content.Context;
import android.widget.Toast;

public class SendFileTask implements Runnable {

    private byte[] file;
    private String filename;
    public static final int WRITE = 2;

    String logString;

    int responseCode;
    Context context;
    String obexURL;

    public SendFileTask(Context ctx, String obexUrl, byte[] file,
            String filename) {
        this.context = ctx;
        this.obexURL = obexUrl;
        this.file = file;
        this.filename = filename;

    }

    public void run() {
        Connection connection = null;
        try {
            System.out.println(obexURL);
            // for ( int i = 0; i 3; i++ )
            {
                // connection = Connector.open("btgoep://"+btConnectionURL+
                // ":6");
                connection = Connector.open(obexURL);
            }
            // connection obtained

            // now, let's create a session and a headerset objects
            ClientSession cs = (ClientSession) connection;

            HeaderSet hs = cs.createHeaderSet();
            // toastMsg(hs.toString());
            // now let's send the connect header
            // cs.notifyAll();

            **cs.connect(hs);**

            hs.setHeader(HeaderSet.NAME, filename);
            // System.out.println("sfname:"+filename);
            int dotIndex = filename.lastIndexOf(".");
            // System.out.println("doti:"+dotIndex);
            String extension = filename.substring(dotIndex).toLowerCase();
            // System.out.println("sfname:"+extension);

            if (extension.equals(".txt")) {
                hs.setHeader(HeaderSet.TYPE, "text/plain");
            } else if (extension.equals(".jpg") || extension.equals(".jpeg")) {
                hs.setHeader(HeaderSet.TYPE, "image/jpeg");
            } else if (extension.equals(".mpeg") || extension.equals(".mpg")
                    || extension.equals(".mp3")) {
                hs.setHeader(HeaderSet.TYPE, "video/mpeg");
            } else if (extension.equals(".wav")) {
                hs.setHeader(HeaderSet.TYPE, "audio/x-wav");
            } else if (extension.equals(".3gp")) {
                hs.setHeader(HeaderSet.TYPE, "image/jpeg");
            } else if (extension.equals("mid") || extension.equals("rmi")) {
                hs.setHeader(HeaderSet.TYPE, "audio/mid");
            }

            hs.setHeader(HeaderSet.LENGTH, new Long(file.length));

            Operation putOperation = cs.put(hs);

            // System.out.println("response code:"+putOperation.getResponseCode());

            // putOperation.getResponseCode();
            // this.responseCode = putOperation.getResponseCode();

            OutputStream outputStream = putOperation.openOutputStream();
            outputStream.write(file);
            // file push complete

            outputStream.close();
            responseCode = putOperation.getResponseCode();

            putOperation.close();

            cs.disconnect(null);

            connection.close();

            // file successfully sent

            System.out.println("RESPONSE CODE " + responseCode);
            if (responseCode == ResponseCodes.OBEX_HTTP_OK) {
                System.out.println("FILE SUCCESSFULLY SENT " + filename);
            }

            else {
                System.out.println("FILE SUCCESSFULLY NOT SENT" + filename
                        + " not in exception");

            }

        } catch (Exception e) {

            System.out.println("FILE SUCCESSFULLY NOT SENT" + filename
                    + " in exception");
            // System.out.println("In exception");
            e.printStackTrace();

            try {
                connection.close();
            } catch (Exception ex) {
                System.out.println("error closing connection" + ex.toString());
            }
        }
    }

    private void toastMsg(String msg) {
        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
    }

}

执行上述代码时,在目标设备上收到连接接受通知对话框,然后在我的logcat中执行行*cs.connect(hs)时通知对等方连接重置。Logcat堆栈跟踪如下:
07-11 14:48:07.044: W/System.err(4527): java.io.IOException: Connection reset by peer 07-11 14:48:07.052: W/System.err(4527): at android.bluetooth.BluetoothSocket.readNative(Native Method) 07-11 14:48:07.059: W/System.err(4527): at android.bluetooth.BluetoothSocket.read(BluetoothSocket.java:336) 07-11 14:48:07.059: W/System.err(4527): at android.bluetooth.BluetoothInputStream.read(BluetoothInputStream.java:96) 07-11 14:48:07.059: W/System.err(4527): at com.intel.bluetooth.BluetoothStackAndroid.connectionRfRead(BluetoothStackAndroid.java:437) 07-11 14:48:07.059: W/System.err(4527): at com.intel.bluetooth.BluetoothRFCommInputStream.read(BluetoothRFCommInputStream.java:139) 07-11 14:48:07.059: W/System.err(4527): at com.intel.bluetooth.obex.OBEXUtils.readFully(OBEXUtils.java:71) 07-11 14:48:07.059: W/System.err(4527): at com.intel.bluetooth.obex.OBEXSessionBase.readPacket(OBEXSessionBase.java:217) 07-11 14:48:07.059: W/System.err(4527): at com.intel.bluetooth.obex.OBEXClientSessionImpl.connectImpl(OBEXClientSessionImpl.java:100) 07-11 14:48:07.059: W/System.err(4527): at com.intel.bluetooth.obex.OBEXClientSessionImpl.connect(OBEXClientSessionImpl.java:85) 07-11 14:48:07.059: W/System.err(4527): at com.example.bluecovesample.SendFileTask.run(SendFileTask.java:78) 07-11 14:48:07.059: W/System.err(4527): at java.lang.Thread.run(Thread.java:1019)
2个回答

3
由于某种原因,客户端正在切断连接。这可能是由于发送错误数据或错误配对等原因引起的。您确定已正确配对设备吗?
未输入配对代码可能是一个问题。这会使配对不正确。
解决方法是进入“设置”-> “无线和网络”->“蓝牙设置”,长按“配对”设备并选择“取消配对”,然后单击产生“蓝牙配对请求”窗口的设备,并在其中键入PIN(配对代码)。完成此操作后,配对将成功。
您还可以使用名为Wireshark的程序分析通过协议发送的消息。

我能够连接,但当我到达cs.connect(hs)时,尝试为连接的设备设置头文件集以发送文件时,它会断开连接。还有其他解决方案吗? - Senthil Mg
你的清单文件中是否有READ_EXTERNAL_STORAGE和BLUETOOTH权限? - Erol
1
如果是的话,请查看这里的源代码:http://gitorious.org/android-obex 并比较你的代码。 - Erol
我真的尝试从您提供的链接中提取代码,但是我不理解如何在我的系统中设置Gitorious。当我检出时,它会要求一些哈希键,我应该在哪里生成这个哈希键? - Senthil Mg
谢谢您的回复。您能否提供一步一步的命令,以便我在我的Windows开发环境中执行上述Fuse文件并将其用于Android应用程序?我也阅读了这个自述文件和安装文件,但我不知道如何使用命令工具。 - Senthil Mg
我尽了最大的努力,希望能用有限的知识帮到你,剩下的就看你自己了。请开一个新问题讨论它或在网上搜索相关信息。祝你好运。 - Erol

0
import javax.obex.ClientSession;

你使用了这个类 "ClientSession"。

据我所知,"createHeaderSet" 不是 "ClientSession" 的一个方法。

我想知道你是如何成功编译你的应用程序的。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接