ramsay-t / ShefRobot

Wrapper classes for Java code to use Lego robots.
3 stars 1 forks source link

Auto bluetooth connect #13

Open Robadob opened 9 years ago

Robadob commented 9 years ago

Decompile NXTCommand, see how it's automatic connecting to Robot works, see If we can do that.


Also look into whether the regular failure to connect initially can be fixed or automatically retried a few times.

ramsay-t commented 9 years ago

I’m not sure it was NXTCommand, I think it was the use robot script that ran something…

On 2 Nov 2015, at 12:32, Robert Chisholm notifications@github.com wrote:

Decompile NXTCommand, see how it's automatic connecting to Robot works, see If we can do that.

— Reply to this email directly or view it on GitHub https://github.com/ramsay-t/ShefRobot/issues/13.

Robadob commented 9 years ago

It was handled by calling

NXTCommand.open();
NXTCommand.setVerify(true);

Yes, bluecove does all the legwork, but the relevant code is within NXTCommand.

A Brief look through the decompliation shows that it passes through a bunch of classes, but icommand.nxt.comm.NXTCommBlueCove.search() probably does the important work.

public NXTInfo[] search(String paramString, int paramInt)
  {
    devices = new Vector();
    nxtInfos = new Vector();
    if ((paramInt | 0x2) == 0)
      return new NXTInfo[0];
    synchronized (this)
    {
      try
      {
        LocalDevice.getLocalDevice().getDiscoveryAgent().startInquiry(10390323, this);
        try
        {
          wait();
        }
        catch (InterruptedException localInterruptedException1)
        {
          localInterruptedException1.printStackTrace();
        }
      }
      catch (BluetoothStateException localBluetoothStateException1)
      {
        localBluetoothStateException1.printStackTrace();
      }
    }
    ??? = devices.elements();
    while (((Enumeration)???).hasMoreElements())
    {
      RemoteDevice localRemoteDevice = (RemoteDevice)((Enumeration)???).nextElement();
      try
      {
        this.nxtInfo = new NXTInfo();
        this.nxtInfo.name = localRemoteDevice.getFriendlyName(false);
        if ((this.nxtInfo.name == null) || (this.nxtInfo.name.length() == 0))
          this.nxtInfo.name = "Unknown";
        this.nxtInfo.btDeviceAddress = localRemoteDevice.getBluetoothAddress();
        this.nxtInfo.protocol = 2;
        if ((paramString == null) || (paramString.equals(this.nxtInfo.name)))
          nxtInfos.addElement(this.nxtInfo);
        else
          continue;
        System.out.println("Found: " + this.nxtInfo.name);
        int[] arrayOfInt = { 256, 257, 258 };
        UUID[] arrayOfUUID = new UUID[1];
        arrayOfUUID[0] = new UUID("1101", true);
        synchronized (this)
        {
          try
          {
            LocalDevice.getLocalDevice().getDiscoveryAgent().searchServices(arrayOfInt, arrayOfUUID, localRemoteDevice, this);
            try
            {
              wait();
            }
            catch (InterruptedException localInterruptedException3)
            {
              localInterruptedException3.printStackTrace();
            }
          }
          catch (BluetoothStateException localBluetoothStateException2)
          {
          }
        }
        try
        {
          Thread.sleep(100L);
        }
        catch (InterruptedException localInterruptedException2)
        {
          localInterruptedException2.printStackTrace();
        }
      }
      catch (IOException localIOException)
      {
        localIOException.printStackTrace();
      }
    }
    ??? = new NXTInfo[nxtInfos.size()];
    for (int i = 0; i < ???.length; i++)
      ???[i] = ((NXTInfo)nxtInfos.elementAt(i));
    return (NXTInfo)???;
  }

This returns to icommand.nxt.comm.NXTCommFactory.createInstance()

public static NXTComm createInstance()
  {
    Properties localProperties = getDefaults();
    File localFile = new File(UDIR + FSEP + "icommand.properties");
    if (!localFile.exists())
      localFile = new File(UHOME + FSEP + "icommand.properties");
    System.out.println("Properties location: " + localFile.getAbsolutePath());
    try
    {
      if (localFile.exists())
      {
        localProperties.load(new FileInputStream(localFile));
      }
      else
      {
        System.out.println("icommand.properties does NOT exist. Creating.");
        localFile.createNewFile();
        localProperties.store(new FileOutputStream(localFile), "Auto-generated iCommand Properties file");
      }
    }
    catch (IOException localIOException1)
    {
      System.out.println(localIOException1);
    }
    String str;
    Object localObject;
    NXTInfo[] arrayOfNXTInfo;
    if (localProperties.getProperty("nxtcomm.type").equals("bluecove"))
    {
      str = localProperties.getProperty("nxt.btaddress");
      if (str == null)
      {
        System.out.println("No NXT address found in icommand.properties. Searching... (this may take up to a minute)");
        localObject = new NXTCommBlueCove();
        arrayOfNXTInfo = ((NXTCommBlueCove)localObject).search(null, 2);
        if (arrayOfNXTInfo[0] != null)
        {
          str = arrayOfNXTInfo[0].btDeviceAddress;
          localProperties.setProperty("nxt.btaddress", str);
          try
          {
            localProperties.store(new FileOutputStream(localFile), "Auto-detected BT address. Feel free to change.");
          }
          catch (IOException localIOException2)
          {
            System.out.println(localIOException2);
          }
        }
        else
        {
          System.out.println("No NXT found via Bluetooth. Perhaps it is not turned on?");
          System.exit(0);
        }
        ((NXTCommBlueCove)localObject).setAddress(str);
        return localObject;
      }
      return new NXTCommBlueCove(localProperties);
    }
    if (localProperties.getProperty("nxtcomm.type").equals("bluez"))
    {
      str = localProperties.getProperty("nxt.btaddress");
      if (str == null)
      {
        System.out.println("No NXT address found in icommand.properties. Searching... (this may take up to a minute)");
        localObject = new NXTCommBluez();
        arrayOfNXTInfo = ((NXTCommBluez)localObject).search(null, 2);
        if (arrayOfNXTInfo[0] != null)
        {
          str = arrayOfNXTInfo[0].btDeviceAddress;
          localProperties.setProperty("nxt.btaddress", str);
          try
          {
            localProperties.store(new FileOutputStream(localFile), "Auto-detected BT address. Feel free to change.");
          }
          catch (IOException localIOException3)
          {
            System.out.println(localIOException3);
          }
        }
        else
        {
          System.out.println("No NXT found via Bluetooth. Perhaps it is not turned on?");
          System.exit(0);
        }
        ((NXTCommBluez)localObject).setAddress(str);
        return localObject;
      }
      return new NXTCommBluez(localProperties);
    }
    if (localProperties.getProperty("nxtcomm.type").equals("sun"))
      return new NXTCommSun(localProperties);
    return (NXTComm)new NXTCommRXTX(localProperties);
  }

etc.

The ???'s are probably due to me decompiling NXTCommand without having BlueCove on the classpath.

Robadob commented 9 years ago

Added note about initial failure to OP (probably worth handling at the same time).

Robadob commented 9 years ago

Research shows that the use of Personal Area Networks is a new development with EV3, and Bluecove doesn't support them (presumably because its development ended long ago). Lejos simply communicates over TCP with the EV3 once the PAN is established.

From my understanding you can not do that. Bluecove does not support the BNEP protocol. Only supports RFCOMM (serial protocol)

Should hopefully be possible to do it using a 3rd party tool/library though.