📄 server.java
字号:
/**
* @author Trojan
*
*6��l�ӵ�Server����
*/
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.bluetooth.BluetoothStateException;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.LocalDevice;
import javax.bluetooth.UUID;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import javax.microedition.io.StreamConnectionNotifier;
public class Server implements Runnable {
private static final UUID DANCE_LIFE_UUID = new UUID(
"F0E0D0C0B0A000908070605040302010", false);// 6�7�������Ե�Ψһ��ʶ����
private LocalDevice localDevice = null;// ����6���豸�Ķ���
private StreamConnectionNotifier notifier = null;// 6��l�Ӻ�6�7���Ķ���
boolean isBTReady = false;// ��־6���豸�Ƿ����õ�
private StreamConnection conn = null;// 6��ͨ��t�ӵĶ���
private DataInputStream dis;// ���������
private DataOutputStream dos;// ��������
private String sendText = null;// ���͵��ַ�
private String reciveText = null;// ���յ��ַ�]
/**
* ���췽�������߳�
*/
public Server() {
new Thread(this).start();
}
/**
* ����������6���豸��ȡ��6��l�ӣ������������
*/
public void run() {
try {
localDevice = LocalDevice.getLocalDevice();
if (!localDevice.setDiscoverable(DiscoveryAgent.GIAC)) {
//����6��ģʽ�����ɹ������
return;
}
//6��l�ӵ�URL
StringBuffer url = new StringBuffer("btspp://");
url.append("localhost").append(':');
url.append(DANCE_LIFE_UUID.toString());
url.append(";name=danceLifeServer");
url.append(";authorize=false");
//���6��l��
notifier = (StreamConnectionNotifier) Connector
.open(url.toString());
localDevice.getRecord(notifier);
conn = notifier.acceptAndOpen();
dis = conn.openDataInputStream();
dos = conn.openDataOutputStream();
isBTReady = true;
} catch (BluetoothStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* �رշ������ر�6��l�Ӻ����������
*/
public void close() {
try {
if (dis != null) {
dis.close();
}
if (dos != null) {
dos.close();
}
if (conn != null) {
conn.close();
}
if (notifier != null) {
notifier.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 6��ͨ���з�����ݵķ���
*
* @throws IOException
*/
public void send() throws IOException {
if (isBTReady == true && sendText != null) {
dos.writeUTF(sendText);
}
}
/**
* 6��ͨ���н�����ݵķ���
*
* @return
* @throws IOException
*/
public String receive() throws IOException {
if (isBTReady) {
reciveText = dis.readUTF();
}
return reciveText;
}
/**
* ���ô�����ݵķ���
*
* @param sendText
*/
public void setSendText(String sendText) {
this.sendText = sendText;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -