obextest.java
来自「GPS Track connects to a GPS and records 」· Java 代码 · 共 184 行
JAVA
184 行
/* * Created on 20.01.2005 * * TODO To change the template for this generated file go to * Window - Preferences - Java - Code Style - Code Templates */package de.avetana.obexsolo;import java.io.IOException;import de.avetana.javax.obex.*;import javax.microedition.lcdui.*;import javax.microedition.midlet.MIDlet;import javax.microedition.midlet.MIDletStateChangeException;import de.avetana.javax.obex.Authenticator;/** * @author gmelin * * */public class OBEXTest extends MIDlet implements CommandListener { private TextField adrTextField; private TextField statusField; private Form commandForm; SessionNotifier notify = null; private Command put; private Command offer; public OBEXTest() { // 6600 000E6D7057F9:9 // bolero 000d9305170e:4 adrTextField = new TextField ("Address", "btgoep://000d9305170e:4", 30, TextField.URL); statusField = new TextField ("Status", "ready", 30, TextField.ANY); put = new Command ("put", Command.SCREEN, 1); offer = new Command ("offer", Command.SCREEN, 2); commandForm = new Form ("OBEX Example"); commandForm.addCommand (put); commandForm.addCommand (offer); commandForm.append(adrTextField); commandForm.append(statusField); commandForm.setCommandListener(this); } /* (non-Javadoc) * @see javax.microedition.midlet.MIDlet#startApp() */ protected void startApp() throws MIDletStateChangeException { Display d = Display.getDisplay(this); d.setCurrent(commandForm); } /* (non-Javadoc) * @see javax.microedition.midlet.MIDlet#pauseApp() */ protected void pauseApp() { } /* (non-Javadoc) * @see javax.microedition.midlet.MIDlet#destroyApp(boolean) */ protected void destroyApp(boolean arg0) throws MIDletStateChangeException { if (notify != null) try { notify.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /* (non-Javadoc) * @see javax.microedition.lcdui.CommandListener#commandAction(javax.microedition.lcdui.Command, javax.microedition.lcdui.Displayable) */ public void commandAction(Command arg0, Displayable arg1) { if (arg0 == put) { try { statusField.setString("connecting"); String adr = adrTextField.getString(); statusField.setString("adr " + adr); ClientSession cs = (ClientSession) OBEXConnector.open (adr); statusField.setString("connected"); HeaderSet hs = cs.connect(cs.createHeaderSet()); statusField.setString("created header set " + hs.getResponseCode()); byte text[] = "Test Message from avetanaBlueooth".getBytes("iso-8859-1"); hs.setHeader (HeaderSet.NAME, "test.txt"); hs.setHeader (HeaderSet.TYPE, "text"); //hs.setHeader(0x49, text); // if everything fits inside a packet, the data can be packed in the PUT command statusField.setString("putting"); Operation po = cs.put(hs); statusField.setString("put "); po.openOutputStream().write(text); po.close(); cs.disconnect(null); cs.close(); statusField.setString("closed"); } catch (Throwable e) { statusField.setString(statusField.getString() + " -> error : " + e.getMessage()); } } else if (arg0 == offer) { try { if (notify != null) notify.close(); notify = (SessionNotifier) OBEXConnector.open("btgoep://localhost:00112233445566778899aabbccddeeff;name=OBEXTest;authenticate=false;master=false;encrypt=false"); statusField.setString ("opened"); ((SessionNotifier)notify).acceptAndOpen(new ServerRequestHandler() { public int onConnect (HeaderSet request, HeaderSet response) { statusField.setString ("RequestHandler got connect"); return 0xa0; } public int onPut (Operation op) { statusField.setString ("RequestHandler got put"); synchronized (this) { try { wait (1000); } catch (InterruptedException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } try { statusField.setString ("name " + op.getReceivedHeaders().getHeader(HeaderSet.NAME)); synchronized (this) { try { wait (1000); } catch (InterruptedException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } statusField.setString(" type " + op.getType()); synchronized (this) { try { wait (1000); } catch (InterruptedException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } } catch (Exception e) { statusField.setString(e.getMessage()); } return 0xa0; } public void onDisconnect (HeaderSet req, HeaderSet resp) { statusField.setString ("RequestHandler got disconnect"); } public int onGet (Operation op) { try { statusField.setString ("RequestHandler got get " + op.getReceivedHeaders().getHeaderList().length); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } try { op.openOutputStream().write ("Test Message from avetanaBluetooth".getBytes()); HeaderSet set = op.getReceivedHeaders(); set.setHeader(HeaderSet.TYPE, "text/plain"); set.setHeader(HeaderSet.NAME, "msg.txt"); op.sendHeaders (set); } catch (IOException e) { e.printStackTrace(); return 0xc5; } return 0xa0; } }); } catch (Exception e) { statusField.setString(statusField.getString() + " -> error : " + e.getMessage()); } } } }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?