/// <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); TODOBEHNAZ UUID = Characteristic.Uuid.ToString(); ReadValueAsync(); characteristic.ValueChanged += Characteristic_ValueChanged; PropertyChanged += ObservableGattCharacteristics_PropertyChanged; if (SPPoverLE_BUFFER_CHAR_UUID == UUID) { SetNotify(); } return; }
/// <summary> /// Connect to this bluetooth device /// </summary> /// <returns>Connection task</returns> public async Task <bool> Connect() { bool ret = false; string debugMsg = String.Format("~~~~Connect: "); Log.WriteLine(debugMsg + "Entering"); //Start checking requests Task.Run(() => ManageRequests()).AsAsyncAction(); RequestedCommands = new Queue <Request>(); RequestedCommands.Clear(); CurrentRequest = null; await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunTaskAsync(async() => { Log.WriteLine(debugMsg + "In UI thread"); try { if (BluetoothLEDevice == null) { Log.WriteLine(debugMsg + "Calling BluetoothLEDevice.FromIdAsync"); BluetoothLEDevice = await BluetoothLEDevice.FromIdAsync(DeviceInfo.Id); } else { Log.WriteLine(debugMsg + "Previously connected, not calling BluetoothLEDevice.FromIdAsync"); } if (BluetoothLEDevice == null) { ret = false; Log.WriteLine(debugMsg + "BluetoothLEDevice is null"); MessageDialog dialog = new MessageDialog("No permission to access device", "Connection error"); await dialog.ShowAsync(); } else { Log.WriteLine(debugMsg + "BluetoothLEDevice is " + BluetoothLEDevice.Name); // Setup our event handlers and view model properties BluetoothLEDevice.ConnectionStatusChanged += BluetoothLEDevice_ConnectionStatusChanged; BluetoothLEDevice.NameChanged += BluetoothLEDevice_NameChanged; IsPaired = DeviceInfo.Pairing.IsPaired; IsConnected = BluetoothLEDevice.ConnectionStatus == BluetoothConnectionStatus.Connected; Name = BluetoothLEDevice.Name; // Get all the services for this device CancellationTokenSource GetGattServicesAsyncTokenSource = new CancellationTokenSource(5000); var GetGattServicesAsyncTask = Task.Run(() => BluetoothLEDevice.GetGattServicesAsync(BluetoothCacheMode.Uncached), GetGattServicesAsyncTokenSource.Token); result = await GetGattServicesAsyncTask.Result; if (result.Status == GattCommunicationStatus.Success) { // In case we connected before, clear the service list and recreate it Services.Clear(); SPPOverLECharacteristic = null; Log.WriteLine(debugMsg + "GetGattServiceAsync SUCCESS"); foreach (var serv in result.Services) { ObservableGattDeviceService obsService = new ObservableGattDeviceService(serv); await obsService.GetAllCharacteristics(); Services.Add(obsService); } ServiceCount = Services.Count(); ret = true; } else if (result.Status == GattCommunicationStatus.ProtocolError) { ErrorText = debugMsg + "GetGattServiceAsync Error: Protocol Error - " + result.ProtocolError.Value; Log.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"; Log.WriteLine(ErrorText); string msg = "Device unreachable"; var messageDialog = new MessageDialog(msg, "Connection failures"); await messageDialog.ShowAsync(); } } } catch (Exception ex) { Log.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) { Log.WriteLine(debugMsg + "Exiting (0)"); } else { Log.WriteLine(debugMsg + "Exiting (-1)"); } return(ret); }