如何使用蓝牙从J2me应用程序发送文件

时间:2012-08-30 18:21:22

标签: java-me bluetooth file-transfer

我正在尝试将文件从j2me应用程序发送到附近范围内的所有可用蓝牙设备。

我搜索范围内的设备并显示但我无法将文件发送到该设备。我得到响应代码为211,同时获得连接响应。

我添加了我的代码:

private void SendingFile() {
    myForm.setTitle("Sending..");
    for (int k = 0; k < myForm.size(); k++) {
        myForm.delete(k);
    }
    myForm.append("Sending application..");
    display.setCurrent(myForm);
    try {
        RemoteDevice remotedevice = (RemoteDevice) myVector.elementAt(devList.getSelectedIndex());
        dAgent.searchServices(null, new UUID[]{new UUID(0x1105)}, remotedevice, this);
        return;
    } catch (BluetoothStateException bluetoothstateexception1) {
        myForm.append("could not open bluetooth: " + bluetoothstateexception1.toString());
    }
}

public void pauseApp() {
}

public void destroyApp(boolean flag) {
}

public void commandAction(Command command, Displayable displayable) {
    if (command == cmdScan) {
        if (myForm == null) {
            myForm = new Form("Scanning");
        } else {
            for (int i = 0; i < myForm.size(); i++) {
                myForm.delete(i);
            }
        }
        myForm.append("Scanning for bluetooth devices..");
        Display.getDisplay(this).setCurrent(myForm);
        if (devList == null) {
            devList = new List("Devices", 3);
            devList.addCommand(cmdSend);
            devList.setCommandListener(this);
        } else {
            for (int j = 0; j < devList.size(); j++) {
                devList.delete(j);
            }
        }
        if (myVector == null) {
            myVector = new Vector();
        } else {
            myVector.removeAllElements();
        }
        try {
            if (localDev == null) {
                localDev = LocalDevice.getLocalDevice();
                localDev.setDiscoverable(DiscoveryAgent.GIAC);
                dAgent = localDev.getDiscoveryAgent();
            }
            dAgent.startInquiry(DiscoveryAgent.GIAC, this);
        } catch (BluetoothStateException bluetoothstateexception) {
            myForm.append("Please check your bluetooth is turn-on");
        }
    }
    if (command == cmdSend) {
        SendingFile();
    }
}

public void deviceDiscovered(RemoteDevice remotedevice, DeviceClass deviceclass) {
    try {
        devList.append(remotedevice.getFriendlyName(false), null);
    } catch (IOException _ex) {
        devList.append(remotedevice.getBluetoothAddress(), null);
    }
    myVector.addElement(remotedevice);
}

public void inquiryCompleted(int i) {
    Display.getDisplay(this).setCurrent(devList);
}

public void servicesDiscovered(int i, ServiceRecord aservicerecord[]) {
    servRecord = aservicerecord[0];

}

public void serviceSearchCompleted(int i, int j) {
    if (j != 1) {
        myForm.append("service search not completed: " + j);
    }
    try {
        startServer();
        myForm.append("Done");
    } catch (Exception ex) {
        myForm.append(ex.toString());
    }
}

private void startServer() {
    if (mServer != null) {
        return;
    }
    mServer = new Thread(this);
    mServer.start();
}

public void run() {
    try {
        boolean conFlag = true, FlagLoop = false;
        myForm.deleteAll();

        while (!FlagLoop) {
            try {
                connection = null;
                String s = servRecord.getConnectionURL(0, false);
                connection = (ClientSession) Connector.open(s);

            } catch (IOException iex) {
                myForm.append("IO Exce Con Create:".concat(iex.toString()));
                continue;
            } catch (Exception e) {
                myForm.append("Exc Con Create:".concat(e.toString()));
                continue;
            }
            CheckConnection();
        }
    } catch (Exception ex) {
    }
}

private void CheckConnection() throws IOException {

    try {

        myForm.deleteAll();
        myForm.append("Conn Check!!!");
        HeaderSet header = connection.connect(null);
        int responseCode = header.getResponseCode();
        myForm.append("responseCode:" + responseCode);
        if (responseCode != ResponseCodes.OBEX_HTTP_OK) {
            throw new IOException();
        }
        myForm.append("After Response Check!!!");
        Pushdata();
    } catch (IOException ie) {
        myForm.append("IO Exc in Checkconnection:".concat(ie.toString()));
    } catch (Exception e) {
        myForm.append("Exc in Checkconnection:".concat(e.toString()));
    }
}

约瑟夫·拉杰

1 个答案:

答案 0 :(得分:2)

确保设备全部在范围内并保持连接并尝试下面的代码

    private void CheckDevice(int indexxx, int size) {
    try {
        for (int i = 0; i < size; i++) {

            DeviceName = devList.getString(i).toString();
            remotedevice = (RemoteDevice) myVector.elementAt(i);
            devAdds = remotedevice.getBluetoothAddress().toString();
            if ((!rmsObj.rmsData.contains(devAdds) && (!Failed_Devices.contains(devAdds)) && (!AllDevices.contains(devAdds)))) {

                SSSendingFile(i);
                return;
            }
        }
    } catch (Exception e) {
        SendingFailed();
    }
}

private void SSSendingFile(int seldeviceindex) {
    myForm.setTitle("Bluetooth Devices Found");
    myForm.deleteAll();
    display.setCurrent(myForm);

    LoadingScreen("Try to Pair with ".concat(DeviceName), "pairing");
    try {
        AllDevices.addElement(devAdds);
        dAgent.searchServices(null, new UUID[]{new UUID(0x1105)}, remotedevice, this);
        return;
    } catch (BluetoothStateException bluetoothstateexception1) {
        myForm.append("Could Not Open Bluetooth: " + bluetoothstateexception1.toString());
        SendingFailed();
    } catch (Exception ex) {
        Date_Purpose("Service Serach EXC:" + DeviceName + " :" + ex.getMessage());
        if (!Devices_Fail_Name.contains(DeviceName.toString()) && (!remotedevice.equals("")) && (remotedevice != null)) {
            Devices_Fail_Name.addElement(DeviceName.toString());
        }
        Failed_Devices.addElement(devAdds);
        SendingFailed();
    }
}