/* * To change this template, choose Tools | Templates * and open the template in the editor. */ /** * * @author sherlock */ import java.util.*; import javax.bluetooth.*; public class NxtBluetooth implements DiscoveryListener { private Vector devices = new Vector (); private RemoteDevice brick; private String url; private NxtMidlet.Logger logger; public static int semmutex; static int i; public NxtBluetooth (NxtMidlet.Logger aLogger) { logger = aLogger; semmutex=0; i=0; } public void deviceDiscovered (RemoteDevice rd, DeviceClass dc) { devices.addElement (rd); logger.line (rd.getBluetoothAddress ()); } public void inquiryCompleted (int status) { logger.line ("Inquiry completed"); synchronized (this) { try { this.notifyAll (); } catch (Exception e){ } } } public void servicesDiscovered (int transId,ServiceRecord[]rec) { // This is hack! We suppose that there is only one SPP service on the brick, // so you just get the first record. url = rec[0].getConnectionURL (ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false); // This is for debug purpose only. It prints all services found. for (int i = 0; i < rec.length; i++) { String url =rec[i].getConnectionURL (ServiceRecord.NOAUTHENTICATE_NOENCRYPT,false); if (url == null) continue; DataElement serviceName =rec[i].getAttributeValue (0x0100); if (serviceName != null) logger.line ("Service " + serviceName.getValue () +" found " + url); else logger.line ("Service found " + url); } } public void serviceSearchCompleted (int transId, int respCode) { logger.line ("Servicio de Búsqueda Completado."); synchronized (this) { try { this.notifyAll (); } catch (Exception e){ } } } public String getURL () { return url; } public void find () throws Exception { try { DiscoveryAgent agent = LocalDevice.getLocalDevice ().getDiscoveryAgent (); LocalDevice.getLocalDevice ().setDiscoverable (DiscoveryAgent.GIAC); logger.line ("Buscando Dispositivos..."); logger.line ("Mac de dispos. bluetooth:"); agent.startInquiry (DiscoveryAgent.GIAC, this); synchronized (this){ try { this.wait (); } catch (Exception e){ } } //logger.line ("Names..."); i=0; NxtMidlet.pantalla.setCurrent( NxtMidlet.menuDisp); for (Enumeration en = devices.elements ();en.hasMoreElements ();) { RemoteDevice device = (RemoteDevice) en.nextElement (); String name = "?"; try { name = device.getFriendlyName (false); NxtMidlet.menuDisp.insert(0,name,null); }catch (java.io.IOException e){ logger.line (e.getMessage ()); } //if (name.equals ("NXT")) //brick = device; } i=0; while (semmutex==0){}; semmutex=0; for (Enumeration en2 = devices.elements ();en2.hasMoreElements ();) { RemoteDevice device = (RemoteDevice) en2.nextElement (); String name = "?"; try { name = device.getFriendlyName (false); if (i==NxtMidlet.select){ brick = device; logger.line ("Dispositivo bluetooth elegido: "+name); i=0; break; } i++; //NxtMidlet.menuDisp.insert(0,name,null); //logger.line (name); }catch (java.io.IOException e){ logger.line (e.getMessage ()); } //if (name.equals ("NXT")) //brick = device; } UUID serviceUUID = new UUID (0x1101); // We will find SPP services only UUID[]uuids = new UUID[] { serviceUUID }; int attrs[] = new int[]{ 0x0100 }; url = null; if (brick != null) { logger.line ("Searching services..."); agent.searchServices (attrs, uuids, brick, this); synchronized (this) { try { this.wait (); }catch (Exception e){ } } if (url == null) throw new Exception ("NXT SPP service not found"); } else throw new Exception ("NXT not found"); }catch (BluetoothStateException e){ logger.line (e.toString ()); throw new Exception ("Bluetooth error"); } } }