通过蓝牙将文件从PC发送到Android手机

时间:2014-02-13 07:43:17

标签: java android mobile bluetooth

我正在开发一个使用java的桌面应用程序将文件发送到Android设备。我按照这篇文章sending-files-to-mobile-phone-using-bluetooth-and-obex来开发它。我在移动设备,基于java测试它,它工作正常,但在Android移动设备,我得到这个例外:

Exception: Failed to connect;    
[10051] Une opération a été tentée sur un réseau impossible à atteindre.  
javax.bluetooth.BluetoothConnectionException: Failed to connect;  
[10051] Une opération a été tentée sur un réseau impossible à atteindre.  
at com.intel.bluetooth.BluetoothStackMicrosoft.connect(Native Method)  
at com.intel.bluetooth.BluetoothStackMicrosoft.access$700(BluetoothStackMicrosoft.java:44)  
at com.intel.bluetooth.BluetoothStackMicrosoft$ConnectThread.run(BluetoothStackMicrosoft.java:651)

这里有send代码:

public class SendFile implements Runnable {
private String btConnectionURL;
private byte[] file;
private String filename;

public SendFile (String url, byte[] file, String filename) {
    this.btConnectionURL = url;
    this.file = file;
    this.filename = filename;
}

public void run() {

    try {

        Connection connection = Connector.open(btConnectionURL);
        // connection obtained

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



        HeaderSet hs = cs.createHeaderSet();

        // now let's send the connect header
        cs.connect(hs);

        hs.setHeader(HeaderSet.NAME, filename);
        hs.setHeader(HeaderSet.TYPE, "image/jpeg");
        hs.setHeader(HeaderSet.LENGTH, new Long(file.length));

        Operation putOperation = cs.put(hs);

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

        outputStream.close();
        putOperation.close();

        cs.disconnect(null);

        connection.close();
    } catch (Exception e) {
        System.err.println("Exception: " + e.getMessage());
        e.printStackTrace();
    }
}

}

0 个答案:

没有答案
相关问题