Advertisement
Guest User

Client

a guest
Jul 23rd, 2012
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.30 KB | None | 0 0
  1.  
  2. import java.io.DataInputStream;
  3. import java.io.DataOutputStream;
  4. import java.io.IOException;
  5. import java.util.Vector;
  6. import javax.bluetooth.*;
  7. import javax.microedition.io.Connector;
  8. import javax.microedition.io.StreamConnection;
  9. import javax.microedition.lcdui.*;
  10. import javax.microedition.midlet.MIDlet;
  11. import javax.microedition.midlet.MIDletStateChangeException;
  12.  
  13. public class client extends MIDlet implements DiscoveryListener, CommandListener {
  14.  
  15.     Command sendCmd;
  16.     Vector services = new Vector();
  17.     Vector devices = new Vector();
  18.     LocalDevice localdrive;
  19.     DiscoveryAgent agent;
  20.     String myServiceUUID = "2d26618601fb47c28d9f10b8ec891363";
  21.     UUID MYSERVICEUUID_UUID = new UUID(myServiceUUID, false);
  22.     UUID[] uuids = {MYSERVICEUUID_UUID};
  23.     String connurl;
  24.     StreamConnection SC;
  25.     ServiceRecord service;
  26.     String url;
  27.     StreamConnection conn;
  28.     DataOutputStream output;
  29.     DataInputStream input;
  30.  
  31.     protected void destroyApp(boolean unconditional) throws MIDletStateChangeException {
  32.     }
  33.  
  34.     protected void pauseApp() {
  35.     }
  36.  
  37.     protected void startApp() throws MIDletStateChangeException {
  38.         Form f = new Form("Client");
  39.         sendCmd = new Command("Send", Command.OK, 0);
  40.         f.addCommand(sendCmd);
  41.         f.setCommandListener(this);
  42.         Display d = Display.getDisplay(this);
  43.         d.setCurrent(f);
  44.  
  45.         try {
  46.             localdrive = LocalDevice.getLocalDevice();
  47.             agent = localdrive.getDiscoveryAgent();
  48.             agent.startInquiry(DiscoveryAgent.GIAC, this);
  49.         } catch (BluetoothStateException ex) {
  50.             ex.printStackTrace();
  51.         }
  52.  
  53.  
  54.     }
  55.  
  56.     public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
  57.         devices.addElement(btDevice);
  58.  
  59.     }
  60.  
  61.     public void inquiryCompleted(int discType) {
  62.         try {
  63.             RemoteDevice remote = (RemoteDevice) devices.elementAt(0);
  64.             agent.searchServices(null, uuids, remote, this);
  65.         } catch (BluetoothStateException ex) {
  66.             ex.printStackTrace();
  67.         }
  68.     }
  69.  
  70.     public void serviceSearchCompleted(int transID, int respCode) {
  71.         switch (respCode) {
  72.             case SERVICE_SEARCH_COMPLETED:
  73.                 createConnection();
  74.  
  75.                 break;
  76.             case SERVICE_SEARCH_DEVICE_NOT_REACHABLE:
  77.                 System.out.println("service not reachable");
  78.                 break;
  79.         }
  80.     }
  81.  
  82.     public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
  83.         service = servRecord[0];
  84.  
  85.  
  86.     }
  87.  
  88.     private void createConnection() {
  89.         try {
  90.             url = service.getConnectionURL(
  91.                     ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
  92.             conn = (StreamConnection) Connector.open(url);
  93.             output = conn.openDataOutputStream();
  94.             input = conn.openDataInputStream();
  95.         } catch (Exception e) {
  96.         }
  97.  
  98.     }
  99.  
  100.     public void commandAction(Command c, Displayable d) {
  101.         if (c == sendCmd) {
  102.             try {
  103.                 output.writeUTF("Hey server");
  104.                 String msg = input.readUTF();
  105.                 System.out.println(msg);
  106.             } catch (IOException ex) {
  107.                 ex.printStackTrace();
  108.             }
  109.         }
  110.     }
  111. }//end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement