gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 02:12:21 26 4
gpt4 key购买 nike

我正在尝试将文件从 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()));
}
}

约瑟夫·拉吉

最佳答案

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

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

关于java-me - 如何使用蓝牙从 J2me 应用程序发送文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12203110/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com