public BluetoothClient() throws BluetoothStateException { LocalDevice localDev = LocalDevice.getLocalDevice(); System.out.println("Local Bluetooth Name is " + localDev.getFriendlyName()); localDev.setDiscoverable(DiscoveryAgent.GIAC); DiscoveryAgent discovery = localDev.getDiscoveryAgent(); discovery.startInquiry(DiscoveryAgent.GIAC, this); }
/** Called by iPOJO when the instance becomes valid. */ @Validate private void start() { logger.log(LogService.LOG_DEBUG, "BD Remote control component instance is starting."); String btAddress = null; LocalDevice localDevice = null; try { localDevice = LocalDevice.getLocalDevice(); } catch (BluetoothStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } DiscoveryAgent discoveryAgent = localDevice.getDiscoveryAgent(); RemoteDevice[] cachedDevices = discoveryAgent.retrieveDevices(DiscoveryAgent.CACHED); if (cachedDevices != null) { int s = cachedDevices.length; for (int i = 0; i < s; i++) { logger.log( LogService.LOG_DEBUG, "Cached device" + i + ":" + cachedDevices[i].getBluetoothAddress() + ", " + cachedDevices[i]); btAddress = cachedDevices[i].getBluetoothAddress(); } } RemoteDevice[] preknownDevices = discoveryAgent.retrieveDevices(DiscoveryAgent.PREKNOWN); if (preknownDevices != null) { int s = preknownDevices.length; for (int i = 0; i < s; i++) { logger.log( LogService.LOG_DEBUG, "Preknown device" + i + ":" + preknownDevices[i].getBluetoothAddress() + ", " + preknownDevices[i]); } } try { incomingThread = new IncomingThread(1, btAddress); incomingThread.start(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
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()); } }
/** * Creates a PrintClient object and prepares the object for device discovery and service * searching. * * @exception BluetoothStateException if the Bluetooth system could not be initialized */ public Discoverer() throws BluetoothStateException { /* * Retrieve the local Bluetooth device object. */ LocalDevice local = LocalDevice.getLocalDevice(); /* * Retrieve the DiscoveryAgent object that allows us to perform device * and service discovery. */ agent = local.getDiscoveryAgent(); deviceList = new Vector(); serviceList = new Vector(); }
public void run() { ran = true; LocalDevice localDevice; try { localDevice = LocalDevice.getLocalDevice(); } catch (BluetoothStateException e) { synchronized (waiter) { waiter.notify(); } return; } DiscoveryAgent discoveryAgent = localDevice.getDiscoveryAgent(); try { discoveryAgent.searchServices(null, uuidSet, device, new BluetoothHelper()); } catch (Exception e) { synchronized (waiter) { waiter.notify(); } return; } }
/** * Create and open a Connection. This Implementation only supports the <code>btl2cap://</code> * protocol schema. If url begins with <code>btl2cap://localhost</code>, this method call will * return a <code>javax.bluetooth.L2CAPConnectionNotifier</code>. * * @param url The URL for the connection. * @return A new <code>javax.bluetooth.L2CAPConnection</code> or a <code> * javax.bluetooth.L2CAPConnectionNotifier</code> * @throws IllegalArgumentException If a parameter is invalid. * @throws IOException If some other kind of I/O error occurs. * @see javax.bluetooth.L2CAPConnection * @see javax.bluetooth.L2CAPConnectionNotifier */ public static Connection open(String url) throws IOException { try { if (url.startsWith("btl2cap://localhost:")) { int endIndex = url.indexOf(';'); String psmString = url.substring(20, endIndex); Short psmShort = Short.decode(psmString); short psm = psmShort.shortValue(); return new JSR82ConnectionNotifier(psm); } if (url.startsWith("btl2cap://")) { byte[] bdAddrBytes = new byte[12]; String bdAddrString = url.substring(10, 22); Long bdAddrLong = Long.decode("0x" + bdAddrString); long remoteAddrLong = bdAddrLong.longValue(); int endIndex = url.indexOf(';', 22); String psmString = url.substring(23, endIndex); Short psmShort = Short.decode(psmString); short psm = psmShort.shortValue(); BluetoothStack bluetooth = BluetoothStack.getBluetoothStack(); LocalDevice localDev = LocalDevice.getLocalDevice(); DiscoveryAgent discovery = localDev.getDiscoveryAgent(); RemoteDevice remoteDevice = discovery.getRemoteDevice(remoteAddrLong); if (remoteDevice != null) { JSR82Channel channel = new JSR82Channel(); bluetooth.connectL2CAPChannel(channel, remoteDevice, psm); return channel; } else throw new IllegalArgumentException("Unable to locate Bluetooth Device."); } } catch (BluetoothStateException e) { throw new IOException("" + e); } catch (HCIException e) { throw new IOException("" + e); } throw new IllegalArgumentException( "This implementation of Connector only supports btl2cap:// Connections."); }
@Override public void run() { try { LocalDevice localDevice = LocalDevice.getLocalDevice(); logger.debug( "Initializing local bluetooth device ({}, {})", localDevice.getBluetoothAddress(), localDevice.getFriendlyName()); DiscoveryAgent agent = localDevice.getDiscoveryAgent(); final Object inquiryCompletedEvent = new Object(); // this is the call back for the bluetooth driver DiscoveryListener listener = new DiscoveryListener() { public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) { if (!newDevices.contains(btDevice)) { newDevices.add(btDevice); if (!oldDevices.contains(btDevice)) { BluetoothDevice device = toBluetoothDevice(btDevice); logger.debug("Device discovered: {}", device.toString()); for (BluetoothEventHandler handler : eventHandler) { handler.handleDeviceInRange(device); } } } } public void inquiryCompleted(int discType) { // check if any device has disappeared for (RemoteDevice btDevice : oldDevices) { if (newDevices.contains(btDevice)) continue; BluetoothDevice device = toBluetoothDevice(btDevice); logger.debug("Device out of range: {}", device.toString()); for (BluetoothEventHandler handler : eventHandler) { handler.handleDeviceOutOfRange(device); } } oldDevices = new HashSet<RemoteDevice>(newDevices); // we now pass the list of all devices in range to the event handlers Iterable<BluetoothDevice> devices = Iterables.transform( newDevices, new Function<RemoteDevice, BluetoothDevice>() { public BluetoothDevice apply(RemoteDevice from) { return toBluetoothDevice(from); } }); for (BluetoothEventHandler handler : eventHandler) { handler.handleAllDevicesInRange(devices); } newDevices.clear(); synchronized (inquiryCompletedEvent) { // tell the main thread that we are done inquiryCompletedEvent.notifyAll(); } } public void serviceSearchCompleted(int transID, int respCode) {} public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {} }; // this is the main loop, which will run as long as the thread is not marked as interrupted while (!interrupted) { long starttime = new Date().getTime(); // check if we at all need to run the bluetooth discovery boolean runDiscovery = false; for (BluetoothEventHandler handler : eventHandler) { if (handler.isActive()) { runDiscovery = true; break; } } if (runDiscovery) { synchronized (inquiryCompletedEvent) { try { logger.debug("Launching bluetooth device discovery..."); boolean started = agent.startInquiry(DiscoveryAgent.GIAC, listener); if (started) { inquiryCompletedEvent.wait(); } } catch (BluetoothStateException e) { logger.error("Error while starting the bluetooth agent", e); } catch (InterruptedException e) { interrupted = true; } } } if (!interrupted) { // let this thread sleep until the next discovery should be done long sleeptime = refreshRate * 1000L - (new Date().getTime() - starttime); if (sleeptime > 0) { logger.debug("Sleeping for {} s...", sleeptime / 1000.0); try { Thread.sleep(sleeptime); } catch (InterruptedException e) { interrupted = true; } } } } } catch (BluetoothStateException e) { logger.error("Error while initializing local bluetooth device.", e); } }
public BluetoothBrowser() throws BluetoothStateException { localDevice = LocalDevice.getLocalDevice(); agent = localDevice.getDiscoveryAgent(); }