|
| 1 | +Direct SDK |
| 2 | +========== |
| 3 | + |
| 4 | +The first step to access Vinli Devices is to discover available Vinli Devices in the area. |
| 5 | + |
| 6 | + |
| 7 | + Observer<Device> devices = VinliDevices.createDeviceObservable(activity); |
| 8 | + |
| 9 | + |
| 10 | +Calling `VinliDevices#createDeviceObservable` returns an `Observer` of devices that will report available devices as they are discovered. |
| 11 | + |
| 12 | +Once a device is obtained, the developer needs to create a `DeviceInterface` by calling `Device#createDeviceInterface` with their current Android context. |
| 13 | + |
| 14 | +To obtain relatively static information such as VIN, device ID, and DTC codes, the device interface exposes `Device#readVin`, `Device#readChipId`, and `Device#readDtcCodes`, respectively |
| 15 | + |
| 16 | +Frequently updated information is accessed by supplying the `Pid` (for Parameter IDs) constant corresponding to the desired vehicle data. To observe realtime changes to the data, pass the desired `Pid` to `DeviceInterface#observe`. To synchronously retrieve the latest value for a parameter, pass the desired `Pid` to `DeviceInterface#getLatest`. Examples of available `Pid` constants include `Pid.Rpm` and `Pid.Speed_Mph` |
| 17 | + |
| 18 | +* To observe realtime streams of frequently updated device characteristic, the device interface provides `Device#observe{{characteristic}}` methods for each available charateristic, e.g. `Device#observeRpm` and `Device#observeSpeedMph` |
| 19 | +* To synchronously get only the latest observed value raw OBD value for a characteristic, the device interface exposes a `Device#getLatest{{characteristic}}` method for each available characteristic, e.g. `Device#getLatestRpm` and `Device#getLatestSpeed` |
| 20 | + |
| 21 | +Example Android fragment to show a device's vehicle's VIN, RPM, and speed: |
| 22 | + |
| 23 | + |
| 24 | + public class DeviceDetailFragment extends Fragment { |
| 25 | + public static final String DEVICE = "vinli_device"; |
| 26 | + |
| 27 | + private final List<Subscription> mCreatedSubscriptions = new ArrayList<Subscription>(); |
| 28 | + private final List<Subscription> mVisibleSubscriptions = new ArrayList<Subscription>(); |
| 29 | + |
| 30 | + private Device mDevice; |
| 31 | + private DeviceInterface mDi; |
| 32 | + |
| 33 | + @Override public void onCreate(Bundle savedInstanceState) { |
| 34 | + super.onCreate(savedInstanceState); |
| 35 | + if (getArguments().containsKey(ARG_ITEM_ID)) { |
| 36 | + mDevice = getArguments().getParcelable(ARG_ITEM_ID); |
| 37 | + mDi = mDevice.createDeviceInterface(getActivity()); |
| 38 | + } else { |
| 39 | + throw new IllegalArgumentException("missing device"); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + @Override public View onCreateView(LayoutInflater inflater, ViewGroup |
| 44 | + container, Bundle savedInstanceState) { |
| 45 | + final View rootView = inflater.inflate(R.layout.fragment_device_detail, container, false); |
| 46 | + |
| 47 | + ((TextView) rootView.findViewById(R.id.name)).setText(mDevice.getName()); |
| 48 | + |
| 49 | + final TextView vin = (TextView) rootView.findViewById(R.id.vin); |
| 50 | + mCreatedSubscriptions.add(AndroidObservable.bindFragment(this, mDi.readVin()) |
| 51 | + .subscribe(new Observer<String>() { |
| 52 | + @Override public void onCompleted() {} |
| 53 | + @Override public void onError(Throwable e) { |
| 54 | + vin.setText("failed to fetch vin"); |
| 55 | + } |
| 56 | + @Override public void onNext(String s) { |
| 57 | + vin.setText("VIN: " + s); |
| 58 | + } |
| 59 | + })); |
| 60 | + |
| 61 | + return rootView; |
| 62 | + } |
| 63 | + |
| 64 | + @Override public void onResume() { |
| 65 | + super.onResume(); |
| 66 | + |
| 67 | + final TextView rpm = (TextView) getView().findViewById(R.id.rpm); |
| 68 | + mVisibleSubscriptions.add(AndroidObservable.bindFragment(this, mDi.observe(Pid.Rpm)) |
| 69 | + .subscribe(new Observer<Float>() { |
| 70 | + @Override public void onCompleted() {} |
| 71 | + @Override public void onError(Throwable e) { |
| 72 | + rpm.setText("failed to get rpm"); |
| 73 | + } |
| 74 | + @Override public void onNext(Float f) { |
| 75 | + rpm.setText("rpm: " + f); |
| 76 | + } |
| 77 | + })); |
| 78 | + |
| 79 | + final TextView speedMph = (TextView) getView().findViewById(R.id.speed_mph); |
| 80 | + mVisibleSubscriptions.add(AndroidObservable.bindFragment(this, mDi.observe(Pid.Speed_Mph)) |
| 81 | + .subscribe(new Observer<Integer>() { |
| 82 | + @Override public void onCompleted() {} |
| 83 | + @Override public void onError(Throwable e) { |
| 84 | + speedMph.setText("failed to get speed Mph"); |
| 85 | + } |
| 86 | + @Override public void onNext(Integer i) { |
| 87 | + speedMph.setText("speed Mph: " + i); |
| 88 | + } |
| 89 | + })); |
| 90 | + } |
| 91 | + |
| 92 | + @Override public void onPause() { |
| 93 | + super.onPause(); |
| 94 | + unsubscribe(mVisibleSubscriptions); |
| 95 | + } |
| 96 | + |
| 97 | + @Override public void onDestroyView () { |
| 98 | + super.onDestroyView(); |
| 99 | + unsubscribe(mCreatedSubscriptions); |
| 100 | + } |
| 101 | + |
| 102 | + private static void unsubscribe(List<Subscription> subs) { |
| 103 | + for (Subscription s : subs) { |
| 104 | + s.unsubscribe(); |
| 105 | + } |
| 106 | + subs.clear(); |
| 107 | + } |
| 108 | + } |
0 commit comments