Esempio n. 1
0
        public async void Connect()
        {
            DeviceType    dv     = MainPage.TestresultModel.GetDeviceType(3);
            BLEDeviceInfo device = null;

            foreach (var dev in MainPage.TestresultModel.BLEDevices)
            {
                if (dev.DeviceType == DeviceTypesenums.THERMOMETER && dev.IsPaired)
                {
                    device = dev;
                    break;
                }
                else if (dev.DeviceType == DeviceTypesenums.THERMOMETER && !dev.IsPaired)
                {
                    MainPage.mainPage.commChannel.SendMessageToMCC(
                        string.Format(CommunicationCommands.THERMORCONNECTIONSTATUS, "Device is not paried."));
                    MainPage.TestresultModel.NotifyStatusMessage?.Invoke("Device is not paried. ", 1);
                    return;
                }
            }

            GTTServicelist.Clear();
            if (device == null)
            {
                MainPage.mainPage.commChannel.SendMessageToMCC(
                    string.Format(CommunicationCommands.THERMORCONNECTIONSTATUS, "Device not found with id:"));

                MainPage.TestresultModel.NotifyStatusMessage?.Invoke(" Device not found with id: " + MainPage.TestresultModel.ThermoMeterId, 1);
                return;
            }
            bluetoothLeDevice = await BluetoothLEDevice.FromIdAsync(device.DeviceInfo.Id);

            if (bluetoothLeDevice == null)
            {
                MainPage.mainPage.commChannel.SendMessageToMCC(
                    string.Format(CommunicationCommands.THERMORCONNECTIONSTATUS, "Device Unreachable."));
                MainPage.TestresultModel.NotifyStatusMessage?.Invoke(" Device Unreachable.", 1);
                return;
            }

            GattDeviceServicesResult result = await bluetoothLeDevice.GetGattServicesAsync(BluetoothCacheMode.Uncached);

            if (result.Status == GattCommunicationStatus.Success)
            {
                var services = result.Services;
                foreach (var svc in services)
                {
                    GTTServicelist.Add(svc.Uuid.ToString().ToLower(), svc);
                }
                SelectService(device);  //MainPage.TestresultModel.ThermoServiceID
            }
            else
            {
                MainPage.mainPage.commChannel.SendMessageToMCC(
                    string.Format(CommunicationCommands.THERMORCONNECTIONSTATUS, "Failed to connect. " + result.Status.ToString()));

                MainPage.TestresultModel.NotifyStatusMessage?.Invoke("Failed to connect. " + result.Status.ToString(), 1);
            }
        }
Esempio n. 2
0
        public async void Connect()
        {
            BLEDeviceInfo device = null;

            foreach (var dev in MainPage.TestresultModel.BLEDevices)
            {
                if (dev.DeviceType == DeviceTypesenums.BPMONITOR && dev.IsPaired)
                {
                    device = dev;
                    break;
                }
                else if (dev.DeviceType == DeviceTypesenums.BPMONITOR && !dev.IsPaired)
                {
                    SendConnectionStatusmessageToMCC("BP Monitor is not Paired.");
                    NotifyStatusMessage?.Invoke("BP Monitor is not Paired.", 3);
                    return;
                }
            }


            GTTServicelist.Clear();
            if (device == null)
            {
                SendConnectionStatusmessageToMCC("BP Monitor is not foud.");
                NotifyStatusMessage?.Invoke("BP Monitor is not foud.", 3);
                return;
            }

            bluetoothLeDevice = await BluetoothLEDevice.FromIdAsync(device.DeviceInfo.Id);

            if (bluetoothLeDevice == null)
            {
                SendConnectionStatusmessageToMCC("Device Unreachable.");
                NotifyStatusMessage?.Invoke("Device Unreachable.", 1);
                return;
            }

            // Note: BluetoothLEDevice.GattServices property will return an empty list for unpaired devices. For all uses we recommend using the GetGattServicesAsync method.
            // BT_Code: GetGattServicesAsync returns a list of all the supported services of the device (even if it's not paired to the system).
            // If the services supported by the device are expected to change during BT usage, subscribe to the GattServicesChanged event.
            GattDeviceServicesResult result = await bluetoothLeDevice.GetGattServicesAsync(BluetoothCacheMode.Uncached);

            if (result.Status == GattCommunicationStatus.Success)
            {
                var services = result.Services;
                foreach (var svc in services)
                {
                    GTTServicelist.Add(svc.Uuid.ToString().ToLower(), svc);
                }
                SelectBPMeasureServicesAsync(device);//MainPage.TestresultModel.BPServiceID);
            }
            else
            {
                SendConnectionStatusmessageToMCC("Failed to connect. " + result.Status.ToString());
                NotifyStatusMessage?.Invoke("Failed to connect. " + result.Status.ToString(), 1);
            }
        }
Esempio n. 3
0
        private static async Task <bool> CheckForCustomCharacteristic(BLEDeviceInfo info)
        {
            try
            {
                await using (var client = new Hm1xBLEClient(info.Id))
                {
                    return(await client.IsHm1xCompatibleDevice());
                }
            }
            catch
            { }

            return(false);
        }
Esempio n. 4
0
        private static BLEDeviceInfo SelectDevice(BLEDeviceInfo[] devices)
        {
            if (devices.Length == 0)
            {
                Console.WriteLine("No compatible Bluetooth LE device found.\r\nEnsure that:\r\n- your device is powered and it is advertising.\r\n- device is compatible with HM-10 module.\r\n- device is not paired with Windows (see Bluetooth Devices in Control Panel).");
                return(null);
            }

            if (devices.Length == 1)
            {
                BLEDeviceInfo selectedDevice = devices[0];
                Console.WriteLine($"A single device matches expected GATT service:\r\n{JsonConvert.SerializeObject(selectedDevice, Formatting.Indented)}\r\nTo continue, press any key");
                Console.ReadKey(intercept: true);
                return(selectedDevice);
            }

            string statusMsg = string.Empty;

            while (true)
            {
                Console.Clear();
                Console.WriteLine(statusMsg);
                Console.WriteLine("Select Bluetooth LE device:");
                for (int i = 0; i < devices.Length; i++)
                {
                    var item = devices[i];
                    Console.WriteLine($"[{i + 1}] {item.Address}\t{item.Name}");
                }

                string input = Console.ReadLine();
                if (!int.TryParse(input, out int index))
                {
                    Console.WriteLine("No selection");
                    return(null);
                }
                if (index < 1 || index > devices.Length)
                {
                    statusMsg = $"Selection {index} is out of range";
                    continue;
                }

                BLEDeviceInfo selectedDevice = devices[index - 1];
                Console.WriteLine($"Selected device:\r\n{JsonConvert.SerializeObject(selectedDevice, Formatting.Indented)}\r\nTo confirm, press Enter");
                if (Console.ReadKey(intercept: true).Key == ConsoleKey.Enter)
                {
                    return(selectedDevice);
                }
            }
        }
Esempio n. 5
0
        public static async Task <string> StartWithDeviceSelection()
        {
            var scanner = new Hm1xDeviceScanner();

            Console.WriteLine("Scanning Bluetooth Low Energy devices of kind HM-1x, please wait...");
            BLEDeviceInfo[] devices = await scanner.ScanBluetoothAdvertisers();

            BLEDeviceInfo selectedDeviceInfo = SelectDevice(devices);

            if (selectedDeviceInfo != null)
            {
                string deviceId = selectedDeviceInfo.Id;
                await StartWithDevice(deviceId);

                return(deviceId);
            }
            return(null);
        }
Esempio n. 6
0
        public async Task <BLEDeviceInfo[]> ScanBluetoothAdvertisers()
        {
            // For a more responsive user experience,
            //  see BluetoothLEAdvertisementWatcher or
            //  DeviceInformation.CreateWatcher()
            //  instead of DeviceInformation.FindAllAsync().
            const string bluetoothLeDevicesFilter  = "(System.Devices.Aep.ProtocolId:=\"{" + BLEProtocolId + "}\")";
            DeviceInformationCollection bleDevices = await DeviceInformation.FindAllAsync(bluetoothLeDevicesFilter, RequestedProperties, DeviceInformationKind.AssociationEndpoint);

            var result = new List <BLEDeviceInfo>(bleDevices.Count);

            for (int i = 0; i < bleDevices.Count; i++)
            {
                var model = BLEDeviceInfo.FromRawDeviceInformation(bleDevices[i]) with {
                    OriginalOrder = i
                };
                if (await CheckForCustomCharacteristic(model))
                {
                    result.Add(model);
                }
            }
            result.Sort(BLEDeviceInfoComparer.Instance);
            return(result.ToArray());
        }
Esempio n. 7
0
        async void SelectService(BLEDeviceInfo dev)
        {
            try
            {
                var service = GTTServicelist[dev.DeviceTypeInfo.ServiceID.ToString().ToLower()];
                // Ensure we have access to the device.
                var accessStatus = await service.RequestAccessAsync();

                if (accessStatus == DeviceAccessStatus.Allowed)
                {
                    var result = await service.GetCharacteristicsAsync(BluetoothCacheMode.Uncached);

                    if (result.Status == GattCommunicationStatus.Success)
                    {
                        characteristics = result.Characteristics;
                    }
                    else
                    {
                        MainPage.mainPage.commChannel.SendMessageToMCC(string.Format(CommunicationCommands.PUSLEOXIMETERCONNECTIONMSG,
                                                                                     "Error accessing service " + result.Status.ToString()));
                        MainPage.TestresultModel.NotifyStatusMessage?.Invoke("Error accessing service " + result.Status.ToString(), 1);
                        return;
                    }
                }
                else
                {
                    MainPage.mainPage.commChannel.SendMessageToMCC(string.Format(CommunicationCommands.PUSLEOXIMETERCONNECTIONMSG,
                                                                                 "Error accessing service " + accessStatus.ToString()));
                    MainPage.TestresultModel.NotifyStatusMessage?.Invoke("Error accessing service " + accessStatus.ToString(), 1);
                    return;
                }
            }
            catch (Exception ex)
            {
                MainPage.mainPage.commChannel.SendMessageToMCC(string.Format(CommunicationCommands.PUSLEOXIMETERCONNECTIONMSG,
                                                                             "Exception " + ex.Message));
                MainPage.TestresultModel.NotifyStatusMessage?.Invoke("Exception " + ex.Message, 2);
                return;
            }

            foreach (GattCharacteristic c in characteristics)
            {
                string characteristicname = DisplayHelpers.GetCharacteristicName(c);
                if (c.AttributeHandle.ToString().Equals(dev.DeviceTypeInfo.CharacteristicAttributeid)) //MainPage.TestresultModel.OximeterHandleid)
                {
                    oxymeterCharacteristic = c;
                    break;
                }
            }

            if (oxymeterCharacteristic == null)
            {
                MainPage.mainPage.commChannel.SendMessageToMCC(string.Format(CommunicationCommands.PUSLEOXIMETERCONNECTIONMSG,
                                                                             "OxyMeter Characteristic not found."));
                MainPage.TestresultModel.NotifyStatusMessage?.Invoke("OxyMeter Characteristic not found.", 1);
                return;
            }
            // BloodPressureMeasurementCharacteristic = selectedCharacteristic;
            var resultch = await oxymeterCharacteristic.GetDescriptorsAsync(BluetoothCacheMode.Uncached);

            if (resultch.Status != GattCommunicationStatus.Success)
            {
                MainPage.mainPage.commChannel.SendMessageToMCC(string.Format(CommunicationCommands.PUSLEOXIMETERCONNECTIONMSG,
                                                                             "Descriptor read failure: " + resultch.Status.ToString()));
                MainPage.TestresultModel.NotifyStatusMessage?.Invoke("Descriptor read failure: " + resultch.Status.ToString(), 1);
                return;
            }
            presentationFormat = null;
            if (oxymeterCharacteristic.PresentationFormats.Count > 0)
            {
                if (oxymeterCharacteristic.PresentationFormats.Count.Equals(1))
                {
                    // Get the presentation format since there's only one way of presenting it
                    presentationFormat = oxymeterCharacteristic.PresentationFormats[0];
                }
                else
                {
                    // It's difficult to figure out how to split up a characteristic and encode its different parts properly.
                    // In this case, we'll just encode the whole thing to a string to make it easy to print out.
                }
            }
            SubscribeDeviceReading();
        }///
Esempio n. 8
0
        public async void Connect()
        {
            DeviceType    dv     = MainPage.TestresultModel.GetDeviceType(2);
            BLEDeviceInfo device = null;

            foreach (var dev in MainPage.TestresultModel.BLEDevices)
            {
                // if (dev.Id.Equals(MainPage.TestresultModel.OximeterID))
                if (dev.DeviceType == DeviceTypesenums.OXIMETER && dev.IsPaired)
                {
                    device = dev;
                    break;
                }
                else if (dev.DeviceType == DeviceTypesenums.OXIMETER && !dev.IsPaired)
                {
                    MainPage.mainPage.commChannel.SendMessageToMCC(string.Format(CommunicationCommands.PUSLEOXIMETERCONNECTIONMSG,
                                                                                 "Device is not paired."));

                    MainPage.TestresultModel.NotifyStatusMessage?.Invoke(" Device is not paired.", 1);
                    return;
                }
            }

            GTTServicelist.Clear();
            if (device == null)
            {
                MainPage.mainPage.commChannel.SendMessageToMCC(string.Format(CommunicationCommands.PUSLEOXIMETERCONNECTIONMSG,
                                                                             "No device found."));
                MainPage.TestresultModel.NotifyStatusMessage?.Invoke("No device found.", 1);
                return;
            }

            bluetoothLeDevice = await BluetoothLEDevice.FromIdAsync(device.DeviceInfo.Id);

            if (bluetoothLeDevice == null)
            {
                MainPage.mainPage.commChannel.SendMessageToMCC(string.Format(CommunicationCommands.PUSLEOXIMETERCONNECTIONMSG,
                                                                             " Device Unreachable."));
                MainPage.TestresultModel.NotifyStatusMessage?.Invoke(" Device Unreachable.", 1);
            }

            // Note: BluetoothLEDevice.GattServices property will return an empty list for unpaired devices. For all uses we recommend using the GetGattServicesAsync method.
            // BT_Code: GetGattServicesAsync returns a list of all the supported services of the device (even if it's not paired to the system).
            // If the services supported by the device are expected to change during BT usage, subscribe to the GattServicesChanged event.
            GattDeviceServicesResult result = await bluetoothLeDevice.GetGattServicesAsync(BluetoothCacheMode.Uncached);

            if (result.Status == GattCommunicationStatus.Success)
            {
                var services = result.Services;
                foreach (var svc in services)
                {
                    GTTServicelist.Add(svc.Uuid.ToString().ToLower(), svc);
                }
                SelectService(device); // MainPage.TestresultModel.OximeterserviceID);
            }
            else
            {
                MainPage.mainPage.commChannel.SendMessageToMCC(string.Format(CommunicationCommands.PUSLEOXIMETERCONNECTIONMSG, "Failed to connect. " + result.Status.ToString()));
                MainPage.TestresultModel.NotifyStatusMessage?.Invoke("Failed to connect. " + result.Status.ToString(), 1);
            }
        }
Esempio n. 9
0
        public async void SelectBPMeasureServicesAsync(BLEDeviceInfo dv)
        {
            try
            {
                var service = GTTServicelist[dv.DeviceTypeInfo.ServiceID.ToLower()];
                // Ensure we have access to the device.
                var accessStatus = await service.RequestAccessAsync();

                if (accessStatus == DeviceAccessStatus.Allowed)
                {
                    var result = await service.GetCharacteristicsAsync(BluetoothCacheMode.Uncached);

                    if (result.Status == GattCommunicationStatus.Success)
                    {
                        characteristics = result.Characteristics;
                    }
                    else
                    {
                        SendConnectionStatusmessageToMCC("Error accessing service " + result.Status.ToString());
                        NotifyStatusMessage?.Invoke("Error accessing service " + result.Status.ToString(), 1);
                        return;
                    }
                }
                else
                {
                    SendConnectionStatusmessageToMCC("Error accessing service " + accessStatus.ToString());
                    NotifyStatusMessage?.Invoke("Error accessing service " + accessStatus.ToString(), 1);
                    return;
                }
            }
            catch (Exception ex)
            {
                SendConnectionStatusmessageToMCC("Exception " + ex.Message);
                NotifyStatusMessage?.Invoke("Exception " + ex.Message, 2);
                return;
            }
            //BloodPressureMeasurement

            foreach (GattCharacteristic c in characteristics)
            {
                string characteristicname = DisplayHelpers.GetCharacteristicName(c);
                if ((string.Compare(characteristicname, MainPage.TestresultModel.BPCharacteristicName, true) == 0))
                {
                    BloodPressureMeasurementCharacteristic = c;
                    break;
                }
            }

            if (BloodPressureMeasurementCharacteristic == null)
            {
                SendConnectionStatusmessageToMCC("BloodPressureMeasurement Characteristic not found.");
                NotifyStatusMessage?.Invoke("BloodPressureMeasurement Characteristic not found.", 1);
                return;
            }
            // BloodPressureMeasurementCharacteristic = selectedCharacteristic;
            var resultch = await BloodPressureMeasurementCharacteristic.GetDescriptorsAsync(BluetoothCacheMode.Uncached);

            if (resultch.Status != GattCommunicationStatus.Success)
            {
                SendConnectionStatusmessageToMCC("Descriptor read failure: " + resultch.Status.ToString());
                NotifyStatusMessage?.Invoke("Descriptor read failure: " + resultch.Status.ToString(), 1);
                return;
            }

            presentationFormat = null;
            if (BloodPressureMeasurementCharacteristic.PresentationFormats.Count > 0)
            {
                if (BloodPressureMeasurementCharacteristic.PresentationFormats.Count.Equals(1))
                {
                    // Get the presentation format since there's only one way of presenting it
                    presentationFormat = BloodPressureMeasurementCharacteristic.PresentationFormats[0];
                }
                else
                {
                    // It's difficult to figure out how to split up a characteristic and encode its different parts properly.
                    // In this case, we'll just encode the whole thing to a string to make it easy to print out.
                }
            }
            SubscribeBPMeasureService();
        }