import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.io.InputStream;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 BlueServer_class{
public static void main(String[] args) {
Thread waitThread = new Thread(new WaitThread());
waitThread.start();
}
} class WaitThread implements Runnable{
/** Constructor */
public WaitThread() {
}
@Override
public void run() {
waitForConnection();
}
/** Waiting for connection from devices */
private void waitForConnection() {
// retrieve the local Bluetooth device object
LocalDevice local = null;
StreamConnectionNotifier notifier;
StreamConnection connection = null;
// setup the server to listen for connection
try {
local = LocalDevice.getLocalDevice();
local.setDiscoverable(DiscoveryAgent.GIAC);
UUID uuid = new UUID(80087355); // "04c6093b-0000-1000-8000-00805f9b34fb"
String url = "btspp://localhost:" + uuid.toString() + ";name=RemoteBluetooth";
notifier = (StreamConnectionNotifier)Connector.open(url);
} catch (Exception e) {
e.printStackTrace();
return;
}
// waiting for connection
while(true) {
try {
System.out.println("waiting for connection...");
connection = notifier.acceptAndOpen();
Thread processThread = new Thread(new ProcessConnectionThread(connection));
processThread.start();
} catch (Exception e) {
e.printStackTrace();
return;
}
}
}
}
class ProcessConnectionThread implements Runnable{
private StreamConnection mConnection;
// Constant that indicate command from devices
private static final int EXIT_CMD = -1;
private static final int KEY_RIGHT = 1;
private static final int KEY_LEFT = 2;
public ProcessConnectionThread(StreamConnection connection)
{
mConnection = connection;
}
@Override
public void run() {
try {
// prepare to receive data
InputStream inputStream = mConnection.openInputStream();
System.out.println("waiting for input");
while (true) {
int command = inputStream.read();
if (command == EXIT_CMD)
{
System.out.println("finish process");
break;
}
processCommand(command);
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Process the command from client
* @param command the command code
*/
private void processCommand(int command) {
try {
Robot robot = new Robot();
switch (command) {
case KEY_RIGHT:
robot.keyPress(KeyEvent.VK_RIGHT);
System.out.println("Right");
break;
case KEY_LEFT:
robot.keyPress(KeyEvent.VK_LEFT);
System.out.println("Left");
break;
}
} catch (Exception e) {
e.printStackTrace();
}
}
} 错误
Exception in thread "Thread-1" java.lang.NoClassDefFoundError: javax/bluetooth/LocalDevice
at WaitThread.waitForConnection(BlueServer_class.java:44)
at WaitThread.run(BlueServer_class.java:31)
at java.lang.Thread.run(Thread.java:680)
Caused by: java.lang.ClassNotFoundException: javax.bluetooth.LocalDevice
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)大神们帮忙看看,小弟刚接触java 愚昧问题请见谅。
import java.awt.event.KeyEvent;
import java.io.InputStream;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 BlueServer_class{
public static void main(String[] args) {
Thread waitThread = new Thread(new WaitThread());
waitThread.start();
}
} class WaitThread implements Runnable{
/** Constructor */
public WaitThread() {
}
@Override
public void run() {
waitForConnection();
}
/** Waiting for connection from devices */
private void waitForConnection() {
// retrieve the local Bluetooth device object
LocalDevice local = null;
StreamConnectionNotifier notifier;
StreamConnection connection = null;
// setup the server to listen for connection
try {
local = LocalDevice.getLocalDevice();
local.setDiscoverable(DiscoveryAgent.GIAC);
UUID uuid = new UUID(80087355); // "04c6093b-0000-1000-8000-00805f9b34fb"
String url = "btspp://localhost:" + uuid.toString() + ";name=RemoteBluetooth";
notifier = (StreamConnectionNotifier)Connector.open(url);
} catch (Exception e) {
e.printStackTrace();
return;
}
// waiting for connection
while(true) {
try {
System.out.println("waiting for connection...");
connection = notifier.acceptAndOpen();
Thread processThread = new Thread(new ProcessConnectionThread(connection));
processThread.start();
} catch (Exception e) {
e.printStackTrace();
return;
}
}
}
}
class ProcessConnectionThread implements Runnable{
private StreamConnection mConnection;
// Constant that indicate command from devices
private static final int EXIT_CMD = -1;
private static final int KEY_RIGHT = 1;
private static final int KEY_LEFT = 2;
public ProcessConnectionThread(StreamConnection connection)
{
mConnection = connection;
}
@Override
public void run() {
try {
// prepare to receive data
InputStream inputStream = mConnection.openInputStream();
System.out.println("waiting for input");
while (true) {
int command = inputStream.read();
if (command == EXIT_CMD)
{
System.out.println("finish process");
break;
}
processCommand(command);
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Process the command from client
* @param command the command code
*/
private void processCommand(int command) {
try {
Robot robot = new Robot();
switch (command) {
case KEY_RIGHT:
robot.keyPress(KeyEvent.VK_RIGHT);
System.out.println("Right");
break;
case KEY_LEFT:
robot.keyPress(KeyEvent.VK_LEFT);
System.out.println("Left");
break;
}
} catch (Exception e) {
e.printStackTrace();
}
}
} 错误
Exception in thread "Thread-1" java.lang.NoClassDefFoundError: javax/bluetooth/LocalDevice
at WaitThread.waitForConnection(BlueServer_class.java:44)
at WaitThread.run(BlueServer_class.java:31)
at java.lang.Thread.run(Thread.java:680)
Caused by: java.lang.ClassNotFoundException: javax.bluetooth.LocalDevice
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)大神们帮忙看看,小弟刚接触java 愚昧问题请见谅。
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货