?? client.java
字號:
import java.util.*;
import javax.bluetooth.*;
public class Client implements Runnable,DiscoveryListener{
private PCanvas canvas;
private Thread thread;
private DiscoveryAgent discoveryagent;//發現服務代理
// 響應服務的UUID
private static final UUID ECHO_SERVER_UUID=new UUID("F0E0D0C0B0A000908070605040302010", false);
private static final int inquiryAccessCode = DiscoveryAgent.GIAC;
private Vector transIDs=new Vector();//服務搜索事務ID的集合
private Vector unsearchedRemoteDevices=new Vector();
private int numServiceSearchesInProgress=0;
private boolean inquiryInProgress=false;
private Hashtable serviceRecord=new Hashtable();
private Vector add=new Vector();
private ClientProcessor newHandler;
// -----------------------------構造函數---------------------------
public Client(PCanvas canvas){
this.canvas=canvas;
try{
LocalDevice localdevice=LocalDevice.getLocalDevice();
discoveryagent=localdevice.getDiscoveryAgent();
localdevice.setDiscoverable(DiscoveryAgent.NOT_DISCOVERABLE);
}catch(BluetoothStateException e){}
start();
}
public synchronized void start() {
thread = new Thread(this);
thread.start();
}
public void run(){
Thread currentThread = Thread.currentThread();
running:
while (thread == currentThread)
{
synchronized (this)
{
if (thread != currentThread)
{
break running;
}
else if (!inquiryInProgress)
{
doServiceSearch();
}
try
{
wait(500);
}
catch (InterruptedException e)
{
// we can safely ignore this
}
}
}
}
// -----------------------------查找主機---------------------------
public void doServiceSearch()
{
if (unsearchedRemoteDevices.size() > 0)
{
synchronized(this)
{
RemoteDevice device =(RemoteDevice) unsearchedRemoteDevices .elementAt(0);
UUID[] uuids = new UUID[1];
uuids[0] = ECHO_SERVER_UUID;
try
{
int[] attrSet = null; // default attrSet
numServiceSearchesInProgress++;
int transId = discoveryagent.searchServices(attrSet, uuids,device, this);
transIDs.addElement(new Integer(transId));
unsearchedRemoteDevices.removeElementAt(0);
}
catch (BluetoothStateException e)
{
numServiceSearchesInProgress--;
}
}
}
}
// -----------------------------喚醒設備查找---------------------------
public void inquiryCompleted(int discType) {
// TODO 自動生成方法存根
inquiryInProgress=false;
}
// -----------------------------喚醒服務查找---------------------------
public void serviceSearchCompleted(int transId, int respCode) {
// TODO 自動生成方法存根
// remove the transaction id from the pending list
for (int i=0; i < transIDs.size(); i++)
{
Integer pendingId = (Integer) transIDs.elementAt(i);
if (pendingId.intValue() == transId)
{
transIDs.removeElement(pendingId);
break;
}
}
}
// -----------------------------服務查找---------------------------
public void servicesDiscovered(int transID, ServiceRecord[] serviceRecords) {
// TODO 自動生成方法存根
if (serviceRecords.length == 1)
{
RemoteDevice device = serviceRecords[0].getHostDevice();
String name = device.getBluetoothAddress();
if (!serviceRecord.containsKey(name))
{
serviceRecord.put(name, serviceRecords[0]);
add.addElement(name);
}
}
}
// -----------------------------設備查找---------------------------
public void deviceDiscovered(RemoteDevice remoteDevice, DeviceClass deviceClass) {
// TODO 自動生成方法存根
boolean isPhone =(deviceClass.getMajorDeviceClass() == 0x200);
// Setting the following line to 'true' is a workaround
// for some early beta SDK device emulators. Set it
// to false when compiling the MIDlet for download to
// real MIDP phones!
boolean isEmulator = true; //false;
if (isPhone || isEmulator)
{
unsearchedRemoteDevices.addElement(remoteDevice);
}
}
//取得地址
public String getAdd(int i){
return add.elementAt(i).toString();
}
//取得服務地址數
public int getAddsize(){
return add.size();
}
//開始查找服務
public void dosearch(){
// remove old device lists
add.removeAllElements();
unsearchedRemoteDevices.removeAllElements();
for (Enumeration keys = serviceRecord.keys();
keys.hasMoreElements();)
{
serviceRecord.remove(keys.nextElement());
}
try
{
// disable page scan and inquiry scan
LocalDevice dev = LocalDevice.getLocalDevice();
dev.setDiscoverable(DiscoveryAgent.NOT_DISCOVERABLE);
//startActivityIndicator();
// this is non-blocking
discoveryagent.startInquiry(inquiryAccessCode, this);
inquiryInProgress=true;
}
catch (BluetoothStateException e) {}
}
//建立連接
public void makeConn(int n){
ServiceRecord ServiceRecord =(ServiceRecord) serviceRecord.get(add.elementAt(n));
newHandler =new ClientProcessor(this,ServiceRecord);
newHandler.start(); // start reader & writer
}
public void read(byte[] temp){
canvas.read(temp);
}
public void send(String data){
newHandler.send(data);
//println(data);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -