티스토리 뷰

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.io.DataInputStream;
import java.io.IOException;

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 {

 static final UUID serverUUID = new UUID("a60f35f0b93a11de8a3908002009c669",
   false);
 static final String serverName = new String("BlueServer");

 private String serverURL;

 private LocalDevice localdevice = null;
 private StreamConnectionNotifier streamnotifier = null;
 private StreamConnection streamconn = null;

 BlueServer() {
  serverURL = new String("btspp://localhost:" + serverUUID.toString()
    + ";name=" + serverName + ";authenticate=false;encrypt=false;");

  try {
   System.out.println("Setting device to be discoverable...");
//   BlueCoveImpl.shutdownThreadBluetoothStack();
//   BlueCoveImpl.shutdown();
   localdevice = LocalDevice.getLocalDevice();
   localdevice.setDiscoverable(DiscoveryAgent.GIAC);

   System.out.println("Start advertising service...");
   streamnotifier = (StreamConnectionNotifier) Connector.open(serverURL);
   
   System.out.println("Waiting for incoming connection...");
   streamconn = streamnotifier.acceptAndOpen();
   System.out.println("Client Connected...");
           
   DataInputStream din = new DataInputStream(
     streamconn.openInputStream());

   while (true) {
    String cmd = "";    
    char c;
    
    while(((c = (char)din.readByte())>0) && (c!='$')){
     cmd += c;
    }
    
    if(cmd.equals("close")){
     break;
    }else if(cmd.equals("enter")){
     control(1);     
    }else if(cmd.equals("backspace")){
     control(2);     
    }
    System.out.println("Received " + cmd);
   }
  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   try {
    System.out.println("close stream notifier");
    streamnotifier.close();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
 }
 
 public void control(int num){
  try {
   Robot rb = new Robot();
   
   switch (num){
   case 1: // enter
    rb.keyPress(KeyEvent.VK_ENTER);
    break;
   case 2: // backspace
    rb.keyPress(KeyEvent.VK_BACK_SPACE);
    break;
   }
  } catch (AWTException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 
 public static void main(String[] args) {
  BlueServer bs = new BlueServer();
 }
}

반응형
댓글
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
최근에 올라온 글
글 보관함
Total
Today
Yesterday