package b4a.HC05LightDimmer;


import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.objects.ServiceHelper;
import anywheresoftware.b4a.debug.*;

public class starter extends  android.app.Service{
	public static class starter_BR extends android.content.BroadcastReceiver {

		@Override
		public void onReceive(android.content.Context context, android.content.Intent intent) {
            BA.LogInfo("** Receiver (starter) OnReceive **");
			android.content.Intent in = new android.content.Intent(context, starter.class);
			if (intent != null)
				in.putExtra("b4a_internal_intent", intent);
            ServiceHelper.StarterHelper.startServiceFromReceiver (context, in, true, BA.class);
		}

	}
    static starter mostCurrent;
	public static BA processBA;
    private ServiceHelper _service;
    public static Class<?> getObject() {
		return starter.class;
	}
	@Override
	public void onCreate() {
        super.onCreate();
        mostCurrent = this;
        if (processBA == null) {
		    processBA = new BA(this, null, null, "b4a.HC05LightDimmer", "b4a.HC05LightDimmer.starter");
            if (BA.isShellModeRuntimeCheck(processBA)) {
                processBA.raiseEvent2(null, true, "SHELL", false);
		    }
            try {
                Class.forName(BA.applicationContext.getPackageName() + ".main").getMethod("initializeProcessGlobals").invoke(null, null);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            processBA.loadHtSubs(this.getClass());
            ServiceHelper.init();
        }
        _service = new ServiceHelper(this);
        processBA.service = this;
        
        if (BA.isShellModeRuntimeCheck(processBA)) {
			processBA.raiseEvent2(null, true, "CREATE", true, "b4a.HC05LightDimmer.starter", processBA, _service, anywheresoftware.b4a.keywords.Common.Density);
		}
        if (!true && ServiceHelper.StarterHelper.startFromServiceCreate(processBA, false) == false) {
				
		}
		else {
            processBA.setActivityPaused(false);
            BA.LogInfo("*** Service (starter) Create ***");
            processBA.raiseEvent(null, "service_create");
        }
        processBA.runHook("oncreate", this, null);
        if (true) {
			ServiceHelper.StarterHelper.runWaitForLayouts();
		}
    }
		@Override
	public void onStart(android.content.Intent intent, int startId) {
		onStartCommand(intent, 0, 0);
    }
    @Override
    public int onStartCommand(final android.content.Intent intent, int flags, int startId) {
    	if (ServiceHelper.StarterHelper.onStartCommand(processBA, new Runnable() {
            public void run() {
                handleStart(intent);
            }}))
			;
		else {
			ServiceHelper.StarterHelper.addWaitForLayout (new Runnable() {
				public void run() {
                    processBA.setActivityPaused(false);
                    BA.LogInfo("** Service (starter) Create **");
                    processBA.raiseEvent(null, "service_create");
					handleStart(intent);
                    ServiceHelper.StarterHelper.removeWaitForLayout();
				}
			});
		}
        processBA.runHook("onstartcommand", this, new Object[] {intent, flags, startId});
		return android.app.Service.START_NOT_STICKY;
    }
    public void onTaskRemoved(android.content.Intent rootIntent) {
        super.onTaskRemoved(rootIntent);
        if (true)
            processBA.raiseEvent(null, "service_taskremoved");
            
    }
    private void handleStart(android.content.Intent intent) {
    	BA.LogInfo("** Service (starter) Start **");
    	java.lang.reflect.Method startEvent = processBA.htSubs.get("service_start");
    	if (startEvent != null) {
    		if (startEvent.getParameterTypes().length > 0) {
    			anywheresoftware.b4a.objects.IntentWrapper iw = ServiceHelper.StarterHelper.handleStartIntent(intent, _service, processBA);
    			processBA.raiseEvent(null, "service_start", iw);
    		}
    		else {
    			processBA.raiseEvent(null, "service_start");
    		}
    	}
    }
	
	@Override
	public void onDestroy() {
        super.onDestroy();
        if (true) {
            BA.LogInfo("** Service (starter) Destroy (ignored)**");
        }
        else {
            BA.LogInfo("** Service (starter) Destroy **");
		    processBA.raiseEvent(null, "service_destroy");
            processBA.service = null;
		    mostCurrent = null;
		    processBA.setActivityPaused(true);
            processBA.runHook("ondestroy", this, null);
        }
	}

@Override
	public android.os.IBinder onBind(android.content.Intent intent) {
		return null;
	}public anywheresoftware.b4a.keywords.Common __c = null;
public static anywheresoftware.b4a.objects.Serial _serial = null;
public static anywheresoftware.b4a.objects.Serial.BluetoothAdmin _admin = null;
public static b4a.HC05LightDimmer.asyncstreamstext _ast = null;
public static boolean _connected = false;
public static boolean _connecting = false;
public static boolean _trytoconnect = false;
public b4a.HC05LightDimmer.main _main = null;
public static String  _admin_devicefound(String _name,String _macaddress) throws Exception{
 //BA.debugLineNum = 26;BA.debugLine="Private Sub admin_DeviceFound (Name As String, Mac";
 //BA.debugLineNum = 27;BA.debugLine="Log($\"Device found: ${Name}\"$)";
anywheresoftware.b4a.keywords.Common.LogImpl("4917505",("Device found: "+anywheresoftware.b4a.keywords.Common.SmartStringFormatter("",(Object)(_name))+""),0);
 //BA.debugLineNum = 28;BA.debugLine="If Name = \"HC-05\" Then";
if ((_name).equals("HC-05")) { 
 //BA.debugLineNum = 29;BA.debugLine="Log(\"CancelDiscovery\")";
anywheresoftware.b4a.keywords.Common.LogImpl("4917507","CancelDiscovery",0);
 //BA.debugLineNum = 30;BA.debugLine="admin.CancelDiscovery";
_admin.CancelDiscovery();
 //BA.debugLineNum = 31;BA.debugLine="Log(\"Connect serial\")";
anywheresoftware.b4a.keywords.Common.LogImpl("4917509","Connect serial",0);
 //BA.debugLineNum = 32;BA.debugLine="Log(MacAddress)";
anywheresoftware.b4a.keywords.Common.LogImpl("4917510",_macaddress,0);
 //BA.debugLineNum = 33;BA.debugLine="serial.Connect(MacAddress)";
_serial.Connect(processBA,_macaddress);
 //BA.debugLineNum = 34;BA.debugLine="Log(\"Connected?\")";
anywheresoftware.b4a.keywords.Common.LogImpl("4917512","Connected?",0);
 //BA.debugLineNum = 35;BA.debugLine="TryToConnect = False";
_trytoconnect = anywheresoftware.b4a.keywords.Common.False;
 //BA.debugLineNum = 36;BA.debugLine="Connecting = True";
_connecting = anywheresoftware.b4a.keywords.Common.True;
 //BA.debugLineNum = 37;BA.debugLine="CallSub(Main, \"SetState\")";
anywheresoftware.b4a.keywords.Common.CallSubNew(processBA,(Object)(mostCurrent._main.getObject()),"SetState");
 };
 //BA.debugLineNum = 39;BA.debugLine="End Sub";
return "";
}
public static String  _admin_discoveryfinished() throws Exception{
 //BA.debugLineNum = 41;BA.debugLine="Private Sub admin_DiscoveryFinished";
 //BA.debugLineNum = 42;BA.debugLine="If Connecting = False Then";
if (_connecting==anywheresoftware.b4a.keywords.Common.False) { 
 //BA.debugLineNum = 43;BA.debugLine="TryToConnect = False";
_trytoconnect = anywheresoftware.b4a.keywords.Common.False;
 //BA.debugLineNum = 44;BA.debugLine="CallSub(Main, \"SetState\")";
anywheresoftware.b4a.keywords.Common.CallSubNew(processBA,(Object)(mostCurrent._main.getObject()),"SetState");
 };
 //BA.debugLineNum = 46;BA.debugLine="End Sub";
return "";
}
public static String  _admin_statechanged(int _newstate,int _oldstate) throws Exception{
 //BA.debugLineNum = 48;BA.debugLine="Private Sub admin_StateChanged (NewState As Int, O";
 //BA.debugLineNum = 49;BA.debugLine="Log(\"OldState \" &	OldState)";
anywheresoftware.b4a.keywords.Common.LogImpl("41048577","OldState "+BA.NumberToString(_oldstate),0);
 //BA.debugLineNum = 50;BA.debugLine="Log(\"NewState \" &	NewState)";
anywheresoftware.b4a.keywords.Common.LogImpl("41048578","NewState "+BA.NumberToString(_newstate),0);
 //BA.debugLineNum = 51;BA.debugLine="End Sub";
return "";
}
public static boolean  _application_error(anywheresoftware.b4a.objects.B4AException _error,String _stacktrace) throws Exception{
 //BA.debugLineNum = 84;BA.debugLine="Sub Application_Error (Error As Exception, StackTr";
 //BA.debugLineNum = 85;BA.debugLine="Return True";
if (true) return anywheresoftware.b4a.keywords.Common.True;
 //BA.debugLineNum = 86;BA.debugLine="End Sub";
return false;
}
public static String  _ast_newtext(String _text) throws Exception{
 //BA.debugLineNum = 70;BA.debugLine="Private Sub ast_NewText (Text As String)";
 //BA.debugLineNum = 71;BA.debugLine="CallSub2(Main, \"MessageFromDevice\", Text)";
anywheresoftware.b4a.keywords.Common.CallSubNew2(processBA,(Object)(mostCurrent._main.getObject()),"MessageFromDevice",(Object)(_text));
 //BA.debugLineNum = 72;BA.debugLine="End Sub";
return "";
}
public static String  _ast_terminated() throws Exception{
 //BA.debugLineNum = 74;BA.debugLine="Private Sub ast_Terminated";
 //BA.debugLineNum = 75;BA.debugLine="Connected = False";
_connected = anywheresoftware.b4a.keywords.Common.False;
 //BA.debugLineNum = 76;BA.debugLine="CallSub(Main, \"SetState\")";
anywheresoftware.b4a.keywords.Common.CallSubNew(processBA,(Object)(mostCurrent._main.getObject()),"SetState");
 //BA.debugLineNum = 77;BA.debugLine="End Sub";
return "";
}
public static String  _connect() throws Exception{
 //BA.debugLineNum = 20;BA.debugLine="Public Sub Connect";
 //BA.debugLineNum = 21;BA.debugLine="admin.StartDiscovery";
_admin.StartDiscovery();
 //BA.debugLineNum = 22;BA.debugLine="TryToConnect = True";
_trytoconnect = anywheresoftware.b4a.keywords.Common.True;
 //BA.debugLineNum = 23;BA.debugLine="CallSub(Main, \"SetState\")";
anywheresoftware.b4a.keywords.Common.CallSubNew(processBA,(Object)(mostCurrent._main.getObject()),"SetState");
 //BA.debugLineNum = 24;BA.debugLine="End Sub";
return "";
}
public static String  _process_globals() throws Exception{
 //BA.debugLineNum = 6;BA.debugLine="Sub Process_Globals";
 //BA.debugLineNum = 7;BA.debugLine="Private serial As Serial";
_serial = new anywheresoftware.b4a.objects.Serial();
 //BA.debugLineNum = 8;BA.debugLine="Public admin As BluetoothAdmin";
_admin = new anywheresoftware.b4a.objects.Serial.BluetoothAdmin();
 //BA.debugLineNum = 9;BA.debugLine="Private ast As AsyncStreamsText";
_ast = new b4a.HC05LightDimmer.asyncstreamstext();
 //BA.debugLineNum = 10;BA.debugLine="Public Connected = False As Boolean";
_connected = anywheresoftware.b4a.keywords.Common.False;
 //BA.debugLineNum = 11;BA.debugLine="Public Connecting = False As Boolean";
_connecting = anywheresoftware.b4a.keywords.Common.False;
 //BA.debugLineNum = 12;BA.debugLine="Public TryToConnect = False As Boolean";
_trytoconnect = anywheresoftware.b4a.keywords.Common.False;
 //BA.debugLineNum = 13;BA.debugLine="End Sub";
return "";
}
public static String  _sendmessage(byte[] _msg) throws Exception{
 //BA.debugLineNum = 66;BA.debugLine="Public Sub SendMessage(msg() As Byte)";
 //BA.debugLineNum = 67;BA.debugLine="ast.astreams.Write(msg) 'sending bytes, not text";
_ast._astreams /*anywheresoftware.b4a.randomaccessfile.AsyncStreams*/ .Write(_msg);
 //BA.debugLineNum = 68;BA.debugLine="End Sub";
return "";
}
public static String  _serial_connected(boolean _success) throws Exception{
 //BA.debugLineNum = 53;BA.debugLine="Private Sub Serial_Connected (Success As Boolean)";
 //BA.debugLineNum = 54;BA.debugLine="If Success Then";
if (_success) { 
 //BA.debugLineNum = 55;BA.debugLine="If ast.IsInitialized Then ast.Close";
if (_ast.IsInitialized /*boolean*/ ()) { 
_ast._close /*String*/ ();};
 //BA.debugLineNum = 56;BA.debugLine="ast.Initialize(Me, \"Ast\", serial.InputStream, se";
_ast._initialize /*String*/ (processBA,starter.getObject(),"Ast",(anywheresoftware.b4a.objects.streams.File.InputStreamWrapper) anywheresoftware.b4a.AbsObjectWrapper.ConvertToWrapper(new anywheresoftware.b4a.objects.streams.File.InputStreamWrapper(), (java.io.InputStream)(_serial.getInputStream())),(anywheresoftware.b4a.objects.streams.File.OutputStreamWrapper) anywheresoftware.b4a.AbsObjectWrapper.ConvertToWrapper(new anywheresoftware.b4a.objects.streams.File.OutputStreamWrapper(), (java.io.OutputStream)(_serial.getOutputStream())));
 //BA.debugLineNum = 57;BA.debugLine="Log(\"Connected\")";
anywheresoftware.b4a.keywords.Common.LogImpl("41114116","Connected",0);
 //BA.debugLineNum = 58;BA.debugLine="Connected = True";
_connected = anywheresoftware.b4a.keywords.Common.True;
 }else {
 //BA.debugLineNum = 60;BA.debugLine="Log(LastException)";
anywheresoftware.b4a.keywords.Common.LogImpl("41114119",BA.ObjectToString(anywheresoftware.b4a.keywords.Common.LastException(processBA)),0);
 };
 //BA.debugLineNum = 62;BA.debugLine="Connecting = False";
_connecting = anywheresoftware.b4a.keywords.Common.False;
 //BA.debugLineNum = 63;BA.debugLine="CallSub(Main, \"SetState\")";
anywheresoftware.b4a.keywords.Common.CallSubNew(processBA,(Object)(mostCurrent._main.getObject()),"SetState");
 //BA.debugLineNum = 64;BA.debugLine="End Sub";
return "";
}
public static String  _service_create() throws Exception{
 //BA.debugLineNum = 15;BA.debugLine="Sub Service_Create";
 //BA.debugLineNum = 16;BA.debugLine="serial.Initialize(\"serial\")";
_serial.Initialize("serial");
 //BA.debugLineNum = 17;BA.debugLine="admin.Initialize(\"admin\")";
_admin.Initialize(processBA,"admin");
 //BA.debugLineNum = 18;BA.debugLine="End Sub";
return "";
}
public static String  _service_destroy() throws Exception{
 //BA.debugLineNum = 88;BA.debugLine="Sub Service_Destroy";
 //BA.debugLineNum = 90;BA.debugLine="End Sub";
return "";
}
public static String  _service_start(anywheresoftware.b4a.objects.IntentWrapper _startingintent) throws Exception{
 //BA.debugLineNum = 79;BA.debugLine="Sub Service_Start (StartingIntent As Intent)";
 //BA.debugLineNum = 81;BA.debugLine="End Sub";
return "";
}
}
