Example #1
0
        public void StopReceiving()
        {
            if (this.ConnectionState == BikeConnectionState.Connected)
            {
                try
                {
                    this.resistanceSetTimer.Stop();

                    if (this.bleBike != null)
                    {
                        //this.bleBike.CloseDevice();
                        this.bleBike.Dispose();

                        this.bleBike = null;
                    }

                    if (this.bleHeart != null)
                    {
                        //this.bleHeart.CloseDevice();
                        this.bleHeart.Dispose();

                        this.bleHeart = null;
                    }
                }
                catch
                {
                    // jammer dan
                }

                this.ConnectionState = BikeConnectionState.Disconnected;
            }
        }
Example #2
0
        /// <summary>
        /// Attempt to connect to the Ergometer. For this the serial code is needed, potential errors are logged in an integer "errorCode".
        /// </summary>
        /// <param name="ergometerBLE"></param>
        /// <param name="ergometerSerialLastFiveNumbers"></param>
        /// <param name="errorCode"></param>

        private async void ConnectToErgoMeter(BLE ergometerBLE, string ergometerSerialLastFiveNumbers, int errorCode)
        {
            // Attempt to connect to the Ergometer.
            errorCode = await ergometerBLE.OpenDevice($"Tacx Flux {ergometerSerialLastFiveNumbers}");             // Example: Tacx Flux 01140

            // Receive bluetooth services and print afterwards, error check.
            PrintServices(ergometerBLE);
            // Set service
            string service1 = "6e40fec1-b5a3-f393-e0a9-e50e24dcca9e";

            errorCode = await ergometerBLE.SetService(service1);

            // Subscribe
            ergometerBLE.SubscriptionValueChanged += this.Ergo_SubscriptionValueChanged;
            string service2 = "6e40fec2-b5a3-f393-e0a9-e50e24dcca9e";

            errorCode = await ergometerBLE.SubscribeToCharacteristic(service2);

            if (errorCode != 0)
            {
                this.usingSim = true;
                this.iSim.Create();
            }
            Console.WriteLine(errorCode);             // Errorcode to check if is connected.
        }
Example #3
0
        public async static void InitBLEConnection()
        {
            BleBike  = new BLE();
            BleHeart = new BLE();
            bool AlreadySubscribed = false;

            if (!AlreadySubscribed)
            {
                AlreadySubscribed = true;

                int errorCode = 0;

                Thread.Sleep(1000); // We need some time to list available devices

                // List available devices
                List <String> bleBikeList = BleBike.ListDevices();
                Console.WriteLine("Devices found: ");
                foreach (var name in bleBikeList)
                {
                    Console.WriteLine($"Device: {name}");
                }

                // Connecting

                // __TODO__ Error check

                var services = BleBike.GetServices;
                foreach (var service in services)
                {
                    Console.WriteLine($"Service: {service}");
                }
            }
        }
Example #4
0
        public async Task <ActionResult <BLE> > PostBLE(BLE ble)
        {
            dbContext.BLEs.Add(ble);
            await dbContext.SaveChangesAsync();

            return(Ok(new{ success = "success" }));
        }
Example #5
0
 // Start is called before the first frame update
 void Start()
 {
     ble = new BLE();
     ButtonEstablishConnection.enabled = false;
     TextTargetDeviceConnection.text   = targetDeviceName + " not found.";
     readingThread = new Thread(ReadBleData);
 }
        /// <summary>
        /// Scan for a BLE Heartrate device.
        /// </summary>
        /// <param name="heartrateBLE"></param>
        /// <returns></returns>

        public async Task ScanConnectForHR(BLE heartrateBLE)
        {
            System.Int32 errorCode = 0;
            // List available device
            printDevices(heartrateBLE);
            // Heart rate monitor Bluetooth Low Energy code
            ConnectToHeartRateSensor(heartrateBLE, errorCode);
        }
Example #7
0
    public void Initialise(BLE cs)
    {
        gch = GCHandle.Alloc(cs);
        BLENativeInitialise(this, GCHandle.ToIntPtr(gch));
#if UNITY_STANDALONE_LINUX || UNITY_TEST_THREADING
        linuxHelperThread = new Thread(LinuxHelperLoop);
        linuxHelperThread.Start();
#endif
    }
        /// <summary>
        /// Stores a potential error code and prints devices, afterwards the connection attempt to the ergometer is made.
        /// </summary>
        /// <param name="ergometerBLE"></param>
        /// <param name="ergometerSerialLastFiveNumbers"></param>
        /// <returns></returns>

        public async Task ScanConnectForErgo(BLE ergometerBLE, System.String ergometerSerialLastFiveNumbers)
        {
            System.Int32 errorCode = 0;
            // List available device
            printDevices(ergometerBLE);
            // Ergometer Bluetooth Low Energy Code
            ConnectToErgoMeter(ergometerBLE, ergometerSerialLastFiveNumbers, errorCode);
            //this.SendResistance(ergometerBLE, 100);  // Change ergometer resistance!
        }
Example #9
0
 public App(IBluetoothPacketProvider provider, string userPath)
 {
     InitializeComponent();
     XF.Material.Forms.Material.Init(this, "Material.Configuration");
     XF.Material.Forms.Material.PlatformConfiguration.ChangeStatusBarColor(Color.White);
     BLEAdapter = new BLE(provider);
     UserPath   = userPath;
     MainPage   = new LoadingPage();
 }
Example #10
0
        /// <summary>
        /// Helper method to connect to the Ergometer and Heart rate monitor. Uses the "Thread.Sleep(1000)" to give some time for scanning for BLE devices.
        /// </summary>
        /// <param name="ergometerSerialLastFiveNumbers"></param>
        /// <returns></returns>

        public async Task ConnectToErgoAndHR(string ergometerSerialLastFiveNumbers)
        {
            this.ErgometerBLE = new BLE();
            this.HeartrateBLE = new BLE();
            Thread.Sleep(2000);
            await this.ScanConnectForErgo(this.ErgometerBLE, ergometerSerialLastFiveNumbers);

            await this.ScanConnectForHR(this.HeartrateBLE);
        }
Example #11
0
        /// <summary>
        /// Print BLE services.
        /// </summary>
        /// <param name="ergoMeterBle"></param>

        private static void PrintServices(BLE ergoMeterBle)
        {
            List <BluetoothLEAttributeDisplay> services = ergoMeterBle.GetServices;

            foreach (BluetoothLEAttributeDisplay service in services)
            {
                Console.WriteLine($"Service: {service}");
            }
        }
Example #12
0
        static void Main(string[] args)
        {
            Console.WriteLine("You can use this program to test the BleWinrtDll.dll. Make sure your Computer has Bluetooth enabled.");

            BLE    ble      = new BLE();
            string deviceId = null;

            BLE.BLEScan scan = BLE.ScanDevices();
            scan.Found = (_deviceId, deviceName) =>
            {
                Console.WriteLine("found device with name: " + deviceName);
                if (deviceId == null && deviceName == "CynteractGlove")
                {
                    deviceId = _deviceId;
                }
            };
            scan.Finished = () =>
            {
                Console.WriteLine("scan finished");
                if (deviceId == null)
                {
                    deviceId = "-1";
                }
            };
            while (deviceId == null)
            {
                Thread.Sleep(500);
            }

            scan.Cancel();
            if (deviceId == "-1")
            {
                Console.WriteLine("no device found!");
                return;
            }

            ble.Connect(deviceId,
                        "{f6f04ffa-9a61-11e9-a2a3-2a2ae2dbcce4}",
                        new string[] { "{f6f07c3c-9a61-11e9-a2a3-2a2ae2dbcce4}",
                                       "{f6f07da4-9a61-11e9-a2a3-2a2ae2dbcce4}",
                                       "{f6f07ed0-9a61-11e9-a2a3-2a2ae2dbcce4}" });

            for (int guard = 0; guard < 2000; guard++)
            {
                BLE.ReadPackage();
                BLE.WritePackage(deviceId,
                                 "{f6f04ffa-9a61-11e9-a2a3-2a2ae2dbcce4}",
                                 "{f6f07ffc-9a61-11e9-a2a3-2a2ae2dbcce4}",
                                 new byte[] { 0, 1, 2 });
                Console.WriteLine(BLE.GetError());
                Thread.Sleep(5);
            }

            Console.WriteLine("Press enter to exit the program...");
            Console.ReadLine();
        }
        /// <summary>
        /// Helper method to connect to the Ergometer and Heart rate monitor. Uses the "Thread.Sleep(1000)" to give some time for scanning for BLE devices.
        /// </summary>
        /// <param name="ergometerSerialLastFiveNumbers"></param>
        /// <returns></returns>

        public async Task ConnectToErgoAndHR(String ergometerSerialLastFiveNumbers)
        {
            BLE ergometerBLE = new BLE();
            BLE heartrateBLE = new BLE();

            Thread.Sleep(1000);

            await ScanConnectForErgo(ergometerBLE, ergometerSerialLastFiveNumbers);
            await ScanConnectForHR(heartrateBLE);
        }
 public RealBike(BLE bike, BLE heart)
 {
     this.bleBike        = bike;
     this.bleHeart       = heart;
     sendTimer           = new System.Timers.Timer(1000);
     sendTimer.Elapsed  += TimerCallBack;
     sendTimer.AutoReset = true;
     System.Threading.Thread thread = new System.Threading.Thread(new ThreadStart(WorkThreadFunctionRealBike));
     thread.Start();
 }
