访问Android的OBEX服务器并读取数据

时间:2012-06-24 17:14:23

标签: android bluecove obex

我想知道是否可以从带有Bluecove的Windows中读取Android&#OBEX服务器上的数据(联系人,未接来电等)。我在windows中尝试了以下代码,但在连接时会返回OBEX_HTTP_NOT_ACCEPTABLE。 我可以告诉设备地址和通道是正确的,因为在运行此测试时,android会弹出一个对话框,询问我是否允许其他设备访问它的接口。 谢谢!

代码:

import java.io.IOException;
import javax.microedition.io.Connector;
import javax.obex.*;

public class PBAPTest1 {

    public static void main(String[] args) {
        String deviceAddress = "001122334455";
        int channel = 19;
        String serverURL = "btgoep://" + deviceAddress + ":" + channel + ";authenticate=false;encrypt=false;master=false";

        System.out.println("Connecting to " + serverURL);

        ClientSession clientSession = null;
        Operation op = null;
        HeaderSet hdr = null;

        try {
            clientSession = (ClientSession) Connector.open(serverURL);
            hdr = clientSession.connect(clientSession.createHeaderSet());
            if (hdr.getResponseCode() != ResponseCodes.OBEX_HTTP_OK) {
                System.out.println("Failed to connect: "
                        + hdr.getResponseCode()); // response: 198 OBEX_HTTP_NOT_ACCEPTABLE
                return;
            }
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }

        byte[] PBAP_TARGET = new byte[] { 0x79, 0x61, 0x35, (byte) 0xf0,
                (byte) 0xf0, (byte) 0xc5, 0x11, (byte) 0xd8, 0x09, 0x66, 0x08,
                0x00, 0x20, 0x0c, (byte) 0x9a, 0x66 };

        hdr.setHeader(HeaderSet.TARGET, PBAP_TARGET);
        hdr.setHeader(HeaderSet.NAME, "pb.vcf");
        hdr.setHeader(HeaderSet.TYPE, "x-bt/vcard-listing");
        try {
            op = clientSession.get(hdr);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

logcat的:

D/BluetoothEventLoop( 1551): Device property changed: XX:XX:XX:XX:XX:XX property: Connected value: true
D/BluetoothService( 1551): CONNECTION_STATE_CHANGE: XX:XX:XX:XX:XX:XX: 0 -> 2
D/Obex ServerSession( 1948): java.io.IOException: Software caused connection abort
D/BluetoothAdapterStateMachine( 1551): BluetoothOn process message: 52
D/BluetoothService( 1551): CONNECTION_STATE_CHANGE: XX:XX:XX:XX:XX:XX: 2 -> 0
D/BluetoothEventLoop( 1551): Device property changed: XX:XX:XX:XX:XX:XX property: Connected value: false

2 个答案:

答案 0 :(得分:2)

hdr = clientSession.connect(clientSession.createHeaderSet());

标题应设置为目标,值为pbap_target byte array。

答案 1 :(得分:0)

clientSession”connect需要目标标题。