Advertisement
Guest User

SPPServer

a guest
Sep 5th, 2013
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.io.InputStreamReader;
  5. import java.io.OutputStream;
  6. import java.io.OutputStreamWriter;
  7. import java.io.PrintWriter;
  8.  
  9. import javax.bluetooth.*;
  10. import javax.microedition.io.*;
  11.  
  12. /**
  13. * Class that implements an SPP Server which accepts single line of
  14. * message from an SPP client and sends a single line of response to the client.
  15. */
  16. public class SimpleSPPServer {
  17.  
  18. //start server
  19. private void startServer() throws IOException{
  20.  
  21. //Create a UUID for SPP
  22. UUID uuid = new UUID("1101", true);
  23. // UUID uuid = new UUID("0000110100001000800000805f9b34fb",false);
  24. //Create the servicve url
  25. String connectionString = "btspp://localhost:" + uuid +";name=Sample SPP Server";
  26.  
  27. //open server url
  28. StreamConnectionNotifier streamConnNotifier = (StreamConnectionNotifier)Connector.open( connectionString, Connector.READ_WRITE );
  29.  
  30. //Wait for client connection
  31. System.out.println("\nServer Started. Waiting for clients to connect...");
  32. StreamConnection connection=streamConnNotifier.acceptAndOpen();
  33.  
  34. RemoteDevice dev = RemoteDevice.getRemoteDevice(connection);
  35. System.out.println("Remote device address: "+dev.getBluetoothAddress());
  36. System.out.println("Remote device name: "+dev.getFriendlyName(true));
  37.  
  38. //read string from spp client
  39. InputStream inStream=connection.openInputStream();
  40. BufferedReader bReader=new BufferedReader(new InputStreamReader(inStream));
  41. String lineRead=bReader.readLine();
  42. System.out.println(lineRead);
  43.  
  44. //send response to spp client
  45. OutputStream outStream=connection.openOutputStream();
  46. PrintWriter pWriter=new PrintWriter(new OutputStreamWriter(outStream));
  47. pWriter.write("Response String from SPP Server\r\n");
  48. pWriter.flush();
  49.  
  50. pWriter.close();
  51. streamConnNotifier.close();
  52.  
  53. }
  54.  
  55.  
  56. public static void main(String[] args) throws IOException {
  57.  
  58. //display local device address and name
  59. LocalDevice localDevice = LocalDevice.getLocalDevice();
  60. System.out.println("Address: "+localDevice.getBluetoothAddress());
  61. System.out.println("Name: "+localDevice.getFriendlyName());
  62.  
  63. SimpleSPPServer sampleSPPServer=new SimpleSPPServer();
  64. sampleSPPServer.startServer();
  65.  
  66. }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement