📄 client.java
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */import javax.microedition.lcdui.*;import java.util.Vector;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.IOException;import javax.microedition.io.Connector;import javax.microedition.io.StreamConnection;import javax.bluetooth.BluetoothStateException;import javax.bluetooth.DeviceClass;import javax.bluetooth.DiscoveryAgent;import javax.bluetooth.DiscoveryListener;import javax.bluetooth.LocalDevice;import javax.bluetooth.RemoteDevice;import javax.bluetooth.ServiceRecord;import javax.bluetooth.UUID;public class Client implements Runnable, DiscoveryListener { private Connection connection; private DiscoveryAgent discoveryAgent; private static final UUID ECHO_SERVER_UUID = new UUID( "F0E0D0C0B0A000908070605040302010", false); Vector devices = new Vector(); Vector records = new Vector(); String messagex; String message_1; //boolean isConnection = false; StreamConnection conn; int[] transIDs; private Thread processorThread; private boolean isClosed = false; private int discType; //i add DataOutputStream dos=null; DataInputStream dis = null; public Client(Connection connection) { this.connection = connection; } public void start() { processorThread = new Thread(this); processorThread.setPriority(9); processorThread.start(); } public void sendMessage(String message) { this.message_1 = message; if(records.size() == 0)return; try { ServiceRecord sr = (ServiceRecord) records.elementAt(0); String url = sr.getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false); conn = (StreamConnection) Connector.open(url); if(conn != null) is(true); dos = conn.openDataOutputStream(); dos.writeUTF(message_1); dos.flush(); dis = conn.openDataInputStream(); messagex = dis.readUTF(); connection.receiveMessage(messagex); } catch (Exception e) { e.printStackTrace(); } } public void is(boolean isConnection) { connection.isConnection(isConnection); } public void run() { try { LocalDevice local = LocalDevice.getLocalDevice(); discoveryAgent = local.getDiscoveryAgent(); } catch (BluetoothStateException e) { System.err.println("Can't initialize bluetooth: " + e); } try { discoveryAgent.startInquiry(DiscoveryAgent.GIAC, this); } catch (BluetoothStateException bse) { } synchronized (this) { try { wait(); } catch (InterruptedException e) { System.err.println("Unexpected interuption: " + e); return; } } if (isClosed) { return; } UUID[] uuids = new UUID[1]; uuids[0] = ECHO_SERVER_UUID; transIDs = new int[devices.size()]; if (devices.size() != 0) { for (int i = 0; i < devices.size(); i++) { RemoteDevice rd = (RemoteDevice) devices.elementAt(i); try { transIDs[i] = discoveryAgent.searchServices(null, uuids, rd, this); } catch (BluetoothStateException e) { continue; } } } synchronized (this) { try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } public void deviceDiscovered(RemoteDevice remoteDevice, DeviceClass deviceClass) { devices.addElement(remoteDevice);// System.out.println("discovered a device ");// System.out.println(remoteDevice.getBluetoothAddress()); } public void inquiryCompleted(int discType) { // System.out.println("start inquiry completed"); this.discType = discType; if (discType != INQUIRY_COMPLETED || devices.size() == 0) { // connection.completeInitialization(false); } synchronized(this){ notify(); } } public void serviceSearchCompleted(int transID, int respCode) { //System.out.println("complete service Search"); switch (respCode) { case DiscoveryListener.SERVICE_SEARCH_COMPLETED: break; case DiscoveryListener.SERVICE_SEARCH_DEVICE_NOT_REACHABLE: break; case DiscoveryListener.SERVICE_SEARCH_ERROR: break; case DiscoveryListener.SERVICE_SEARCH_NO_RECORDS: break; case DiscoveryListener.SERVICE_SEARCH_TERMINATED: break; } synchronized (this) { notify(); } } public void servicesDiscovered(int transID, ServiceRecord[] serviceRecord) { //System.out.println("get server"); for (int i = 0; i < serviceRecord.length; i++) { // System.out.println("添加了records"); records.addElement(serviceRecord[i]); } } public void close() { isClosed = true; synchronized(this){ notify(); } //等本线程运行完 try { processorThread.join(); } catch (InterruptedException e) { e.printStackTrace(); } //关闭流 try{ if(dos!=null)this.dos.close(); if(dis!=null)this.dis.close(); }catch(IOException e){ e.printStackTrace(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -