Advertisement
sfat

Server Bluetooth

May 19th, 2011
1,665
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.54 KB | None | 0 0
  1. package project;
  2.  
  3. import java.io.DataInputStream;
  4. import java.io.IOException;
  5.  
  6. import javax.bluetooth.BluetoothStateException;
  7. import javax.bluetooth.LocalDevice;
  8. import javax.microedition.io.Connector;
  9. import javax.microedition.io.StreamConnection;
  10. import javax.microedition.io.StreamConnectionNotifier;
  11.  
  12. public class SocketServer {
  13.     private static LocalDevice mLocalDevice;
  14.     private static StreamConnectionNotifier connectionNotifier;
  15.    
  16.     public static void main(String[] args) throws IOException, InterruptedException {
  17.         try
  18.         {
  19.             connectionNotifier =
  20.                 (StreamConnectionNotifier) Connector.open("btspp://localhost:" +
  21.                 "00000000000000000000000000001234;name=BtExample;" +
  22.                 "authenticate=false;encrypt=false;master=false");
  23.             while(true){
  24.                 new SocketServer().start();
  25.             }
  26.         } catch (BluetoothStateException e) {
  27.             System.out.println("Bluetooth not enabled!\nPlease enable your bluetooth first!");
  28.         }
  29.     }
  30.  
  31.     public SocketServer() throws IOException {
  32.         mLocalDevice = LocalDevice.getLocalDevice();
  33.         System.out.println("accepting on " + mLocalDevice.getBluetoothAddress());
  34.     }
  35.  
  36.     public void start() throws IOException {
  37.    
  38.         StreamConnection streamConnection = connectionNotifier.acceptAndOpen();
  39.         DataInputStream is = streamConnection.openDataInputStream();
  40.  
  41.         byte[] bytes = new byte[1024];
  42.         int r;
  43.         while ((r = is.read(bytes)) > 0) {
  44.                 System.out.println(new String(bytes, 0, r));
  45.         }
  46.     }
  47.  
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement