I'm working on a Project (Console-Application .NET Framework 4.7.2 c#) to find nearby BLE devices and it's working well, the device(The device is just an Arduino with a BLE shield.) is discovered and i was able to read values from it. so the problem is that i wanted to do the same but on another PC (Same Code, Conditions, everything the same). it showed me that the pc is with the device connected but unreachable here on this place of the code :
GattDeviceServicesResult result = await bluetoothLeDevice.GetGattServicesAsync();
So i tried again and again on 3 different PCs and Laptops but it didn't work.
On the pc first i got connected to, it works well without problems. Can someone pls help me and say what is the matter. Here is my Code:
namespace QuickBlueToothLE
{
class Program
{
static public readonly Dictionary<string, DeviceInformation> mDiscoveredDevices = new Dictionary<string, DeviceInformation>();
static DeviceInformation device = null;
public static string HEG_Service_ID = "00001826-0000-1000-8000-00805f9b34fb";
public static DeviceWatcher deviceWatcher = null;
// Abfrage nach zusätzlichen Eigenschaften, die zurückgegeben werden sollen
public static string[] requestedProperties =
{
"System.Devices.Aep.DeviceAddress",
"System.Devices.Aep.IsConnected",
"System.Devices.Aep.Bluetooth.Le.IsConnectable"
};
static async Task Main(string[] args)
{
deviceWatcher = DeviceInformation.CreateWatcher
(BluetoothLEDevice.GetDeviceSelectorFromPairingState(false),
requestedProperties,
DeviceInformationKind.AssociationEndpoint);
// Register event handlers before starting the watcher.
// Added, Updated and Removed are required to get all nearby devices
deviceWatcher.Added += DeviceWatcher_Added;
deviceWatcher.Updated += DeviceWatcher_Updated;
deviceWatcher.Removed += DeviceWatcher_Removed;
// EnumerationCompleted and Stopped are optional to implement.
deviceWatcher.EnumerationCompleted += DeviceWatcher_EnumerationCompletedAsync;
deviceWatcher.Stopped += DeviceWatcher_Stopped;
// Start the watcher.
deviceWatcher.Start();
while (true)
{
if (device == null)
Thread.Sleep(2000);
else
{
Console.WriteLine("Press Any Key to connect to HEG-900725");
Console.ReadKey();
BluetoothLEDevice bluetoothLeDevice = await BluetoothLEDevice.FromIdAsync(device.Id);
Console.WriteLine("Attempting to connect with device");
GattDeviceServicesResult result = await bluetoothLeDevice.GetGattServicesAsync();
var services = result.Services;
if (result.Status == GattCommunicationStatus.Success)
{
Console.WriteLine("Connection succeeded" + " "+ bluetoothLeDevice.DeviceInformation.Pairing.IsPaired);
Console.WriteLine(device.Id);
foreach (var service in services)
{
if (service.Uuid.ToString() == HEG_Service_ID)
{
Console.WriteLine("Found Heg Service");
GattCharacteristicsResult charactiristicResult = await service.GetCharacteristicsAsync();
if (charactiristicResult.Status == GattCommunicationStatus.Success)
{
var characteristics = charactiristicResult.Characteristics;
foreach (var characteristic in characteristics)
{
Console.WriteLine("---------------");
GattCharacteristicProperties properties = characteristic.CharacteristicProperties;
Console.WriteLine("CharactesticHandle:"+characteristic.AttributeHandle +"\r\n"+"UUID"+characteristic.Uuid);
if (properties.HasFlag(GattCharacteristicProperties.Notify))
{
Console.WriteLine("Notify poroperty found");
GattCommunicationStatus status =
await characteristic.WriteClientCharacteristicConfigurationDescriptorAsync(
GattClientCharacteristicConfigurationDescriptorValue.Notify);
if (status == GattCommunicationStatus.Success)
{
characteristic.ValueChanged += Characteristic_ValueChanged;
// Server has been informed of clients interest.
}
}
}
}
}
}
}
Console.WriteLine("Press Any Key to Exit application");
Console.WriteLine("\r\n" + "Values" + "\r\n");
Console.ReadKey();
foreach (var service in services)
{
service.Dispose();
}
bluetoothLeDevice.Dispose();
bluetoothLeDevice = null;
device = null;
Console.WriteLine(mDiscoveredDevices);
Console.WriteLine(mDiscoveredDevices.Count());
break;
}
}
Console.ReadKey();
deviceWatcher.Stop();
deviceWatcher = null;
}
private static void TXCharacteristic_ValueChanged(GattCharacteristic sender, GattValueChangedEventArgs args)
{
byte[] buffer = args.CharacteristicValue.ToArray();
}
private static void Characteristic_ValueChanged(GattCharacteristic sender, GattValueChangedEventArgs args)
{
var reader = DataReader.FromBuffer(args.CharacteristicValue);
var flags = reader.ReadByte();
var value = reader.ReadByte();
Console.WriteLine($"{flags} - {value}");
}
private static void DeviceWatcher_Stopped(DeviceWatcher sender, object args)
{
}
private static void DeviceWatcher_EnumerationCompletedAsync(DeviceWatcher sender, object args)
{
}
private static void DeviceWatcher_Removed(DeviceWatcher sender, DeviceInformationUpdate args)
{
}
private static void DeviceWatcher_Updated(DeviceWatcher sender, DeviceInformationUpdate args)
{
//throw new NotImplementedException();
}
private static void DeviceWatcher_Added(DeviceWatcher sender, DeviceInformation args)
{
Console.WriteLine(args.Name);
if (args.Name == "HEG_100053" || args.Name =="Arduino")
{
Console.WriteLine("Found: " + args.Name);
device = args;
bool keyExists = mDiscoveredDevices.ContainsKey(device.Id);
if (!keyExists)
{
mDiscoveredDevices.Add(device.Id, device);
}
}
}
//900725
//|| args.Name == "HEG_100053"
}
}