📄 btserver.java
字号:
/*
* BTServer.java
*
* Created on 2006年9月6日, 下午6:38
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package com.j2me.networld.connection;
import java.io.*;
import javax.bluetooth.*;
import javax.microedition.io.*;
/**
*
* @author DONGHOME
*/
public class BTServer
implements Runnable
{
public static final String uuidString = "0123456789ABCDEF0123456789ABCDEF";
public static UUID uuid = null;
private LocalDevice localDevice = null;
private Thread thread = null;
private Thread readWorkTH = null;
private Thread writeWorkTH = null;
private StreamConnectionNotifier notifier = null;
private ServiceRecord record = null;
private StreamConnection sc = null;
private boolean isClose = false;
private boolean isBTReady = false;
private DataInputStream dis = null;
private DataOutputStream dos = null;
private String URL = null;
private String sendText = null;
/** Creates a new instance of BTServer */
public BTServer(String message)
{
thread = new Thread(this);
thread.start();
}
public void run()
{
try
{
uuid = new UUID(uuidString, false);
localDevice = LocalDevice.getLocalDevice();
if(!localDevice.setDiscoverable(DiscoveryAgent.GIAC))
{
return;
}
}
catch(Exception e)
{
System.err.println("Exception: " + e.toString());
}
StringBuffer url = new StringBuffer("btspp://");
url.append("localhost").append(':');
url.append(uuid.toString());
url.append(";name=donghome");
url.append(";authorize=false");
try
{
notifier = (StreamConnectionNotifier)Connector.open(url.toString());
record = localDevice.getRecord(notifier);
sc = notifier.acceptAndOpen();
dis = sc.openDataInputStream();
dos = sc.openDataOutputStream();
readWorkTH = new ReadWorkThread();
readWorkTH.start();
writeWorkTH = new WriteWorkThread();
writeWorkTH.start();
isBTReady = true;
}
catch(IOException ioe)
{
ioe.printStackTrace();
return;
}
catch(SecurityException se)
{
se.printStackTrace();
return;
}
thread = null;
}
public void send(String str)
{
if(writeWorkTH == null)
{
return;
}
sendText = str;
synchronized(writeWorkTH)
{
writeWorkTH.notify();
}
}
public void close()
{
try
{
isClose = true;
if(writeWorkTH != null)
{
synchronized(writeWorkTH)
{
writeWorkTH.notify();
}
}
if(dis != null)
{
dis.close();
}
if(dos != null)
{
dos.close();
}
if(sc != null)
{
sc.close();
}
if(readWorkTH != null)
{
readWorkTH.join();
}
if(writeWorkTH != null)
{
writeWorkTH.join();
}
if(notifier != null)
{
notifier.close();
}
if(thread != null)
{
thread.join();
}
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
catch(InterruptedException ie)
{
ie.printStackTrace();
}
}
class ReadWorkThread extends Thread
{
public void run()
{
try
{
while(!isClose)
{
String str = dis.readUTF();
if(str != null)
{
URL = str;
}
}
}
catch(IOException ioe)
{
if(!isClose)
{
ioe.printStackTrace();
}
}
}
}
class WriteWorkThread extends Thread
{
public void run()
{
try
{
while(!isClose)
{
synchronized(this)
{
try
{
wait();
}
catch(InterruptedException ie)
{
ie.printStackTrace();
}
if(isClose)
{
break;
}
if(sendText != null)
{
dos.writeUTF(sendText);
}
}
}
}
catch(IOException ioe)
{
if(!isClose)
{
ioe.printStackTrace();
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -