@Override public void onOOBDataArrived(String dataName, String dataValue) { msg("(OOB Message) " + dataName + "=" + dataValue); // state change? if (dataName.equals(OOBMessageTypes.IO_STATE_CHANGE)) { int newState = 0; try { newState = Integer.valueOf(dataValue); ioStateChanged(newState); } catch (Exception e) { msg( "ERROR: Could not interpret new state as string: " + dataValue + " E=" + e.getMessage()); } } // end of "if this was a io state change". // Bluetooth unable to connect to peer? if (dataName.equals(OOBMessageTypes.BLUETOOTH_FAILED_CONNECT)) { if (dataValue.equals("0")) { msg("Unable to find peer. Searching for nearby OBD adapters."); aHelper.startDiscovering(); } } // session state change? if (dataName.equals(OOBMessageTypes.SESSION_STATE_CHANGE)) { int newState = 0; // convert from string to integer. try { newState = Integer.valueOf(dataValue); } catch (NumberFormatException e) { return; } // just connected? if (newState >= OBD2Session.STATE_OBDCONNECTED) { msg("Just connected - adding SPEED DPN to routinescan."); // Add some datapoints to the "routine scan" which is an // automatic loop that continuously scans those PIDs. hs.getRoutineScan().addDPN("SPEED"); hs.getRoutineScan().addDPN("RPM"); } else { msg("Just disconnected. removing all DPNs from routinescan."); hs.getRoutineScan().removeAllDPNs(); } } // end of session state change handler. // Did we just perform detection stuff? if (dataName.equals(OOBMessageTypes.AUTODETECT_SUMMARY)) { if (hs.isDetectionValid() != true) return; SetupSessionBasedOnCapabilities(); } // end of "if this is a autodetect summary" } // end of OOB event arrived handler function definition.
public AutoSessionOBD(HybridSession hsession, EventCallback newParentOOBMessageHandler) { hs = hsession; mParentOOBMessageHandler = newParentOOBMessageHandler; // set session type to OBD. That's all we'll be doing here. hs.setActiveSession(HybridSession.SESSION_TYPE_OBD2); rs = new RoutineScan(hs.getOBDSession(), hs.getPIDDecoder()); rs.addDPN("RPM"); rs.addDPN("SPEED"); msg("AutoSessionOBD initialized."); }
private boolean doBestAvailable() { // kick off Bluetooth discovery. At this point the phone looks for // nearby bluetooth devices. // For each device, ActivityHelper checks its name to see if it's an OBD // device. // If the device seems to be an ELM OBD device, it calls the // "chosenCallback" event with details about the device (its MAC). if (hs == null || hs.getEBT().isConnected() != true) { msg("Performing bluetooth discovery..."); connectToBestAvailable(); } else { msg("Unable to perform bluetooth discovery. BT Connected = " + hs.getEBT().isConnected()); return false; } return true; }
/** * @return - a reference to our generalstats object, which will also contain all the stats of our * children classes. */ public GeneralStats getStats() { // Merge stats from hybridsession and all of its children class. if (hs != null) mgStats.merge("hs", hs.getStats()); // returns our stats object, which now contains all the stats of our // children classes. return mgStats; }
/** * This method gets called by the broadcast receiver, for bluetooth devices which are "OBD" * devices. This takes care of any necessary actions to open a connection to the specified device. * Run synchronized in case the discovery process throws us multiple devices. We only want the * first valid one. * * @param deviceMACAddress * @return - true on success, false otherwise. */ private synchronized boolean setupSession(String deviceMACAddress) { // If there's an existing hybrid session, shut it down. if (hs != null) { hs.shutdown(); } // instantiate dashDB if necessary. if (ddb == null) { msg("Spinning up DashDB..."); ddb = new DashDB(MainActivity.this); msg("DashDB Ready."); } msg("Setting up hybridSession. It will now establish a bluetooth connection."); aHelper.setLastUsedMAC(deviceMACAddress); // instantiate hybridsession, which is just a class that controls // subclasses such as Monitorsession and OBDSession, that communicate // with the network in different ways. hs = new HybridSession( BluetoothAdapter.getDefaultAdapter(), deviceMACAddress, ddb, mLocalecbOOBMessageHandler); // after hybridsession is successful at opening the bluetooth // connection, we will get an OOB notification that the IO state changed // to "1". // Sets the session type to OBD2. nothing fancy. // hs.setActiveSession(HybridSession.SESSION_TYPE_OBD2); // register a method to be called when new data arrives. hs.registerDPArrivedCallback(mLocalDPNArrivedHandler); mBTPeerAddr = deviceMACAddress; return true; }
/** * Call this method upon connecting. We'll see what the capabilities are and set up the * appropriate session. */ private void SetupSessionBasedOnCapabilities() { if (hs.isDetectionValid() != true) { return; } if (hs.isHardwareSniffable() == true) { msg("Monitor is supported! Switching to monitor mode."); hs.setActiveSession(HybridSession.SESSION_TYPE_MONITOR); // at this point, monitor mode is active and it will automatically detect the network if // available. } else { msg("Monitor mode not supported so we'll enable OBD2 scanning."); // switch to OBD2 communications mode hs.setActiveSession(HybridSession.SESSION_TYPE_OBD2); // sanity check if (hs.getRoutineScan() == null) return; // add one or more datapoints to the routine scan class so that it actively scans that PID to // generate DPN arrived events. hs.getRoutineScan().addDPN("RPM"); } // // OBD2? // if (hs.isDetectionValid() == true) { // msg("Detection was successful, switching to OBD mode and adding a few datapoints to the // scan..."); // // switch to OBD mode. // hs.setActiveSession(HybridSession.SESSION_TYPE_OBD2); // // // start with a clean slate // hs.getRoutineScan().removeAllDPNs(); // // add speed and RPM to the routinescan. Routinescan will // // continuously request PIDs and as they are decoded, the // // DPDecoded event will fire. // hs.getRoutineScan().addDPN("SPEED"); // hs.getRoutineScan().addDPN("RPM"); // } }
protected void onDestroy() { super.onDestroy(); // give hs a chance to properly close the network/bluetooth link. if (hs != null) hs.shutdown(); };