Share via

connected Device Get Service always return null

Babu R 81 Reputation points
2024-04-25T08:22:03.87+00:00

I want to connect bluetooth device (JDY-23) for serial communication and my device is connecting and shows id also.

But when I tried to get service it always return null. Is there any problem with my code below

 var adapter = CrossBluetoothLE.Current.Adapter;
 var ble = CrossBluetoothLE.Current;
 adapter.ScanTimeout = 2000;
 adapter.ScanMode = Plugin.BLE.Abstractions.Contracts.ScanMode.LowPower;
 await adapter.StartScanningForDevicesAsync();
 if (adapter.DiscoveredDevices.Count == 0)
 {
     string text = "Unable to find Bluetooth";
     lbl.Text = text;
     return;
 }
 else
 {
     string text = "Found Devices";
     lbl.Text = text;
     IDevice device = adapter.DiscoveredDevices.Where(x => x.Name == "JDY-23").FirstOrDefault();
     if (device != null)
     {
         var connectedDevice = await adapter.ConnectToKnownDeviceAsync(device.Id);
         lbl_Device.Text = device.Name;
         var service = await connectedDevice.GetServiceAsync(device.Id);
         var characteristic = await service.GetCharacteristicAsync(device.Id);
         var bytes = await characteristic.ReadAsync();
         //await characteristic.WriteAsync();
     }
 }
Developer technologies | .NET | .NET Multi-platform App UI

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.