Example #15
0
 private void ReadBleData(object obj)
 {
     byte[] packageReceived = BLE.ReadBytes();
     // Convert little Endian.
     // In this example we're interested about an angle
     // value on the first field of our package.
     remoteAngle = packageReceived[0];
     Debug.Log("Angle: " + remoteAngle);
     //Thread.Sleep(100);
 }
Example #16
0
        /// <summary>
        /// Print BLE devices.
        /// </summary>
        /// <param name="ergoMeterBle"></param>

        private static void PrintDevices(BLE ergoMeterBle)
        {
            List <string> bluetoothDeviceList = ergoMeterBle.ListDevices();

            Console.WriteLine("Devices currently found:");
            foreach (string deviceName in bluetoothDeviceList)
            {
                Console.WriteLine($"Device: {deviceName}");
            }
        }
Example #17
0
        /// <summary>
        /// Attempt to change resistance of Ergometer.
        /// </summary>
        /// <param name="ble"></param>
        /// <param name="percentage"></param>

        public async void SendResistance(BLE ble, double percentage)
        {
            string service3 = "6e40fec3-b5a3-f393-e0a9-e50e24dcca9e";

            byte[] resistance = { 0xA4, 0x09, 0x4E, 0x05, 0x30, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, (byte)(percentage * 2), 0 };
            byte   checksum   = BLEDecoder.GetXorValue(resistance);

            resistance[resistance.Length - 1] = checksum;
            await ble.WriteCharacteristic(service3, resistance);
        }
        public Bluetooth(string bikeId, string heartMonitorId, ServerConnection sc) : base(sc)
        {
            decoder  = new Decoder();
            BleBike  = new BLE();
            BleHeart = new BLE();

            // Connect to the devices
            //var connectionTask = ConnectToDevices(bikeId, heartMonitorId);
            //connectionTask.Start();
            Task.Run(() => ConnectToDevices(bikeId, heartMonitorId));
        }
Example #19
0
        /// <summary>
        /// Attempt to setup a connection with the Heart Rate monitor.
        /// </summary>
        /// <param name="heartrateSensorBLE"></param>
        /// <param name="errorCode"></param>

        private async void ConnectToHeartRateSensor(BLE heartrateSensorBLE, int errorCode)
        {
            // Attempt to connect to the heart rate sensor.
            errorCode = await heartrateSensorBLE.OpenDevice("Decathlon Dual HR");

            // Set service
            await heartrateSensorBLE.SetService("HeartRate");

            // Subscribe
            heartrateSensorBLE.SubscriptionValueChanged += this.HR_SubscriptionValueChanged;
            await heartrateSensorBLE.SubscribeToCharacteristic("HeartRateMeasurement");
        }
Example #20
0
 public bool StartConnection()
 {
     try
     {
         bleBike           = new BLE();
         bleHeart          = new BLE();
         CurrentResistance = 0;
         Thread.Sleep(1000); // We need some time to list available devices
         return(true);
     }
     catch { return(false); }
 }
