java怎么用蓝牙传_java-me蓝牙文件发送

package hello;

import java.io.*;

import java.util.Vector;

import javax.bluetooth.*;

import javax.microedition.io.*;

import javax.microedition.io.StreamConnection.*;

import javax.microedition.lcdui.*;

import javax.microedition.midlet.MIDlet;

import javax.obex.*;

import javax.obex.ResponseCodes;

public class MyMidlet extends MIDlet implements CommandListener, DiscoveryListener

{

public Command cmdSend;

public Command cmdScan;

public TextBox myText;

public List devList;

public Form myForm;

private LocalDevice localDev;

private DiscoveryAgent dAgent;

private ServiceRecord servRecord;

private Vector myVector;

private ClientSession connection = null;

private String url = null;

private Operation op = null;

private boolean cancelInvoked = false;

public MyMidlet()

{

cmdSend = new Command("Send", 2, 0);

cmdScan = new Command("Scan", 5, 0);

}

public void startApp()

{

if(myText == null)

{

myText = new TextBox("Dummy Text", "Hello", 10, 0);

myText.addCommand(cmdScan);

myText.setCommandListener(this);

Display.getDisplay(this).setCurrent(myText);

}

}

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(0x9e8b33);

dAgent = localDev.getDiscoveryAgent();

}

dAgent.startInquiry(0x9e8b33, this);

}

catch(BluetoothStateException bluetoothstateexception)

{

myForm.append("Please check your bluetooth is turn-on");

}

}

if(command == cmdSend)

{

myForm.setTitle("Sending");

for(int k = 0; k < myForm.size(); k++) myForm.delete(k);

myForm.append("Sending application..");

Display.getDisplay(this).setCurrent(myForm);

try

{

RemoteDevice remotedevice = (RemoteDevice)myVector.elementAt(devList.getSelectedIndex());

dAgent.searchServices(null, new UUID[] {new UUID(4358L)}, remotedevice, this);

return;

}

catch(BluetoothStateException bluetoothstateexception1)

{

myForm.append("could not open bluetooth: " + bluetoothstateexception1.toString());

}

}

}

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 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

{

byte[] fileContent = "Raxit Sheth -98922 38248".getBytes();

String s=servRecord.getConnectionURL(0, false);

myForm.append("Debug 0");

connection = (ClientSession) Connector.open(s);

myForm.append("Debug1");

HeaderSet headerSet = connection.connect(null);

myForm.append("Debug1.1");

headerSet.setHeader(HeaderSet.NAME, "a.txt");

headerSet.setHeader(HeaderSet.TYPE, "text/plain");

headerSet.setHeader(HeaderSet.LENGTH, new Long(fileContent.length));

myForm.append("Debug1.2");

//op = connection.put(headerSet); throwing java.lang.IllegalArgument.Exception

op = connection.put(null);

myForm.append("Debug1.2.1");

op.sendHeaders(headerSet);

myForm.append("Debug1.3");

OutputStream out = op.openOutputStream();

myForm.append("Debug2");

//sending data

myForm.append("Debug3");

out.write(fileContent);

myForm.append("Debug4");

//int responseCode = op.getResponseCode();

//myForm.append("resp code="+responseCode);

out.close();

op.close();

connection.close();

myForm.append("Done");

//i was expecting this will send a.txt file with content Raxit Sheth -98922 38248

//to remote device's inbox/gallery/bluetooth folder

}

catch(Exception ex) { myForm.append(ex.toString()); }

}

public void inquiryCompleted(int i)

{

Display.getDisplay(this).setCurrent(devList);

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值