protected override void OnCreate(Bundle savedInstanceState) { MessagingCenter.Subscribe <MainPage, int>(this, "dc_motor", (sender, e) => // this is going from MainPage to HERE (Board) { data = e; Output_To_Bluetooth(); }); TabLayoutResource = Resource.Layout.Tabbar; ToolbarResource = Resource.Layout.Toolbar; base.OnCreate(savedInstanceState); Xamarin.Essentials.Platform.Init(this, savedInstanceState); global::Xamarin.Forms.Forms.Init(this, savedInstanceState); BluetoothSocket _socket = null; { myConnection = new BluetoothConnection(); myConnection.getAdapter(); myConnection.thisAdapter.StartDiscovery(); try { myConnection.getDevice(); myConnection.thisDevice.SetPairingConfirmation(false); myConnection.thisDevice.SetPairingConfirmation(true); myConnection.thisDevice.CreateBond(); } catch (Exception) {} myConnection.thisAdapter.CancelDiscovery(); try { _socket = myConnection.thisDevice.CreateRfcommSocketToServiceRecord(Java.Util.UUID.FromString("00001101-0000-1000-8000-00805f9b34fb")); } catch (Exception) {} myConnection.thisSocket = _socket; try { myConnection.thisSocket.Connect(); } catch (Exception) {} }; LoadApplication(new App()); }
protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); SetContentView(Resource.Layout.Main); Button buttonConnect = FindViewById <Button>(Resource.Id.button1); Button buttonDisconnect = FindViewById <Button>(Resource.Id.button2); Button button1On = FindViewById <Button>(Resource.Id.button3); Button button2On = FindViewById <Button>(Resource.Id.button4); TextView connected = FindViewById <TextView>(Resource.Id.textView1); BluetoothSocket _socket = null; System.Threading.Thread listenThread = new System.Threading.Thread(listener); listenThread.Abort(); buttonConnect.Click += delegate { listenThread.Start(); myConnection = new BluetoothConnection(); myConnection.getAdapter(); myConnection.thisAdapter.StartDiscovery(); try { myConnection.getDevice(); myConnection.thisDevice.SetPairingConfirmation(false); myConnection.thisDevice.SetPairingConfirmation(true); myConnection.thisDevice.CreateBond(); } catch (Exception deviceEX) { } myConnection.thisAdapter.CancelDiscovery(); _socket = myConnection.thisDevice.CreateRfcommSocketToServiceRecord(Java.Util.UUID.FromString("00001101-0000-1000-8000-00805f9b34fb")); myConnection.thisSocket = _socket; try { myConnection.thisSocket.Connect(); connected.Text = "Connected!"; buttonDisconnect.Enabled = true; buttonConnect.Enabled = false; if (listenThread.IsAlive == false) { listenThread.Start(); } } catch (Exception CloseEX) { } }; buttonDisconnect.Click += delegate { try { buttonConnect.Enabled = true; listenThread.Abort(); myConnection.thisDevice.Dispose(); myConnection.thisSocket.OutputStream.WriteByte(187); myConnection.thisSocket.OutputStream.Close(); myConnection.thisSocket.Close(); myConnection = new BluetoothConnection(); _socket = null; connected.Text = "Disconnected!"; } catch { } }; button1On.Click += delegate { try { myConnection.thisSocket.OutputStream.WriteByte(1); myConnection.thisSocket.OutputStream.WriteByte(1); myConnection.thisSocket.OutputStream.WriteByte(1); myConnection.thisSocket.OutputStream.Close(); } catch (Exception outPutEX) { } }; button2On.Click += delegate { try { myConnection.thisSocket.OutputStream.WriteByte(2); myConnection.thisSocket.OutputStream.WriteByte(2); myConnection.thisSocket.OutputStream.WriteByte(2); myConnection.thisSocket.OutputStream.Close(); } catch (Exception outPutEX) { } }; void listener() { byte[] read = new byte[1]; TextView readTextView = FindViewById <TextView>(Resource.Id.textView2); TextView timeTextView = FindViewById <TextView>(Resource.Id.textView3); while (true) { try { myConnection.thisSocket.InputStream.Read(read, 0, 1); myConnection.thisSocket.InputStream.Close(); RunOnUiThread(() => { if (read[0] == 1) { readTextView.Text = "Relais AN"; } else if (read[0] == 0) { readTextView.Text = "Relais AUS"; timeTextView.Text = ""; } }); } catch { } } } }
protected override void OnCreate(Bundle bundle) { var obj = DependencyService.Get<PortableInterface>().GetLogicFromAndroidProject(); base.OnCreate(bundle); Xamarin.Forms.Forms.Init(this, bundle); //Haal de button uit de layout bron //verbind hieraan een event Arm ButtonUp = new Arm(); Button Bleuthootconnect = FindViewById<Button>(Bleutooth.ClassIdProperty(Bleuthootconnect)); Button BleuthootDisconnect = FindViewById<Button>(; Button ButtonOmhoog = FindViewById<Button>(Arm.ClassIdProperty(ButtonUp)); Button ButtonVooruit = FindViewById<Button>(Wielen.ClassIdProperty(ButtonFront)); Button ButtonAchteruit = FindViewById<Button>(Resource.Id.button4); Button ButtonRechts = FindViewById<Button>(Resource.Id.button5); Button ButtonLinks = FindViewById<Button>(Resource.Id.buttonPanel); BluetoothSocket _socket = null; System.Threading.Thread listenThread = new System.Threading.Thread(Listener); listenThread.Abort(); Bleuthootconnect.Click += delegate { //deze code heb ik van de volgende website:https://www.instructables.com/3-LED-Backlight-Xamarin-and-Arduino-With-HC05/ listenThread.Start(); myConnection = new BluetoothConnection(); myConnection.getAdapter(); myConnection.thisAdapter.StartDiscovery(); try { myConnection.getDevice(); myConnection.thisDevice.SetPairingConfirmation(false); myConnection.thisDevice.SetPairingConfirmation(true); myConnection.thisDevice.CreateBond(); } catch (Exception ex) { } myConnection.thisAdapter.CancelDiscovery(); _socket = myConnection.thisDevice.CreateRfcommSocketToServiceRecord(Java.Util.UUID.FromString("00001101-0000-1000-8000-00805f9b34fb")); myConnection.thisSocket = _socket; // System.Threading.Thread.Sleep(500); try { myConnection.thisSocket.Connect(); //deze kan later nog worden gemaakt voorlopig geeft dit nog een error. //Connected.Text = "Verbonden!"; BleuthootConnect.Enabled = true; BleuthootDisconnect.Enabled = false; if (listenThread.IsAlive == false) { listenThread.Start(); } } catch (Exception CloseEX) { } myConnection.thisSocket = _socket; try { myConnection.thisSocket.Connect(); Bleuthootconnect.Text = "verbonden met de arduino!!! kusjes Enrick xxx"; BleuthootDisconnect.Enabled = true; Bleuthootconnect.Enabled = false; if (listenThread.IsAlive == false) { listenThread.Start(); } } catch (Exception ex) { Console.WriteLine("connect to device", ex); } }; BleuthootDisconnect.Click += delegate { try { //is de connectie aan ja of nee? Bleuthootconnect.Enabled = true; myConnection.thisDevice.Dispose(); //dit moet 187 zijn want dit is de nummer om te verbinden met de bleuthoot module HC05. myConnection.thisSocket.OutputStream.WriteByte(187); myConnection.thisSocket.OutputStream.Close(); myConnection.thisSocket.Close(); myConnection = new BluetoothConnection(); _socket = null; Bleuthootconnect.Text = "Not connected to the Arduino!"; } catch { } }; ButtonVooruit.Click += delegate { try { //De nummer die moet worden doorgegeven voor vooruit de gaan is 4. myConnection.thisSocket.OutputStream.WriteByte(4); myConnection.thisSocket.OutputStream.Close(); } catch (Exception ex) { Console.WriteLine(ex.Message); } }; ButtonAchteruit.Click += delegate { try { //bij het nummer 3 wordt het commando achteruit rijden geactiveerd myConnection.thisSocket.OutputStream.WriteByte(3); myConnection.thisSocket.OutputStream.Close(); } catch (Exception) { throw; } }; ButtonLinks.Click += delegate { try { //bij het nummer 1 wordt het commando links rijden geactiveerd myConnection.thisSocket.OutputStream.WriteByte(1); myConnection.thisSocket.OutputStream.Close(); } catch (Exception) { throw; } }; ButtonOmhoog.Click += delegate { try { //bij deze 3 commando's gaat de arm helemaal naar boven. myConnection.thisSocket.OutputStream.WriteByte(8); myConnection.thisSocket.OutputStream.WriteByte(10); myConnection.thisSocket.OutputStream.WriteByte(12); myConnection.thisSocket.OutputStream.Close(); } catch (Exception) { throw; } }; }