Example #21
0
        static async Task Main(string[] args)
        {
            int errorCode = 0;
            BLE bleBike   = new BLE();
            BLE bleHeart  = new BLE();

            Thread.Sleep(1000); // We need some time to list available devices

            // List available devices
            List <String> bleBikeList = bleBike.ListDevices();

            Console.WriteLine("Devices found: ");
            foreach (var name in bleBikeList)
            {
                Console.WriteLine($"Device: {name}");
            }

            // Connecting
            errorCode = errorCode = await bleBike.OpenDevice(System.IO.File.ReadAllText(@"BikeBluetoothName.txt"));

            // __TODO__ Error check

            var services = bleBike.GetServices;

            foreach (var service in services)
            {
                Console.WriteLine($"Service: {service}");
            }

            // Set service
            errorCode = await bleBike.SetService("6e40fec1-b5a3-f393-e0a9-e50e24dcca9e");

            // __TODO__ error check



            // Subscribe
            bleBike.SubscriptionValueChanged += BleBike_SubscriptionValueChanged;
            errorCode = await bleBike.SubscribeToCharacteristic("6e40fec2-b5a3-f393-e0a9-e50e24dcca9e");

            // Heart rate
            errorCode = await bleHeart.OpenDevice(System.IO.File.ReadAllText(@"BikeBluetoothName.txt"));

            await bleHeart.SetService("HeartRate");

            bleHeart.SubscriptionValueChanged += BleBike_SubscriptionValueChanged;
            await bleHeart.SubscribeToCharacteristic("HeartRateMeasurement");



            Console.Read();
        }
Example #22
0
    static void ScanDeviceFound(IntPtr ctx, IntPtr peripheral, IntPtr add, long RSSI)
    {
        GCHandle gch = GCHandle.FromIntPtr(ctx);
        BLE      ble = (BLE)gch.Target;

        // Log("Device found: " + ble + ", " + peripheral + ", " + add + ", " + RSSI);

        if (PeripheralDiscovered != null)
        {
            BLENativePeripheral peri = BLENativeCreatePeripheral(peripheral);
            peri.manager = ble.manager;
            PeripheralDiscovered(peri);
        }
    }
        /// <summary>
        /// Attempt to connect to the Ergometer. For this the serial code is needed, potential errors are logged in an integer "errorCode".
        /// </summary>
        /// <param name="ergometerBLE"></param>
        /// <param name="ergometerSerialLastFiveNumbers"></param>
        /// <param name="errorCode"></param>

        private async void ConnectToErgoMeter(BLE ergometerBLE, System.String ergometerSerialLastFiveNumbers, System.Int32 errorCode)
        {
            // Attempt to connect to the Ergometer.
            errorCode = await ergometerBLE.OpenDevice($"Tacx Flux {ergometerSerialLastFiveNumbers}"); // Example: Tacx Flux 01140

            // Receive bluetooth services and print afterwards, error check.
            printServices(ergometerBLE);
            // Set service
            errorCode = await ergometerBLE.SetService("6e40fec1-b5a3-f393-e0a9-e50e24dcca9e");

            // Subscribe
            ergometerBLE.SubscriptionValueChanged += Ergo_SubscriptionValueChanged;
            errorCode = await ergometerBLE.SubscribeToCharacteristic("6e40fec2-b5a3-f393-e0a9-e50e24dcca9e");
        }
 public async Task <bool> InitBleBike()
 {
     return(await Task.Run(() =>
     {
         try
         {
             this.BleBike = new BLE();
             Thread.Sleep(1000);
             return true;
         }
         catch (Exception)
         {
             return false;
         }
     }));
 }
Example #25
0
        private async void StartHeartrateMonitor()
        {
            Console.WriteLine("Connecting to HeartrateMonitor");
            int errorCode = 0;
            BLE BlEheart  = new BLE();

            errorCode = await BlEheart.OpenDevice("Decathlon Dual HR");

            Console.WriteLine("Waiting for Heartrate Services");
            await BlEheart.SetService("HeartRate");

            Console.WriteLine("Connected to Heartrate service");
            BlEheart.SubscriptionValueChanged += BleBike_SubscriptionValueChanged;
            await BlEheart.SubscribeToCharacteristic("HeartRateMeasurement");

            Console.WriteLine("Subscribed to Heartrate Measurement");
            Console.WriteLine("Errorcode: " + errorCode); // 1 = no heartbeat detected.
        }
        private async void ConnectToBike()
        {
            Console.WriteLine("Starting connection to bike");
            int errorCode = 0;
            BLE bleBike   = new BLE();

            Thread.Sleep(1000); // We need some time to list available devices

            // Connecting
            errorCode = errorCode = await bleBike.OpenDevice("Tacx Flux " + ModelNumber);

            // Set service
            errorCode = await bleBike.SetService("6e40fec1-b5a3-f393-e0a9-e50e24dcca9e");

            // Subscribe
            bleBike.SubscriptionValueChanged += BleBike_SubscriptionValueChanged;
            errorCode = await bleBike.SubscribeToCharacteristic("6e40fec2-b5a3-f393-e0a9-e50e24dcca9e");
        }
