/// <summary>
        /// Initializes a new instance of the<see cref="ObservableGattCharacteristics" /> class.
        /// </summary>
        /// <param name="characteristic">Characteristic this class wraps</param>
        /// <param name="parent">The parent service that wraps this characteristic</param>
        public ObservableGattCharacteristics(GattCharacteristic characteristic, ObservableGattDeviceService parent)
        {
            Characteristic = characteristic;
            Parent         = parent;

            Name = GattCharacteristicUuidHelper.ConvertUuidToName(characteristic.Uuid);
            UUID = characteristic.Uuid.ToString();
        }
        private async Task <bool> GetAllPrimaryServices(BluetoothCacheMode cacheMode)
        {
            var    succeeded = false;
            string debugMsg  = String.Format("GetAllPrimaryServices: ");

            Debug.WriteLine(debugMsg + "Entering");

            // Get all the services for this device
            var result = await BluetoothLEDevice.GetGattServicesAsync(cacheMode);

            if (result.Status == GattCommunicationStatus.Success)
            {
                System.Diagnostics.Debug.WriteLine(debugMsg + "GetGattServiceAsync SUCCESS");

                lock (Services)
                {
                    foreach (var serv in result.Services)
                    {
                        if (!GattServiceUuidHelper.IsReserved(serv.Uuid))
                        {
                            var temp = new ObservableGattDeviceService(serv);
                            // This isn't awaited so that the user can disconnect while the services are still being enumerated
                            temp.Initialize();
                            Services.Add(temp);
                        }
                        else
                        {
                            serv.Dispose();
                        }
                    }
                    ServiceCount = Services.Count();
                }

                succeeded = true;
            }
            else if (result.Status == GattCommunicationStatus.ProtocolError)
            {
                ErrorText = debugMsg + "GetGattServiceAsync Error: Protocol Error - " + result.ProtocolError.Value;
                System.Diagnostics.Debug.WriteLine(ErrorText);
                string msg           = "Connection protocol error: " + result.ProtocolError.Value.ToString();
                var    messageDialog = new MessageDialog(msg, "Connection failures");
                await messageDialog.ShowAsync();
            }
            else if (result.Status == GattCommunicationStatus.Unreachable)
            {
                ErrorText = debugMsg + "GetGattServiceAsync Error: Unreachable";
                System.Diagnostics.Debug.WriteLine(ErrorText);
                string msg           = "Device unreachable";
                var    messageDialog = new MessageDialog(msg, "Connection failures");
                await messageDialog.ShowAsync();
            }

            return(succeeded);
        }
Example #3
0
 /// <summary>
 /// Initializes a new instance of the<see cref="ObservableGattCharacteristics" /> class.
 /// </summary>
 /// <param name="characteristic">Characteristic this class wraps</param>
 /// <param name="parent">The parent service that wraps this characteristic</param>
 public ObservableGattCharacteristics(GattCharacteristic characteristic, ObservableGattDeviceService parent)
 {
     Characteristic = characteristic;
     Parent         = parent;
     Name           = GattCharacteristicUuidHelper.ConvertUuidToName(Characteristic.Uuid);
     UUID           = Characteristic.Uuid.ToString();
     tempString     = "";
     ReadValueAsync();
     GetAllDescriptors();
     characteristic.ValueChanged += Characteristic_ValueChanged;
     PropertyChanged             += ObservableGattCharacteristics_PropertyChanged;
 }
Example #4
0
        /// <summary>
        /// Initializes a new instance of the<see cref="ObservableGattCharacteristics" /> class.
        /// </summary>
        /// <param name="characteristic">Characteristic this class wraps</param>
        /// <param name="parent">The parent service that wraps this characteristic</param>
        public ObservableGattCharacteristics(GattCharacteristic characteristic, ObservableGattDeviceService parent)
        {
            Characteristic = characteristic;
            Parent         = parent;
            Name           = GattUuidsService.ConvertUuidToName(Characteristic.Uuid);
            UUID           = Characteristic.Uuid.ToString();

            ReadValueAsync();

            characteristic.ValueChanged += Characteristic_ValueChanged;

            PropertyChanged += ObservableGattCharacteristics_PropertyChanged;

            return;
        }
