Old school Easter eggs.
↓↓xuống cuối trang↓↓
Chào mừng bạn đến với wap DinhLoi.XtGem.Com | Chúc bạn có 1 năm mới vui vẻ tốt lành hạnh phúc! Hãy giới thiệu website này cho bạn bè nhé!img
DinhLoi.XtGem.Com =>> CaFe9x.Gq
Bạn đến từ quốc gia:



Hôm nay ngày
21.11.24/12:12
img- Hi. Xin chào! Mozilla/5.0
logo
.
HOMECHÁTTruyệnFORUM
Trang chủ>Thủ thuật>Xử lí Java>
Bluetooth in j2me api75 part 0.01
15.07.2014/13:58
Bluetooth là gì? Những thiết bị nào hỗ trợ bluetooth? Sử dụng bluetooth trong lập trình j2me như thế nào?
Bluetooth nằm trong jsr 82. File connection mới nằm trong jsr 75
Tạo một kết nối bluetooth đơn giản! BluetoothDemo.java [MIDlet]
import javax.microedition.midlet.MIDlet; import javax.microedition.midlet.MIDletStateChangeException; import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Displayable; import javax.microedition.lcdui.Command; import javax.microedition.lcdui.CommandListener; import javax.microedition.lcdui.List; import javax.microedition.lcdui.Choice; import javax.bluetooth.UUID; publicfinalclassBluetoothDemoextendsMIDletimplementsCommandListener {publicstaticfinal UUID RFCOMM_UUID = new UUID(0x0003); privatestaticfinal String SELECT = "Select"; privatestaticfinal String EXIT = "Exit"; privatefinal Command CMD_EXIT = new Command(EXIT,Command.EXIT, 1); privatefinal Command CMD_SLECT = new Command(SELECT, Command.SCREEN,2); privatestaticfinal String[] MENULABLES = {"Server","Client" }; privatefinal List menu =new List("Bluetooth Echo Demo", Choice.IMPLICIT,MENULABLES,null); private Display display= null; public BluetoothDemo() { menu.addCommand(CMD_EXIT); menu.addCommand(CMD_SLECT); menu.setCommandListener(this); } publicvoid startApp() { display =Display.getDisplay(this); display.setCurrent(menu); } protectedvoid destroyApp(boolean unconditional) { } protectedvoid pauseApp(){ } publicvoid commandAction(Command cmd, Displayable d) { if (cmd == CMD_EXIT) { destroyApp(true); notifyDestroyed(); return; }elseif (cmd == CMD_SLECT) { switch(menu.getSelectedIndex()) { case0: new Server(display); break; case1: new Client(display); break; }}}}
bluetooth tương tự như kết nối mạng!
Server.java
import javax.microedition.lcdui.Display; import javax.microedition.lcdui.TextBox; import javax.microedition.lcdui.TextField; import javax.bluetooth.LocalDevice; import javax.bluetooth.DiscoveryAgent; import javax.bluetooth.BluetoothStateException; import javax.microedition.io.Connector; import javax.microedition.io.StreamConnection; import javax.microedition.io.StreamConnectionNotifier; import java.io.InputStream; import java.io.OutputStream; import java.io.IOException; publicclassServerimplementsRunnable{private StreamConnectionNotifier notifier; private StreamConnection conn; private LocalDevice localDevice; privateboolean isInit; privatestaticfinal String SERVERURL = "btspp:/"+"/localhost:"+BluetoothDemo.RFCOMM_UUID; name=rfcommtest; authorize=false; private Display display =null; privatefinal Form form =new Form("Server..."); privatestaticfinal String SERVERMSG = "nHello Form Server..."; public Server(Display dis) { display = dis; display.setCurrent(form); isInit = false; Thread thread = new Thread(this); thread.start(); } publicvoid run() { if (!isInit) { try { conn = null; form.append("Searching for Client..."); localDevice =LocalDevice.getLocalDevice(); localDevice.setDiscoverable(DiscoveryAgent.GIAC); notifier =(StreamConnectionNotifier)Connector.open(SERVERURL); } catch(BluetoothStateException e) { form.append("BluetoothStateException: "+e.getMessage()); } catch(IOException e) { form.append("IOException: " +e.getMessage()); } isInit = true; } try { conn =notifier.acceptAndOpen(); OutputStream output =conn.openOutputStream(); output.write(SERVERMSG.length()); output.write(SERVERMSG.getBytes()); output.close(); InputStream inputStream= conn.openInputStream(); int length =inputStream.read(); byte[] data = null; data = newbyte[length]; length = 0; while (length !=data.length) { int ch = inputStream.read(data, length,data.length -length); if (ch == -1) { thrownew IOException("Can't read data"); } length += ch; } String msg = new String(data); form.append(msg); inputStream.close(); conn.close(); notifier.close(); } catch (Exception ex) { form.append("Bluetooth Server Running Error: " +ex); }}}
Client.java
import javax.microedition.lcdui.Display; import javax.microedition.lcdui.TextBox; import javax.microedition.lcdui.TextField; import javax.microedition.lcdui.Alert; import javax.microedition.lcdui.AlertType; import javax.bluetooth.DiscoveryListener; import javax.bluetooth.DeviceClass; import javax.bluetooth.DiscoveryAgent; import javax.bluetooth.RemoteDevice; import javax.bluetooth.UUID; import javax.bluetooth.LocalDevice; import javax.bluetooth.ServiceRecord; import javax.microedition.io.StreamConnection; import javax.microedition.io.Connector; import java.io.OutputStream; import java.io.InputStream; import java.io.IOException; publicclassClientimplementsDiscoveryListener {private DiscoveryAgent discoveryAgent; private UUID[] uuidSet; private String serviceUrl; private Display display =null; private StreamConnection conn = null; privatefinal Form form =new Form("Client..."); privatestaticfinal String CLIENTMSG= "nHello From Client.."; public Client(Display dis){ display = dis; display.setCurrent(form); try { LocalDevice localDevice =LocalDevice.getLocalDevice(); discoveryAgent =localDevice.getDiscoveryAgent(); discoveryAgent.startInquiry(DiscoveryAgent.GIAC,this); form.append("nstart Inquiry..."); form.append("nSearching for device..."); } catch (Exception e) { form.append("exception"+e); } } publicvoid deviceDiscovered(RemoteDevice btDevice,DeviceClass cod) { try { form.append("nDevice Discovered"); form.append("nMajor Device Class: " +cod.getMajorDeviceClass() +"Minor Device Class: "+cod.getMinorDeviceClass()); form.append("nBluetooth Address: " +btDevice.getBluetoothAddress()); form.append("nBluetooth Friendly Name: " +btDevice.getFriendlyName(true)); uuidSet = new UUID[1]; uuidSet[0] =BluetoothDemo.RFCOMM_UUID; int searchID =discoveryAgent.searchServices(null,uuidSet,btDevice,this); } catch (Exception e) { form.append("nDevice Discovered Error: " + e); } } publicvoid inquiryCompleted(int discType) { form.append("nInquiryCompleted"); } publicvoid servicesDiscovered(int transID, ServiceRecord[] servRecord) { form.append("nServicesDiscovered"); for(int i=0;i
Tag:
Bạn đến từ:
Share: img img img img img

Thanks To:XtGem
Liên kết:U-ON