⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 client.java

📁 一款模仿劲舞团的手机游戏
💻 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 + -