07-01-2023 06:54 PM
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
07-08-2023 08:05 PM - edited 07-08-2023 08:09 PM
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
04-08-2024 10:36 AM
Hello, Did you solve the problem? I'm trying to get data with similar MAUI.