Guest User

Untitled

a guest
Jul 15th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. package hello;
  2.  
  3. import java.io.IOException;
  4. import javax.bluetooth.BluetoothStateException;
  5. import javax.bluetooth.DeviceClass;
  6. import javax.bluetooth.DiscoveryAgent;
  7. import javax.bluetooth.DiscoveryListener;
  8. import javax.bluetooth.LocalDevice;
  9. import javax.bluetooth.RemoteDevice;
  10. import javax.bluetooth.ServiceRecord;
  11. import javax.microedition.midlet.*;
  12. import javax.microedition.lcdui.*;
  13.  
  14.  
  15. public class HelloMIDlet extends MIDlet implements CommandListener, DiscoveryListener {
  16.  
  17. private Command exitCommand; // The exit command
  18. private Display display; // The display for this MIDlet
  19. private Command bluetoothCommand; // The exit command
  20. private Command clearCommand;
  21.  
  22. private TextBox t;
  23.  
  24. private int deviceCounter;
  25. private LocalDevice device;
  26. private DiscoveryAgent agent;
  27.  
  28. public HelloMIDlet() {
  29. display = Display.getDisplay(this);
  30. t = new TextBox("Hello", "Urządzenia", 500, 0);
  31. exitCommand = new Command("Exit", Command.EXIT, 0);
  32. bluetoothCommand = new Command("Bluetooth", Command.ITEM, 0);
  33. clearCommand = new Command("Clear", Command.ITEM, 0);
  34. try {
  35. device = LocalDevice.getLocalDevice();
  36. } catch (BluetoothStateException ex) {
  37. ex.printStackTrace();
  38. }
  39. agent = device.getDiscoveryAgent();
  40. }
  41.  
  42. public void startApp() {
  43.  
  44. t = new TextBox("Hello", "Urządzenia", 500, 0);
  45. t.addCommand(exitCommand);
  46. t.addCommand(bluetoothCommand);
  47. t.addCommand(clearCommand);
  48. t.setCommandListener(this);
  49. display.setCurrent(t);
  50. }
  51.  
  52. public void pauseApp() {
  53. }
  54.  
  55. public void destroyApp(boolean unconditional) {
  56. }
  57.  
  58. public void commandAction(Command c, Displayable s) {
  59. if (c == exitCommand) {
  60. destroyApp(false);
  61. notifyDestroyed();
  62. } else if (c == bluetoothCommand) {
  63. try {
  64.  
  65. agent.startInquiry(DiscoveryAgent.GIAC, this);
  66. } catch (BluetoothStateException ex) {
  67. ex.printStackTrace();
  68. }
  69. } else if (c == clearCommand) {
  70.  
  71. t.setString("Urządzenia: ");
  72. deviceCounter = 0;
  73. }
  74. }
  75.  
  76. public void deviceDiscovered(RemoteDevice arg0, DeviceClass arg1) {
  77. try {
  78. String text = t.getString();
  79. text = text + arg0.getFriendlyName(false);
  80. t.setString(text);
  81. deviceCounter++;
  82. } catch (IOException ex) {
  83. t.setString(ex.getMessage());
  84. }
  85. }
  86.  
  87. public void inquiryCompleted(int arg0) {
  88. String text = t.getString();
  89. text = text + "Znaleziono " + deviceCounter + " urządzeń";
  90. t.setString(text);
  91. }
  92.  
  93. public void serviceSearchCompleted(int arg0, int arg1) {
  94. throw new UnsupportedOperationException("Not supported yet.");
  95. }
  96.  
  97. public void servicesDiscovered(int arg0, ServiceRecord[] arg1) {
  98. throw new UnsupportedOperationException("Not supported yet.");
  99. }
  100.  
  101. }
Add Comment
Please, Sign In to add comment