?? client.java
字號:
/**
* @author Trojan
*
*6??l???Client??
*/
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Vector;
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;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
public class Client implements Runnable, DiscoveryListener {
private DiscoveryAgent discoveryAgent;// 6???豸???????
private StreamConnection conn = null;// 6??l?????
private DataInputStream dis = null;// ?????????
private DataOutputStream dos = null;// ????????
private UUID[] uuidSet;// ????????UUID????
public boolean isBTReady = false;// ???6???豸???t???
private static final UUID DANCE_LIFE_UUID = new UUID(
"F0E0D0C0B0A000908070605040302010", false);// 6?7?????????Ψ????????
private Vector deviceRecords = new Vector();// ????????6???豸????
private Hashtable hashtable = new Hashtable();// ????????6???豸???
private Vector serviceRecords = new Vector();// ????????6?7???
private String[] deviceName;// 6???豸????????
private String sendText = "ready";// ????????
private String reciveText = null;// ????????
private String connDeviceName = null;// ???t????豸??
/**
* ??????,?????
*
*/
public Client() {
new Thread(this).start();
}
/**
* ???6???豸?????6??l??????????????????
*/
public synchronized void run() {
try {
// ????豸?????
LocalDevice localDevice = LocalDevice.getLocalDevice();
discoveryAgent = localDevice.getDiscoveryAgent();
isBTReady = true;
} catch (Exception e) {
e.printStackTrace();
}
if (!isBTReady)
return;
uuidSet = new UUID[2];
uuidSet[0] = new UUID(0x1101);
uuidSet[1] = DANCE_LIFE_UUID;
try {
// ??????????豸????
discoveryAgent.startInquiry(DiscoveryAgent.GIAC, this);
} catch (BluetoothStateException e) {
}
try {
wait();
} catch (InterruptedException e1) {
e1.printStackTrace();
}
deviceName = new String[deviceRecords.size()];
for (int i = 0; i < deviceRecords.size(); i++) {
try {
RemoteDevice remoteDevice = (RemoteDevice) deviceRecords
.elementAt(i);
deviceName[i] = remoteDevice.getFriendlyName(false);
hashtable.put(deviceName[i], remoteDevice);
} catch (IOException e) {
e.printStackTrace();
}
}
try {
wait();
} catch (InterruptedException e2) {
e2.printStackTrace();
}
RemoteDevice remoteDevice = (RemoteDevice) hashtable
.get(connDeviceName);
try {
// ??????豸???з???????
discoveryAgent.searchServices(null, uuidSet, remoteDevice, this);
} catch (BluetoothStateException e) {
e.printStackTrace();
}
try {
wait();
} catch (InterruptedException e1) {
e1.printStackTrace();
}
for (int k = 0; k < serviceRecords.size(); k++) {
ServiceRecord serviceRecord = (ServiceRecord) serviceRecords
.elementAt(k);
// ???l???URL
String url = serviceRecord.getConnectionURL(
ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
try {
// ??l????????????
if (conn == null) {
conn = (StreamConnection) Connector.open(url);
}
if (conn != null) {
dis = conn.openDataInputStream();
dos = conn.openDataOutputStream();
send();
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* ????6???豸?????????浽deviceRecords??
*/
public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
if (deviceRecords.indexOf(btDevice) == -1) {
deviceRecords.addElement(btDevice);
}
}
/**
* ?????????
*
*/
public void wakeUp() {
synchronized (this) {
notify();
}
}
/**
* ???l????????????????
*
*/
public void close() {
try {
if (dis != null) {
dis.close();
}
if (dos != null) {
dos.close();
}
if (conn != null) {
conn.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* ?豸???????
*/
public void inquiryCompleted(int discType) {
synchronized (this) {
notify();
}
}
/**
* ????6?7??
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -