Exemplo n.º 1
0
        public async Task <ERROR_CODE> UnPairing(string deviceName)
        {
            ERROR_CODE task_result = ERROR_CODE.UNKNOWN_ERROR;

            if (string.IsNullOrEmpty(deviceName))
            {
                return(task_result);
            }

            var    devs    = _deviceList.OrderBy(d => d.Name).Where(d => !string.IsNullOrEmpty(d.Name)).ToList();
            string foundId = Utilities.GetIdByNameOrNumber(devs, deviceName);



            try
            {
                if (_selectedDevice == null)
                {
                    Console.WriteLine("Selected device is null");
                    task_result = ERROR_CODE.NO_SELECTED_SERVICE;
                    return(task_result);
                }
                if (_selectedDevice.ConnectionStatus == BluetoothConnectionStatus.Connected)
                //if (_selectedDevice.DeviceInformation.Pairing.IsPaired)
                {
                    Console.WriteLine($"{_selectedDevice.Name} Try Pairing");
                    var result1 = await _selectedDevice.DeviceInformation.Pairing.UnpairAsync();

                    task_result = ConvertErrorCodeUnPairing(result1.Status);
                    Console.WriteLine($"{result1.Status}");
                }
                else
                {
                    Console.WriteLine($"{_selectedDevice.Name} wasn't paired");
                    task_result = ERROR_CODE.UNPAIR_FAILED_DISCONNECTED;
                }
            }
            catch
            {
                task_result = ERROR_CODE.OPENDEVICE_UNREACHABLE;
            }

            _selectedService = null;
            _services.Clear();

            return(task_result);
        }
Exemplo n.º 2
0
        public async Task <ERROR_CODE> OpenDevice(string deviceName)
        {
            ERROR_CODE task_result = ERROR_CODE.NONE;

            if (string.IsNullOrEmpty(deviceName))
            {
                return(task_result);
            }

            var    devs    = _deviceList.OrderBy(d => d.Name).Where(d => !string.IsNullOrEmpty(d.Name)).ToList();
            string foundId = Utilities.GetIdByNameOrNumber(devs, deviceName);

            _selectedService = null;
            _services.Clear();

            try
            {
                _selectedDevice = await BluetoothLEDevice.FromIdAsync(foundId).AsTask().TimeoutAfter(_timeout);

                //string_result= $"Connecting to {_selectedDevice.Name}.";

                var result = await _selectedDevice.GetGattServicesAsync(BluetoothCacheMode.Uncached);

                if (result.Status == GattCommunicationStatus.Success)
                {
                    //    listStatus.Items.Adde($"Found {result.Services.Count} services:");

                    for (int i = 0; i < result.Services.Count; i++)
                    {
                        var serviceToDisplay = new BluetoothLEAttributeDisplay(result.Services[i]);
                        _services.Add(serviceToDisplay);
                        //        listStatus.Items.Add($"#{i:00}: {_services[i].Name}");
                    }
                }
                else
                {
                    //    listStatus.Items.Add($"Device {deviceName} is unreachable.");
                    task_result = ERROR_CODE.OPENDEVICE_UNREACHABLE;
                }
            }
            catch
            {
                task_result = ERROR_CODE.UNKNOWN_ERROR;
            }
            return(task_result);
        }
Exemplo n.º 3
0
        public async Task <ERROR_CODE> Pairing(string deviceName)
        {
            ERROR_CODE task_result = ERROR_CODE.UNKNOWN_ERROR;

            if (string.IsNullOrEmpty(deviceName))
            {
                return(task_result);
            }

            var    devs    = _deviceList.OrderBy(d => d.Name).Where(d => !string.IsNullOrEmpty(d.Name)).ToList();
            string foundId = Utilities.GetIdByNameOrNumber(devs, deviceName);

            _selectedService = null;
            _services.Clear();

            //try
            {
                _selectedDevice = await BluetoothLEDevice.FromIdAsync(foundId).AsTask().TimeoutAfter(_timeout);

                //string_result= $"Connecting to {_selectedDevice.Name}.";


                if (_selectedDevice.DeviceInformation.Pairing.IsPaired)
                {
                    var result1 = await _selectedDevice.DeviceInformation.Pairing.UnpairAsync();

                    task_result = ConvertErrorCodeUnPairing(result1.Status);
                    if (task_result != ERROR_CODE.UNPAIRED_SUCCESS)
                    {
                        Console.WriteLine($"{result1.Status}");
                        return(task_result);
                    }
                }

                if (_selectedDevice.ConnectionStatus == BluetoothConnectionStatus.Disconnected)
                {
                    Console.WriteLine($"{_selectedDevice.Name} Try Pairing");
                    _selectedDevice.DeviceInformation.Pairing.Custom.PairingRequested += CustomOnPairingRequested;

                    //var result1 = await _selectedDevice.DeviceInformation.Pairing.Custom.PairAsync(
                    //      DevicePairingKinds.ConfirmOnly, DevicePairingProtectionLevel.None);
                    var result1 = await _selectedDevice.DeviceInformation.Pairing.Custom.PairAsync(
                        DevicePairingKinds.ConfirmOnly);

                    _selectedDevice.DeviceInformation.Pairing.Custom.PairingRequested -= CustomOnPairingRequested;
                    task_result = ConvertErrorCodePairing(result1.Status);

                    Console.WriteLine($"{result1.Status}");
                    if (task_result != ERROR_CODE.PAIRING_SUCCESS)
                    {
                        return(task_result);
                    }
                }
                else
                {
                    task_result = ERROR_CODE.PAIRING_ALREADY_CONNECTED;
                    return(task_result);
                }

                var result = await _selectedDevice.GetGattServicesAsync(BluetoothCacheMode.Uncached);

                if (result.Status == GattCommunicationStatus.Success)
                {
                    //    listStatus.Items.Adde($"Found {result.Services.Count} services:");

                    for (int i = 0; i < result.Services.Count; i++)
                    {
                        var serviceToDisplay = new BluetoothLEAttributeDisplay(result.Services[i]);
                        _services.Add(serviceToDisplay);
                        //        listStatus.Items.Add($"#{i:00}: {_services[i].Name}");
                    }
                }
                else
                {
                    //    listStatus.Items.Add($"Device {deviceName} is unreachable.");
                    task_result = ERROR_CODE.OPENDEVICE_UNREACHABLE;
                }
            }
            //catch
            //{
            //    task_result = ERROR_CODE.UNKNOWN_ERROR;
            //}
            return(task_result);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Set active service for current device
        /// </summary>
        /// <param name="parameters"></param>
        public async Task <ERROR_CODE> SetService(string serviceName)
        {
            ERROR_CODE task_result = ERROR_CODE.NONE;

            if (_selectedDevice != null)
            {
                if (!string.IsNullOrEmpty(serviceName))
                {
                    string foundName = Utilities.GetIdByNameOrNumber(_services, serviceName);

                    // If device is found, connect to device and enumerate all services
                    if (!string.IsNullOrEmpty(foundName))
                    {
                        var attr = _services.FirstOrDefault(s => s.Name.Equals(foundName));
                        IReadOnlyList <GattCharacteristic> characteristics = new List <GattCharacteristic>();

                        try
                        {
                            // Ensure we have access to the device.
                            var accessStatus = await attr.service.RequestAccessAsync();

                            if (accessStatus == DeviceAccessStatus.Allowed)
                            {
                                // BT_Code: Get all the child characteristics of a service. Use the cache mode to specify uncached characterstics only
                                // and the new Async functions to get the characteristics of unpaired devices as well.
                                var result = await attr.service.GetCharacteristicsAsync(BluetoothCacheMode.Uncached);

                                if (result.Status == GattCommunicationStatus.Success)
                                {
                                    characteristics  = result.Characteristics;
                                    _selectedService = attr;
                                    _characteristics.Clear();
                                    Console.WriteLine($"Selected service {attr.Name}.");

                                    if (characteristics.Count > 0)
                                    {
                                        for (int i = 0; i < characteristics.Count; i++)
                                        {
                                            var charToDisplay = new BluetoothLEAttributeDisplay(characteristics[i]);
                                            _characteristics.Add(charToDisplay);
                                            Console.WriteLine($"#{i:00}: {charToDisplay.Name}\t{charToDisplay.Chars}");
                                        }
                                    }
                                    else
                                    {
                                        task_result = ERROR_CODE.SERVICE_NO_SERVICE;
                                    }
                                }
                                else
                                {
                                    task_result = ERROR_CODE.SERVICE_ACCESS_ERROR;
                                }
                            }
                            // Not granted access
                            else
                            {
                                task_result = ERROR_CODE.SERVICE_NOT_GRANT_ACCEWSS;
                            }
                        }
                        catch (Exception ex)
                        {
                            task_result = ERROR_CODE.SERVICE_CANOT_READ_CHAR;
                        }
                    }
                    else
                    {
                        task_result = ERROR_CODE.SERVICE_INVALID_SERVICE;
                    }
                }
                else
                {
                    task_result = ERROR_CODE.SERVICE_INVALID_SERVICE;
                }
            }
            else
            {
                task_result = ERROR_CODE.BLE_NO_CONNECTED;
            }

            return(task_result);
        }