Example #5
0
        /// <summary>
        /// Initializes a new instance of the<see cref="ObservableGattCharacteristics" /> class.
        /// </summary>
        /// <param name="characteristic">Characteristic this class wraps</param>
        /// <param name="parent">The parent service that wraps this characteristic</param>
        public ObservableGattCharacteristics(GattCharacteristic characteristic, ObservableGattDeviceService parent)
        {
            Characteristic = characteristic;
            Parent         = parent;
            Name           = GattCharacteristicUuidHelper.ConvertUuidToName(characteristic.Uuid);
            var shortId = BluetoothUuidHelper.TryGetShortId(characteristic.Uuid);

            if (shortId.HasValue)
            {
                ShortUUID = "0x" + shortId.Value.ToString("X");
            }
            else
            {
                ShortUUID = "";
            }
            UUID = characteristic.Uuid.ToString();
        }
        /// <summary>
        /// Connect to this bluetooth device
        /// </summary>
        /// <returns>Connection task</returns>
        public async Task <bool> Connect()
        {
            bool   ret      = false;
            string debugMsg = String.Format("Connect: ");

            Debug.WriteLine(debugMsg + "Entering");

            await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunTaskAsync(async() =>
            {
                Debug.WriteLine(debugMsg + "In UI thread");
                try
                {
                    if (bluetoothLEDevice == null)
                    {
                        Debug.WriteLine(debugMsg + "Calling BluetoothLEDevice.FromIdAsync");
                        BluetoothLEDevice = await BluetoothLEDevice.FromIdAsync(DeviceInfo.Id);

                        if (bluetoothLEDevice == null)
                        {
                            ret = false;
                            Debug.WriteLine(debugMsg + "BluetoothLEDevice is null");

                            MessageDialog dialog = new MessageDialog("No permission to access device", "Connection error");
                            await dialog.ShowAsync();
                            return;
                        }
                        else
                        {
                            // Setup our event handlers and view model properties
                            BluetoothLEDevice.ConnectionStatusChanged += BluetoothLEDevice_ConnectionStatusChanged;
                            BluetoothLEDevice.NameChanged             += BluetoothLEDevice_NameChanged;
                        }
                    }
                    else
                    {
                        Debug.WriteLine(debugMsg + "Previously connected, not calling BluetoothLEDevice.FromIdAsync");
                    }

                    Debug.WriteLine(debugMsg + "BluetoothLEDevice is " + BluetoothLEDevice.Name);

                    Name        = bluetoothLEDevice.Name;
                    CanPair     = DeviceInfo.Pairing.CanPair;
                    IsPaired    = DeviceInfo.Pairing.IsPaired;
                    IsConnected = BluetoothLEDevice.ConnectionStatus == BluetoothConnectionStatus.Connected;

                    UpdateSecureConnectionStatus();

                    // Get all the services for this device
                    CancellationTokenSource GetGattServicesAsyncTokenSource = new CancellationTokenSource(5000);

                    BluetoothCacheMode cacheMode = BluetoothLEExplorer.Services.SettingsServices.SettingsService.Instance.UseCaching ? BluetoothCacheMode.Cached : BluetoothCacheMode.Uncached;

                    // In case we connected before, clear the service list and recreate it
                    Services.Clear();

                    var GetGattServicesAsyncTask = Task.Run(() => BluetoothLEDevice.GetGattServicesAsync(cacheMode), GetGattServicesAsyncTokenSource.Token);

                    result = await GetGattServicesAsyncTask.Result;

                    if (result.Status == GattCommunicationStatus.Success)
                    {
                        System.Diagnostics.Debug.WriteLine(debugMsg + "GetGattServiceAsync SUCCESS");
                        foreach (var serv in result.Services)
                        {
                            if (!GattServiceUuidHelper.IsReserved(serv.Uuid))
                            {
                                ObservableGattDeviceService temp = new ObservableGattDeviceService(serv);
                                // This isn't awaited so that the user can disconnect while the services are still being enumerated
                                temp.Initialize();
                                Services.Add(temp);
                            }
                            else
                            {
                                serv.Dispose();
                            }
                        }

                        ServiceCount = Services.Count();
                        ret          = true;
                    }
                    else if (result.Status == GattCommunicationStatus.ProtocolError)
                    {
                        ErrorText = debugMsg + "GetGattServiceAsync Error: Protocol Error - " + result.ProtocolError.Value;
                        System.Diagnostics.Debug.WriteLine(ErrorText);
                        string msg        = "Connection protocol error: " + result.ProtocolError.Value.ToString();
                        var messageDialog = new MessageDialog(msg, "Connection failures");
                        await messageDialog.ShowAsync();
                    }
                    else if (result.Status == GattCommunicationStatus.Unreachable)
                    {
                        ErrorText = debugMsg + "GetGattServiceAsync Error: Unreachable";
                        System.Diagnostics.Debug.WriteLine(ErrorText);
                        string msg        = "Device unreachable";
                        var messageDialog = new MessageDialog(msg, "Connection failures");
                        await messageDialog.ShowAsync();
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(debugMsg + "Exception - " + ex.Message);
                    string msg = String.Format("Message:\n{0}\n\nInnerException:\n{1}\n\nStack:\n{2}", ex.Message, ex.InnerException, ex.StackTrace);

                    var messageDialog = new MessageDialog(msg, "Exception");
                    await messageDialog.ShowAsync();

                    // Debugger break here so we can catch unknown exceptions
                    Debugger.Break();
                }
            });

            if (ret)
            {
                Debug.WriteLine(debugMsg + "Exiting (0)");
            }
            else
            {
                Debug.WriteLine(debugMsg + "Exiting (-1)");
            }

            return(ret);
        }