private void handleBleConnecting()
 {
     BleApi.Service    service = new BleApi.Service();
     BleApi.ScanStatus status;
     do
     {
         status = BleApi.PollService(out service, false);
         if (status == BleApi.ScanStatus.AVAILABLE)
         {
             Debug.Log("Found service: " + service.uuid);
             string uuid = service.uuid.Replace("{", null).Replace("}", null);
             if (uuid.Equals(SERVICE_UUID, StringComparison.OrdinalIgnoreCase))
             {
                 /* Service found, subscribe to updates from TX char */
                 BleApi.SubscribeCharacteristic(truetouchDevice.id, SERVICE_UUID, TX_CHAR_UUID, false);
                 state = BleState.CONNECTED;
                 Debug.Log("Connected to TrueTouch");
                 return;
             }
         }
         else if (status == BleApi.ScanStatus.FINISHED)
         {
             /* Couldn't find target service on the device */
             errorString = "Could not find NUS service on TrueTouch";
             state       = BleState.ERROR;
         }
     } while (status == BleApi.ScanStatus.AVAILABLE);
 }
 /** Search for the TrueTouch device. */
 private void handleBleScanning()
 {
     BleApi.DeviceUpdate device = new BleApi.DeviceUpdate();
     BleApi.ScanStatus   status;
     do
     {
         status = BleApi.PollDevice(ref device, false);
         if (status == BleApi.ScanStatus.AVAILABLE)
         {
             Debug.Log("Found device: " + device.name + " | " + device.id);
             if (device.name.Equals(DEVICE_NAME, StringComparison.OrdinalIgnoreCase))
             {
                 /* TrueTouch found, scan for the desired service UUID */
                 BleApi.StopDeviceScan();
                 truetouchDevice = device;
                 BleApi.ScanServices(truetouchDevice.id);
                 state = BleState.CONNECTING;
                 Debug.Log("Scanning TrueTouch's services");
                 return;
             }
         }
         else if (status == BleApi.ScanStatus.FINISHED)
         {
             /* Ran out of devices without finding target device */
             errorString = "Could not find TrueTouch";
             state       = BleState.ERROR;
         }
     } while (status == BleApi.ScanStatus.AVAILABLE);
 }
 private void handleBleIdle()
 {
     /* Entering here must mean an error happened; just try to restart */
     BleApi.StartDeviceScan();
     state = BleState.SCANNING;
     Debug.Log("Restart scanning for devices");
 }
    // Start is called before the first frame update
    void Start()
    {
        /* Start scanning */
        BleApi.StartDeviceScan();
        state = BleState.SCANNING;
        Debug.Log("Scanning for devices");

        InvokeRepeating("executeHandUpdate", SendRateSeconds, SendRateSeconds);
    }
    /** Queue an update to be sent to the remote device. All queued updates are formatted
     *  into a BLE message and sent at the start of each frame. */
    public void UpdateFinger(Finger finger, UpdateType type, byte pwm = 0)
    {
        switch (type)
        {
        case UpdateType.PULSE_SOLENOID:
            solenoidsToPulse.Add(finger);
            break;

        case UpdateType.SET_ERM: {
            HashSet <Finger> set;

            /* If this finger was previously updated, negate that */
            foreach (KeyValuePair <byte, HashSet <Finger> > entry in ermUpdates)
            {
                if (entry.Value.Contains(finger) && entry.Key != pwm)
                {
                    entry.Value.Remove(finger);
                }
                else if (entry.Value.Contains(finger))
                {
                    /* Same finger, same PWM, nothing to be done */
                    return;
                }
            }

            /* New finger or PWM */
            if (ermUpdates.ContainsKey(pwm))
            {
                if (!ermUpdates.TryGetValue(pwm, out set))
                {
                    errorString = "Couldn't get value from ermUpdates";
                    state       = BleState.ERROR;
                }
                else
                {
                    set.Add(finger);         // don't care if it succeeds or not
                }
            }
            else
            {
                set = new HashSet <Finger>();
                set.Add(finger);
                ermUpdates.Add(pwm, set);
            }
        }
        break;
        }
    }
 private void handleBleError()
 {
     Debug.Log("Encountered Error: " + errorString);
     errorString = "None";
     state       = BleState.IDLE;
 }