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();
    }

}

在执行上述代码时,我在目标设备上收到了连接接受通知对话框,然后在执行 *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): 在 android.bluetooth.BluetoothInputStream.read(BluetoothInputStream.java:96) 07-11 14:48:07.059: W/System.err(4527): 在 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): 在 com.intel.bluetooth.obex.OBEXUtils.readFully(OBEXUtils.java:71) 07-11 14:48:07.059: W/System。错误(4527):在 com.intel.bluetooth.obex.OBEXSessionBase.readPacket(OBEXSessionBase.java:217)07-11 14:48:07.059:W/System.err(4527):在 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): 在 com.example.bluecovesample.SendFileTask.run(SendFileTask.java:78) 07-11 14:48:07.059: W/System.err(4527) : 在 java.lang.Thread.run(Thread.java:1019)在 com.intel.bluetooth.obex.OBEXClientSessionImpl.connect(OBEXClientSessionImpl.java:85) 07-11 14:48:07.059: W/System.err(4527): 在 com.example.bluecovesample.SendFileTask.run(SendFileTask. java:78) 07-11 14:48:07.059: W/System.err(4527): 在 java.lang.Thread.run(Thread.java:1019)在 com.intel.bluetooth.obex.OBEXClientSessionImpl.connect(OBEXClientSessionImpl.java:85) 07-11 14:48:07.059: W/System.err(4527): 在 com.example.bluecovesample.SendFileTask.run(SendFileTask. java:78) 07-11 14:48:07.059: W/System.err(4527): 在 java.lang.Thread.run(Thread.java:1019)

4

2 回答 2

3

出于某种原因,客户端正在切断连接。这可能是由于多种原因造成的,例如发送错误数据或错误配对。您是否确保正确配对设备?

未输入配对代码可能是个问题。这使得配对不合适。

解决方法是进入设置 -> 无线和网络 -> 蓝牙设置,长按“配对”设备并选择“取消配对”,然后单击产生带有文本字段的“蓝牙配对请求”窗口的设备,其中我输入了 PIN(配对码)。完成后,配对将成功。

您还可以使用名为 Wireshark 的程序来分析通过协议发送的消息。

于 2012-07-14T04:55:53.813 回答
0
import javax.obex.ClientSession;

您使用此类“ClientSession”。

据我所知,createHeaderSet 不是 ClientSession 的方法

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

于 2012-09-07T08:18:07.943 回答