Developer Portal Community

    cancel
    Showing results for 
    Search instead for 
    Did you mean: 

    .Net Maui and MtProtocol

    .Net Maui and MtProtocol

    CBSG
    New Poster

    Hi together,

    is there a workaround or an api for using MtProtocol in .Net Maui- Apps? I get a connection with GLM 50c but I am not able to receive data.

    Would be great to get help.

    Schöne Grüße

     

    Steve

    2 REPLIES 2

    CBSG
    New Poster

    Well the last week I tried to rebuilt the Android- App in .Net Maui with c#. I get a connection but I do not receive an answer for TurnAutoSyncOn(). I think the problem is in my GLMDeviceController. But I do not know where. Can anybody help?

     

    using Android.Content;
    using Android.Util;
    using Com.Bosch.Mtprotocol.Glm100C.Event;
    using VindorMobileApp.Platforms.Android.Bosch.bluetooth;
    using Com.Bosch.Mtprotocol;
    using Com.Bosch.Mtprotocol.Glm100C.Message.Laser;
    using Com.Bosch.Mtprotocol.Glm100C.Message.Sync;
    using Com.Bosch.Mtprotocol.Glm100C.Message.Edc;
    using Com.Bosch.Mtprotocol.Thermo.Message.Edct;
    using Com.Bosch.Mtprotocol.Glm100C;
    using static Com.Bosch.Mtprotocol.IMtProtocol;
    
    public class GLMDeviceController : Java.Lang.Object, MTProtocolEventObserver
    {
        private static readonly string TAG = "GLMDeviceController";
    
        private static readonly string ACTION_ERROR = "ERROR";
    
        public static readonly string ACTION_SYNC_CONTAINER_RECEIVED = "SYNC_CONTAINER_RECEIVED";
    
        public static readonly string ACTION_THERMAL_CONTAINER_RECEIVED = "THERMAL_CONTAINER_RECEIVED";
    
        public static readonly string EXTRA_MEASUREMENT = "MEASUREMENT";
    
        public Context context;
    
        public IMtProtocol protocol;
    
        public MTBluetoothDevice bluetoothDevice;
    
        public bool initSyncRequest;
    
        public bool ready;
        public bool disposedValue;
    
        public GLMDeviceController(Context context)
        {
            this.context = context;
        }
    
        /**
         * Test utility:
         * Use this method to turn the laser of connected GLM device on
         */
        public void TurnLaserOn()
        {
            if (IsReady())
            {
                ready = false;
                protocol.SendMessage(new LaserOnMessage());
            }
        }
    
        /**
         * Test utility:
         * Use this method to turn the laser of connected GLM device off
         */
        public void TurnLaserOff()
        {
            if (IsReady())
            {
                ready = false;
                protocol.SendMessage(new LaserOffMessage());
            }
        }
    
        /**
         * Starts sync mode between app and GLM device
         * When sync mode is started the GLM device will send every event to the app
         */
        private void TurnAutoSyncOn()
        {
            if (IsReady())
            {
                ready = false;
                if (bluetoothDevice != null)
                {
                    if (BluetoothUtils.ValidateGLM100Name(bluetoothDevice))
                    {
                        // GLM 100 device
                        SyncOutputMessage requestDoSync = new SyncOutputMessage();
                        requestDoSync.SyncControl = SyncOutputMessage.ModeAutosyncControlOn;
                        protocol.SendMessage(requestDoSync);
                        Log.Debug(TAG, "Sync started GLM 100...");
                    }
                    else if (BluetoothUtils.ValidateEDCDevice(bluetoothDevice))
                    {
                        // Exchange Data Container (EDC) based device
                        EDCOutputMessage requestEDCSync = new EDCOutputMessage();
                        requestEDCSync.SyncControl = EDCOutputMessage.ModeAutosyncControlOn;
                        requestEDCSync.DevMode = EDCOutputMessage.ReadOnlyMode;
                        protocol.SendMessage(requestEDCSync);
                        Console.WriteLine(TAG, "Sync started EDC device...");
                    }
                    else if (BluetoothUtils.ValidateGISName(bluetoothDevice))
                    {
                        // GIS device
                        EDCTOutputMessage requestEDCTSync = new EDCTOutputMessage();
                        requestEDCTSync.SyncControl = EDCTOutputMessage.ModeAutosyncControlOn;
                        requestEDCTSync.RemoteMode = EDCTOutputMessage.RemoteSetPingCycle;
                        requestEDCTSync.RemoteCtrlData = 20;
                        protocol.SendMessage(requestEDCTSync);
                        Log.Debug(TAG, "Sync started GIS device...");
                    }
                }
            }
        }
    
        /**
         * Receives MtProtocolEvents from MtProtocol layer and acts accordingly.
         * The MtProtocol layer will forward received MtProtocolEvents to its observers.
         * @param e the MtProtocolEvent received
         */
        void MTProtocolEventObserver.OnEvent(IMTProtocolEvent e)
        { 
            
            ready = true;
    
            if (e is MtProtocolFatalErrorEvent)
            {
                // fatal error
                Log.Debug(TAG, "Received MtProtocolFatalErrorEvent");
                protocol.Reset();
                context.SendBroadcast(new Intent(ACTION_ERROR));
            }
            else if (e is MtProtocolReceiveMessageEvent)
            {
                // received MT message -> act considering message type
                IMtMessage message = ((MtProtocolReceiveMessageEvent)e).Message;
                if (message is SyncInputMessage)
                {
                    // Sync Message Type used by GLM 100 C
                    SyncInputMessage syncMessage = (SyncInputMessage)message;
    
                    if (initSyncRequest)
                    {
                        // Ignore first response
                        initSyncRequest = false;
                        return;
                    }
                    Log.Debug(TAG, "SyncInputMessageReceived: " + syncMessage.ToString());
                    if (syncMessage.Mode == SyncInputMessage.MeasModeSingle && syncMessage.LaserOn == 0)
                    {
                        // Handle only distance measurements
                        BroadcastMeasurement(ACTION_SYNC_CONTAINER_RECEIVED, syncMessage.Result);
                    }
                }
                else if (message is EDCInputMessage)
                {
                    // Exchange Data Container (EDC) Message Type used by all other connected GLM devices
                    if (initSyncRequest)
                    {
                        // Ignore first response
                        initSyncRequest = false;
                        return;
                    }
                    Log.Debug(TAG, "Received EDC: " + message.ToString());
                    EDCInputMessage edcMessage = (EDCInputMessage)message;
                    Log.Debug(TAG, "EDCInputMessageReceived: " + edcMessage.ToString());
                    if (edcMessage.DevMode == EDCInputMessage.ModeSingleDistance || edcMessage.DevMode == EDCInputMessage.ModeContinuousDistance)
                    {
                        // Handle only distance measurements
                        BroadcastMeasurement(ACTION_SYNC_CONTAINER_RECEIVED, edcMessage.Result);
                    }
                }
                else if (message is EDCTInputMessage)
                {
                    // Exchange Data Container for Thermal device (EDCT) message type used by GIS 1000 C
                    if (initSyncRequest)
                    {
                        // Ignore first response
                        initSyncRequest = false;
                        return;
                    }
                    Log.Debug(TAG, "EDCT message received from " + bluetoothDevice.DisplayName);
                    HandleEDCTMessage((EDCTInputMessage)message);
                }
                else
                {
                    Log.Debug(TAG, "Receivedother message");
                }
            }
            else if (e is MtProtocolRequestTimeoutEvent)
            {
                // protocol timeout
                Log.Debug(TAG, "Received MtProtocolRequestTimeoutEvent");
                Intent intent = new Intent(ACTION_ERROR);
                context.SendBroadcast(intent);
            }
            else
            {
                Log.Error(TAG, "Received unknown event");
            }
            initSyncRequest = false;
        }
    
        private void BroadcastMeasurement(string action, float value)
        {
            Intent i = new Intent(action);
            i.PutExtra(EXTRA_MEASUREMENT, value);
            context.SendBroadcast(i);
        }
    
        private bool IsReady()
        {
            return protocol != null && ready;
        }
    
        public void Init(IMtConnection connection, MTBluetoothDevice btDevice)
        {
            Destroy();
    
            bluetoothDevice = btDevice;
    
            if (connection is BLEConnection)
            {
                // MirX based device
                throw new System.Exception("BLE Protokoll nicht implementiert");
                protocol = new MtProtocolImpl();
            }
            else
            {
                // PAN 1026 based device
                protocol = new MtProtocolImpl();
            }
            protocol.AddObserver(this);
            protocol.SetTimeout(5000);
            protocol.Initialize(connection);
    
            ready = true;
    
            initSyncRequest = true;
            TurnAutoSyncOn();
        }
    
        public void Destroy()
        {
            if (protocol != null)
            {
                protocol.RemoveObserver(this);
                protocol.Destroy();
                protocol = null;
            }
        }
    
        public MTBluetoothDevice GetBTDevice()
        {
            return bluetoothDevice;
        }
    
        private void HandleEDCTMessage(EDCTInputMessage edctMessage)
        {
            int packNum = edctMessage.PacketNum;
            Log.Debug(TAG, "EDCT Message Packet Number: " + packNum);
            if (packNum == EDCTInputMessage.PacketNum1)
            {
                // handle packet 1 only for this example
                BroadcastMeasurement(ACTION_THERMAL_CONTAINER_RECEIVED, edctMessage.Result);
            }
        }
    }

     

     

     

     Thanks a lot.

    Ciao Steve

    mcyenikoylu
    Occasional Visitor

    Hello, Did you solve the problem? I'm trying to get data with similar MAUI.

    Icon--AD-black-48x48Icon--address-consumer-data-black-48x48Icon--appointment-black-48x48Icon--back-left-black-48x48Icon--calendar-black-48x48Icon--center-alignedIcon--Checkbox-checkIcon--clock-black-48x48Icon--close-black-48x48Icon--compare-black-48x48Icon--confirmation-black-48x48Icon--dealer-details-black-48x48Icon--delete-black-48x48Icon--delivery-black-48x48Icon--down-black-48x48Icon--download-black-48x48Ic-OverlayAlertIcon--externallink-black-48x48Icon-Filledforward-right_adjustedIcon--grid-view-black-48x48IC_gd_Check-Circle170821_Icons_Community170823_Bosch_Icons170823_Bosch_Icons170821_Icons_CommunityIC-logout170821_Icons_Community170825_Bosch_Icons170821_Icons_CommunityIC-shopping-cart2170821_Icons_CommunityIC-upIC_UserIcon--imageIcon--info-i-black-48x48Icon--left-alignedIcon--Less-minimize-black-48x48Icon-FilledIcon--List-Check-grennIcon--List-Check-blackIcon--List-Cross-blackIcon--list-view-mobile-black-48x48Icon--list-view-black-48x48Icon--More-Maximize-black-48x48Icon--my-product-black-48x48Icon--newsletter-black-48x48Icon--payment-black-48x48Icon--print-black-48x48Icon--promotion-black-48x48Icon--registration-black-48x48Icon--Reset-black-48x48Icon--right-alignedshare-circle1Icon--share-black-48x48Icon--shopping-bag-black-48x48Icon-shopping-cartIcon--start-play-black-48x48Icon--store-locator-black-48x48Ic-OverlayAlertIcon--summary-black-48x48tumblrIcon-FilledvineIc-OverlayAlertwhishlist