Example #27
0
        /// <summary>
        /// Checks for available devices to connect to, and if one is found, it connects to it
        /// </summary>
        public void Connect()
        {
            BLE bleBike = new BLE();

            Thread.Sleep(1000); // We need some time to list available devices

            // List available devices
            List <String> bleBikeList = bleBike.ListDevices();

            Console.WriteLine("[BLEHANDLER] Devices found: ");
            foreach (var name in bleBikeList)
            {
                Console.WriteLine(name);
                if (name.Contains("Avans Bike"))
                {
                    Console.WriteLine("[BLEHANDLER] connecting to {0}", name);
                    Connect(name);
                    break;
                }
            }
        }
Example #28
0
        static async Task Main(string[] args)
        {
            int errorCode = 0;
            BLE bleBike   = new BLE();
            BLE bleHeart  = new BLE();

            Thread.Sleep(1000); // We need some time to list available devices

            // List available devices
            List <String> bleBikeList = bleBike.ListDevices();

            Console.WriteLine("Devices found: ");
            foreach (var name in bleBikeList)
            {
                Console.WriteLine($"Device: {name}");
            }

            // Connecting
            errorCode = errorCode = await bleBike.OpenDevice("Tacx Flux 00438");

            Console.WriteLine($"Errorcode: {errorCode}");

            var services = bleBike.GetServices;

            foreach (var service in services)
            {
                Console.WriteLine($"Service: {service}");
            }

            // Set service
            errorCode = await bleBike.SetService("6e40fec1-b5a3-f393-e0a9-e50e24dcca9e");

            // __TODO__ error check

            // Subscribe
            bleBike.SubscriptionValueChanged += BleBike_SubscriptionValueChanged;
            errorCode = await bleBike.SubscribeToCharacteristic("6e40fec2-b5a3-f393-e0a9-e50e24dcca9e");

            Console.Read();
        }
Example #29
0
    void ScanBleDevices()
    {
        scan = BLE.ScanDevices();
        Debug.Log("BLE.ScanDevices() started.");
        scan.Found = (_deviceId, deviceName) =>
        {
            Debug.Log("found device with name: " + deviceName);
            discoveredDevices.Add(_deviceId, deviceName);

            if (deviceId == null && deviceName == targetDeviceName)
            {
                deviceId = _deviceId;
            }
        };

        scan.Finished = () =>
        {
            isScanning = false;
            Debug.Log("scan finished");
            if (deviceId == null)
            {
                deviceId = "-1";
            }
        };
        while (deviceId == null)
        {
            Thread.Sleep(500);
        }
        scan.Cancel();
        scanningThread = null;
        isScanning     = false;

        if (deviceId == "-1")
        {
            Debug.Log("no device found!");
            return;
        }
    }
Example #30
0
        /// <summary>
        /// Connects to the device with the given name
        /// </summary>
        /// <param name="deviceName">The name of the device to connect to</param>
        public async void Connect(string deviceName)
        {
            int errorCode = 0;

            bleBike  = new BLE();
            bleHeart = new BLE();

            errorCode = errorCode = await bleBike.OpenDevice(deviceName);

            if (errorCode > 0)
            {
                disposeBLE();
                return;
            }

            // Set service
            errorCode = await bleBike.SetService("6e40fec1-b5a3-f393-e0a9-e50e24dcca9e");

            if (errorCode > 0)
            {
                disposeBLE();
                return;
            }

            // Subscribe
            bleBike.SubscriptionValueChanged += BleBike_SubscriptionValueChanged;
            errorCode = await bleBike.SubscribeToCharacteristic("6e40fec2-b5a3-f393-e0a9-e50e24dcca9e");

            if (errorCode > 0)
            {
                disposeBLE();
                return;
            }

            // Heart rate
            errorCode = await bleHeart.OpenDevice(deviceName);

            if (errorCode > 0)
            {
                disposeBLE();
                return;
            }

            errorCode = await bleHeart.SetService("HeartRate");

            if (errorCode > 0)
            {
                disposeBLE();
                return;
            }

            bleHeart.SubscriptionValueChanged += BleBike_SubscriptionValueChanged;
            errorCode = await bleHeart.SubscribeToCharacteristic("HeartRateMeasurement");

            if (errorCode > 0)
            {
                disposeBLE();
                return;
            }

            Console.WriteLine("connected to BLE");
            this.Running = true;
        }