0

我正在研究使用Java通过蓝牙将文件发送到我的手机的项目。但是当我运行代码时,它会引发异常:

java.io.IOException: Session not connected
at com.intel.bluetooth.obex.OBEXClientSessionImpl.canStartOperation(OBEXClientSessionImpl.java:166)
at com.intel.bluetooth.obex.OBEXClientSessionImpl.put(OBEXClientSessionImpl.java:212)
at com.practise.FileTransfer.run(FileTransfer.java:39)
at java.lang.Thread.run(Thread.java:724)

这是代码:

package com.practise;

import java.io.File;
import java.io.FileInputStream;
import java.util.Vector;

import javax.bluetooth.DeviceClass;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.DiscoveryListener;
import javax.bluetooth.LocalDevice;
import javax.bluetooth.RemoteDevice;
import javax.bluetooth.ServiceRecord;
import javax.bluetooth.UUID;
import javax.swing.JFileChooser;

class BlueApplication implements DiscoveryListener {

LocalDevice local;
DiscoveryAgent agent;
Vector<RemoteDevice> devices = new Vector<RemoteDevice>();
Vector<ServiceRecord> services = new Vector<ServiceRecord>();
Object lock = new Object();
JFileChooser jfcFile = new JFileChooser();

BlueApplication() {
    try {

        local = LocalDevice.getLocalDevice();
        System.out.println("Local Address :- " + local.getBluetoothAddress() +"\nName :- " + local.getFriendlyName());

        agent = local.getDiscoveryAgent();

        agent.startInquiry(DiscoveryAgent.GIAC, this);

        synchronized (lock) {
            lock.wait();
        }

        if(!devices.isEmpty()) {
            for(RemoteDevice rd : devices) {
                System.out.println("Device :- " + rd.getBluetoothAddress() + "(" + rd.getFriendlyName(true) + ")");
            }

            System.out.println("Searching for service ...");

            UUID[] uid = new UUID[1];
            uid[0] = new UUID("1106", true);

            agent.searchServices(null, uid, devices.get(0), this);

            synchronized(lock) {
                lock.wait();
            }

            if(!services.isEmpty()) {
                for(ServiceRecord sr : services) {
                    System.out.println("Service :- " + sr.getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false));
                }

                String url = services.get(0).getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);

                int option = jfcFile.showOpenDialog(null);

                if(option == JFileChooser.APPROVE_OPTION) {

                    File file = jfcFile.getSelectedFile();

                    byte[] data = new byte[(int)file.length()];

                    FileInputStream fs = new FileInputStream(file);
                    fs.read(data);
                    fs.close();
                    FileTransfer ft = new FileTransfer(url, file, data);
                    Thread t = new Thread(ft);
                    t.start();
                }

            } else {
                System.out.println("Not support");
            }
        } else {
            System.out.println("No device found");
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
}

@Override
public void deviceDiscovered(RemoteDevice remDevice, DeviceClass devClass) {
    devices.add(remDevice);
}

@Override
public void inquiryCompleted(int respCode) {
    synchronized (lock) {
        lock.notify();
    }

    switch(respCode) {
    case DiscoveryListener.INQUIRY_COMPLETED:
        System.out.println("Inquiry completed");
        break;
    case DiscoveryListener.INQUIRY_ERROR:
        System.out.println("Inquiry error");
        break;
    case DiscoveryListener.INQUIRY_TERMINATED:
        System.out.println("Inquiry terminated");
        break;
    default:
        System.out.println("Invalid code");

    }
}

@Override
public void serviceSearchCompleted(int transID, int respCode) {
    synchronized(lock) {
        lock.notify();
    }

    switch(respCode) {
    case DiscoveryListener.SERVICE_SEARCH_COMPLETED:
        System.out.println("Service search completed");
        break;
    case DiscoveryListener.SERVICE_SEARCH_DEVICE_NOT_REACHABLE:
        System.out.println("Service search device not reachable");
        break;
    case DiscoveryListener.SERVICE_SEARCH_ERROR:
        System.out.println("Service search error");
        break;
    case DiscoveryListener.SERVICE_SEARCH_NO_RECORDS:
        System.out.println("Service serach no records");
        break;
    case DiscoveryListener.SERVICE_SEARCH_TERMINATED:
        System.out.println("Service search terminated");
        break;
    default:
        System.out.println("Invalid service response code");
    }
}

@Override
public void servicesDiscovered(int transID, ServiceRecord[] srvRecord) {

    for(ServiceRecord sr : srvRecord) {
        services.add(sr);
    }
}

public static void main(String[] args) {
    new BlueApplication();
}
}

package com.practise;

import java.io.File;
import java.io.OutputStream;
import java.nio.file.Files;

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

public class FileTransfer implements Runnable{

File file;
String url;
byte[] data;

public FileTransfer(String url, File file, byte[] data) {
    this.url = url;
    this.file = file;
    this.data = data;

}

public void run() {
    try {

        Connection con = Connector.open(url, Connector.WRITE);
        System.out.println("Connnectttt");

        ClientSession cs = (ClientSession) con;
        HeaderSet hs = cs.createHeaderSet();

        hs.setHeader(HeaderSet.NAME, file.getName());
        hs.setHeader(HeaderSet.TYPE, Files.probeContentType(file.toPath()));
        hs.setHeader(HeaderSet.LENGTH, file.length());

        Operation opr = cs.put(hs);

        OutputStream os = opr.openOutputStream();
        os.write(data);

        os.close();
        opr.close();
        cs.disconnect(null);
        con.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
  }
}

我正在使用 ubuntu 13.04 OS(Linux) & bluecove implementation & bluetooth dongle。任何想法为什么会发生这种情况。

4

0 回答 0