Как отправить файл из приложения J2me с помощью bluetooth

Я пытаюсь отправить файл из приложения j2me на все доступные устройства Bluetooth в диапазоне, близком к этому.

Я искал устройства в диапазоне и отображался, но я не смог отправить файл на это устройство. Я получаю код ответа как 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 ответ

убедитесь, что все устройства находятся в зоне действия и поддерживают соединения, и попробуйте выполнить код ниже

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();
 }
}

licensed under cc by-sa 3.0 with attribution.