// JAVADOC COMMENT ELIDED protected RemoteDevice(String address) { if (address == null) { throw new NullPointerException("null address"); } final String errorMsg = "Malformed address: " + address; if (address.length() != 12) { throw new IllegalArgumentException(errorMsg); } if (address.startsWith("-")) { throw new IllegalArgumentException(errorMsg); } try { l_address = Long.parseLong(address, 16); } catch (NumberFormatException e) { throw new IllegalArgumentException(errorMsg); } // should be upper case only address = address.toUpperCase(); try { String lAddr = LocalDevice.getLocalDevice().getBluetoothAddress(); if (address.equals(lAddr)) { throw new IllegalArgumentException("Can't use the local address."); } } catch (BluetoothStateException e) { throw new RuntimeException("Can't initialize bluetooth support"); } s_address = address; }
public NXTLocator(Display d) { mDisplay = d; messages = new TextBox("NXT Status", "Welcome!\n", 8192, TextField.ANY); mDisplay.setCurrent(messages); // retrieve the local Bluetooth device object try { local = LocalDevice.getLocalDevice(); agent = local.getDiscoveryAgent(); } catch (BluetoothStateException bse) { messages.insert("no device found\n", messages.size()); } }
public BluetoothHandler() { // display local device address and name LocalDevice localDevice; try { localDevice = LocalDevice.getLocalDevice(); System.out.println("Address: " + localDevice.getBluetoothAddress()); System.out.println("Name: " + localDevice.getFriendlyName()); } catch (BluetoothStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
protected BTGPSLocationProvider() throws LocationException { // TODO: Move this to searchConnect method? // TODO: The problem here is that it searches every time. Slow. Need to try Properties? // TODO: BIG ONE: Should only connect to GPS that isPaired() (from menu). Will // allow some degree of control over which GPS is connects to in classroom. try { da = LocalDevice.getLocalDevice().getDiscoveryAgent(); da.startInquiry(DiscoveryAgent.GIAC, this); } catch (BluetoothStateException e) { throw new LocationException(e.getMessage()); } while (!doneInq) { Thread.yield(); } if (btDevice == null) throw new LocationException("No device found"); String address = btDevice.getBluetoothAddress(); String btaddy = "btspp://" + address; try { StreamConnectionNotifier scn = (StreamConnectionNotifier) Connector.open(btaddy); if (scn == null) throw new LocationException("Bad BT address"); StreamConnection c = scn.acceptAndOpen(); /* This problem below occurred one time for my Holux GPS. The solution was to * remove the device from the Bluetooth menu, then find and pair again. */ if (c == null) throw new LocationException("Failed. Try pairing at menu again"); InputStream in = c.openInputStream(); if (in != null) { gps = new SimpleGPS(in); // c.close(); // TODO: Clean up when done. HOW TO HANDLE IN LOCATION? } } catch (IOException e) { throw new LocationException(e.getMessage()); } // Add itself to SimpleGPS as listener SimpleGPS.addListener(this); }
public static List<RemoteDevice> findDevices() { List<RemoteDevice> bluetoothDevices = new ArrayList<>(); final Object inquiryCompletedEvent = new Object(); DiscoveryListener listener = new DiscoveryListener() { @Override public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) { bluetoothDevices.add(btDevice); } @Override public void inquiryCompleted(int discType) { synchronized (inquiryCompletedEvent) { inquiryCompletedEvent.notifyAll(); } } @Override public void serviceSearchCompleted(int transID, int respCode) {} @Override public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {} }; synchronized (inquiryCompletedEvent) { try { boolean started = LocalDevice.getLocalDevice() .getDiscoveryAgent() .startInquiry(DiscoveryAgent.GIAC, listener); if (started) { inquiryCompletedEvent.wait(); } } catch (BluetoothStateException | InterruptedException ex) { LogManager.getLogger(BluetoothDevices.class).debug(ex); } } return bluetoothDevices; }
public static void main(String[] args) throws Exception { LocalDevice device = LocalDevice.getLocalDevice(); System.out.print(device.getFriendlyName() + " at "); System.out.print(device.getBluetoothAddress()); System.exit(0); }