0
votes

I want to use "BluetoothLEAdvertisementWatcher" in WCF Service

public override void StartWathchingAsync()
        {
            BluetoothLEAdvertisementWatcher BluetoothWatcher = new BluetoothLEAdvertisementWatcher();
        }

But when I create instances of "BluetoothLEAdvertisementWatcher" I get the error below

Error Image

An unhandled exception of type 'System.ServiceModel.FaultException`1' occurred in mscorlib.dll

Additional information: The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))

An ExceptionDetail, likely created by IncludeExceptionDetailInFaults=true, whose value is: System.ArgumentException: The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG)) at MeasuringDeviceService.MeasuringDeviceClasseis.XiaomiMiScale.StartWathchingAsync() at MeasuringDeviceService.DeviceService.GetData(MeasuringDevice measuringDevice) in C:\Users\Programmer\Desktop\Project MahdKodak\MahdKodak\MeasuringDeviceService\DeviceService.svc.cs:line 37 at MeasuringDeviceService.DeviceService.GetDataXiaomiMiScale(XiaomiMiScale xiaomiMiScale) in C:\Users\Programmer\Desktop\Project MahdKodak\MahdKodak\MeasuringDeviceService\DeviceService.svc.cs:line 25 at SyncInvokeGetDataXiaomiMiScale(Object , Object[] , Object[] ) at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]& outputs) at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc& rpc) at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)

And when I comment the code line below, my program work well

//BluetoothLEAdvertisementWatcher BluetoothWatcher = new BluetoothLEAdvertisementWatcher();

EDIT :

This is my code

Service Code :

public class DeviceService : IDeviceService
    {
        Semaphore DeviceSemaphor = null;
        public DeviceService()
        {
            DeviceSemaphor = new Semaphore(1, 1);
        }
        public XiaomiMiScaleData GetDataXiaomiMiScale(XiaomiMiScale xiaomiMiScale)
        {
            return GetData(xiaomiMiScale) as XiaomiMiScaleData;
        }

        public MeasuringDeviceData GetData(MeasuringDevice measuringDevice)
        {
            try
            {
                measuringDevice.StartWathchingAsync();
                return measuringDevice.LastData;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
    }

Other Classes :

[DataContract]
    public class MeasuringDevice
    {
        [DataMember]
        public MeasuringDeviceData LastData { get; set; }
        public MeasuringDevice()
        {
        }
        [DataMember]
        public ulong DeviceBluetoothAddress { get; set; }
        public virtual void StartWathchingAsync()
        {

        }
    }

[DataContract]
    public class XiaomiMiScale : MeasuringDevice
    {
        public XiaomiMiScale()
        {
            LastData = new XiaomiMiScaleData();
        }

        public override void StartWathchingAsync()
        {
            BluetoothLEAdvertisementWatcher BluetoothWatcher = new BluetoothLEAdvertisementWatcher();
        }
    }
1
If your method has only one line of code that is causing exception, of course your method will work well when you comment the line. Have you checked if the BluetoothLEAdvertisementWatcher constructor requires a constructor parameter? Do you have access to the API or may be post the entire code?Ayusman

1 Answers

0
votes

The problem is either in MeasuringDeviceService.MeasuringDeviceClasseis.XiaomiMiScale.StartWathchingAsync() or somewhere in MeasuringDeviceService.DeviceService.GetData(MeasuringDevice measuringDevice) that's causing an exception to be thrown.

The reason why you see WCF in the stack is because the above code is being executed inside a WCF service and when the error occurs, the exception is being sent back to the client by means of a FaultException.

I don't know your code or system, but my first guess is you need to figure out what DeviceService.GetData() does and how to get it to work in a standalone manner.

EDIT: Try running BluetoothLEAdvertisementWatcher BluetoothWatcher = new BluetoothLEAdvertisementWatcher(); call in a standalone method outside the realm of WCF.

When you do so, does it work as expected? Can you put a breakpoint on your server side and see exactly what exception is being thrown and paste the callstack?