Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package hello;
- import java.io.IOException;
- import javax.bluetooth.BluetoothStateException;
- import javax.bluetooth.DeviceClass;
- import javax.bluetooth.DiscoveryAgent;
- import javax.bluetooth.DiscoveryListener;
- import javax.bluetooth.LocalDevice;
- import javax.bluetooth.RemoteDevice;
- import javax.bluetooth.ServiceRecord;
- import javax.microedition.midlet.*;
- import javax.microedition.lcdui.*;
- public class HelloMIDlet extends MIDlet implements CommandListener, DiscoveryListener {
- private Command exitCommand; // The exit command
- private Display display; // The display for this MIDlet
- private Command bluetoothCommand; // The exit command
- private Command clearCommand;
- private TextBox t;
- private int deviceCounter;
- private LocalDevice device;
- private DiscoveryAgent agent;
- public HelloMIDlet() {
- display = Display.getDisplay(this);
- t = new TextBox("Hello", "Urządzenia", 500, 0);
- exitCommand = new Command("Exit", Command.EXIT, 0);
- bluetoothCommand = new Command("Bluetooth", Command.ITEM, 0);
- clearCommand = new Command("Clear", Command.ITEM, 0);
- try {
- device = LocalDevice.getLocalDevice();
- } catch (BluetoothStateException ex) {
- ex.printStackTrace();
- }
- agent = device.getDiscoveryAgent();
- }
- public void startApp() {
- t = new TextBox("Hello", "Urządzenia", 500, 0);
- t.addCommand(exitCommand);
- t.addCommand(bluetoothCommand);
- t.addCommand(clearCommand);
- t.setCommandListener(this);
- display.setCurrent(t);
- }
- public void pauseApp() {
- }
- public void destroyApp(boolean unconditional) {
- }
- public void commandAction(Command c, Displayable s) {
- if (c == exitCommand) {
- destroyApp(false);
- notifyDestroyed();
- } else if (c == bluetoothCommand) {
- try {
- agent.startInquiry(DiscoveryAgent.GIAC, this);
- } catch (BluetoothStateException ex) {
- ex.printStackTrace();
- }
- } else if (c == clearCommand) {
- t.setString("Urządzenia: ");
- deviceCounter = 0;
- }
- }
- public void deviceDiscovered(RemoteDevice arg0, DeviceClass arg1) {
- try {
- String text = t.getString();
- text = text + arg0.getFriendlyName(false);
- t.setString(text);
- deviceCounter++;
- } catch (IOException ex) {
- t.setString(ex.getMessage());
- }
- }
- public void inquiryCompleted(int arg0) {
- String text = t.getString();
- text = text + "Znaleziono " + deviceCounter + " urządzeń";
- t.setString(text);
- }
- public void serviceSearchCompleted(int arg0, int arg1) {
- throw new UnsupportedOperationException("Not supported yet.");
- }
- public void servicesDiscovered(int arg0, ServiceRecord[] arg1) {
- throw new UnsupportedOperationException("Not supported yet.");
- }
- }
Add Comment
Please, Sign In to add comment