Access Android's OBEX server and read data

徘徊边缘 提交于 2019-12-07 19:12:25

问题


I was wondering if it's possible to read data (contacts, missed calls, etc.) from Android's OBEX server from windows with Bluecove. I tried the following code from windows, but it returns OBEX_HTTP_NOT_ACCEPTABLE when connecting. Device address and channel are correct as I can tell since when running this test, android pops up a dialog asking if I allow the other device to access it's contacs. Thanks!

code:

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

回答1:


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

the header should be set a target with the value pbap_target byte array.




回答2:


"clientSession" connect need a target header.



来源:https://stackoverflow.com/questions/11179526/access-androids-obex-server-and-read-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!