📄 btserver.java
字号:
package com.j2medev.chapter8.bt;
import java.io.*;
import javax.bluetooth.*;
import javax.microedition.io.*;
public class BTServer implements Runnable {
public static final String uuidString = "0123456789ABCDEF0123456789ABCDEF";
public static UUID uuid;
private LocalDevice localDevice;// 本地设备实例
String localBTAddress;// 本地蓝牙地址
String localBTName;// 蓝牙名称
BTServerUI ui;
Thread th;
Thread readWorkTh;
Thread writeWorkTh;
StreamConnectionNotifier notifier;
private ServiceRecord record;
StreamConnection conn;
boolean exitFlag;
boolean BTReady;
DataInputStream in;
DataOutputStream out;
String sendText = "";
public BTServer(BTServerUI ui) {
this.ui = ui;
th = new Thread(this);
th.start();
}
public boolean initBT() {
boolean success = false;
try {
uuid = new UUID(uuidString, false);// 我们的UUID
// 取得本地设备实例
localDevice = LocalDevice.getLocalDevice();
// 设置服务器可发现,不成功则返回
if (!localDevice.setDiscoverable(DiscoveryAgent.GIAC)) {
return false;
}
// 记录蓝牙地址
localBTAddress = localDevice.getBluetoothAddress();
// 记录蓝牙名称
localBTName = localDevice.getFriendlyName();
// //取得蓝牙代理
// agent=localDevice.getDiscoveryAgent();
success = true;
} catch (Exception e) {
System.err.println("初始化蓝牙设备失败:" + e);
}
return success;
}
public void run() {
if (!initBT()) {
ui.state.setText("初始化失败");
return;
}
StringBuffer url = new StringBuffer("btspp://");
url.append("localhost").append(':');
url.append(uuid.toString());
url.append(";name=p2pChatServer");
url.append(";authorize=false");
try {
notifier = (StreamConnectionNotifier) Connector
.open(url.toString());
record = localDevice.getRecord(notifier);
ui.state.setText("等待客户端连接");
conn = notifier.acceptAndOpen();
in = conn.openDataInputStream();
out = conn.openDataOutputStream();
readWorkTh = new ReadWorkThread();
readWorkTh.start();
writeWorkTh = new WriteWorkThread();
writeWorkTh.start();
ui.state.setText("就绪");
BTReady = true;
} catch (IOException e) {
ui.state.setText("初始化失败");
return;
} catch (SecurityException e) {
ui.state.setText("初始化失败");
return;
}
th = null;
}
public void close() {
try {
exitFlag = true;
if (writeWorkTh != null) {
synchronized (writeWorkTh) {
writeWorkTh.notify();
}
}
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
if (conn != null)
conn.close();
if (readWorkTh != null)
readWorkTh.join();
if (writeWorkTh != null)
writeWorkTh.join();
if (notifier != null)
notifier.close();
if (th != null)
th.join();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void send(String str) {
if (writeWorkTh == null)
return;
sendText = str;
synchronized (writeWorkTh) {
writeWorkTh.notify();
}
}
class ReadWorkThread extends Thread {
public void run() {
try {
while (!exitFlag) {
String str = in.readUTF();
if (str != null) {
ui.receiveTF.setString(str);
}
}
} catch (IOException e) {
if (!exitFlag)
ui.state.setText("读取数据失败");
}
}
}
class WriteWorkThread extends Thread {
public void run() {
try {
while (!exitFlag) {
synchronized (this) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
if (exitFlag)// 可能因为关闭操作被打断
break;
if (sendText != null)
out.writeUTF(sendText);
}
}
} catch (IOException e) {
if (!exitFlag)
ui.state.setText("写数据失败");
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -