Learn in 5 simple steps how Bluetooth and the External Accessory Framework play together to control a BeeWi car with Xamarin iOS.
If you are interested in doing so with Android, I have already covered this topic in Ein BeeWi Car mit Xamarin.Android steuern - unfortunately in German. Before I start, this is my first article I published in English, so please feel free and leave some feedback.
Open up Xamarin Studio and create a new iOS project by choosing the project template you want to use. I use and recommend the "Single View Application"-template from the Unified API iPhone folder. The new Unified API provides, besides some other things, 32bit and 64bit support, which Apple requires for all apps in February 2015.
Inside the newly created project you will find a Mainstoryboard.storyboard file. Double click that file brings the Xamarin.iOS UI-designer to the front. Design your user-interface like you wish, but don't forget that we will need to be able to send the following commands:
As you can see by looking at my user interface, I am not a designer.
By providing an interface that can be used to choose which command should be send to the BeeWi car, it is time to implement the communication between the toy and our device. Therefor iOS provides some APIs that you can choose from depending on what you need.
In this case we need the External Accessory Framework. An accessory represents a device, in this case the car, in iOS, which is paired with an iPad or iPhone. Because it is possible that multiple accessories are available, we have to find the one we want to use.
var connectedAccessories = EAAccessoryManager.SharedAccessoryManager.ConnectedAccessories;
EAAccessory beeWiCar = null;
foreach (var accessory in connectedAccessories)
{
foreach (var protocolString in accessory.ProtocolStrings)
{
protocolString.Contains("com.beewi.controlleur");
beeWiCar = accessory;
break;
}
}
Now that we have found the car, we need to open a communication session. Such a session is represented as EASession
in the External Accessory Framework that allows developers to retrieve or send data from and to the accessory. To start a session you have to provide the accessory and the protocol as parameters to the EASession-constructor. After that, you can open the stream to read and write data.
if (beeWiCar != null)
{
try
{
session = new EASession(beeWiCar, "com.beewi.controlleur");
session.Accessory.Disconnected += delegate
{
CloseSession();
new UIAlertView("BeeWi Car", "BeeWi car disconnected", null, "OK").Show();
};
session.InputStream.Schedule(NSRunLoop.Current, NSRunLoop.NSDefaultRunLoopMode);
session.InputStream.Open();
session.OutputStream.Schedule(NSRunLoop.Current, NSRunLoop.NSDefaultRunLoopMode);
session.OutputStream.Open();
}
catch (Exception ex)
{
new UIAlertView("BeeWi Car", "Ups something went wrong.", null, "OK").Show();
}
}
else
{
new UIAlertView("BeeWi Car", "No BeeWi car connected", null, "OK").Show();
}
Now that the connection to the BeeWi car is established, it is time to wire up the buttons like the following.
btnForward.TouchDown += delegate
{
SendCommand(BeeWiCarCommands.Forward_Go);
};
btnForward.TouchUpInside += delegate
{
SendCommand(BeeWiCarCommands.Forward_Stop);
};
Every time you press a button the command for that button will be send by the SendCommand
method. But we also need to send a signal that tells the car to stop doing whatever it does. Keep this in mind.
The SendCommand
method will check if the session is still active and the car is connected and then send the command.
void SendCommand(BeeWiCarCommands command)
{
try
{
if (session == null)
{
new UIAlertView("BeeWi Car", "No connection established", null, "OK").Show();
return;
}
if (!session.Accessory.Connected)
{
new UIAlertView("BeeWi Car", "Car not connected", null, "OK").Show();
return;
}
session.OutputStream.Write(new byte[]{ (byte)command }, 0, 1);
}
catch (Exception ex)
{
new UIAlertView("BeeWi Car", ex.Message, null, "OK").Show();
}
}
One last thing you have to do, to control the BeeWi car with your iPhone or iPad, is to register the com.beewi.controlleur
protocol with your app. To do this open the Info.plist file and change the tab to Source. In this window add a new entry and select "Supported external accessory protocols" and insert "com.beewi.controlleur".
Playing with this tiny little toy by controlling it with a self written app is really funny. And it is pretty easy using the External Accessory Framework to play with connected devices on iOS. This is also a good starting point to learn more about sensors. Imagine to control the car by touch or by moving your phone. But this should be part of another article.
The complete code can be found on bitbucket.
Als Mobile-Enthusiast und Geschäftsführer der Cayas Software GmbH ist es mir ein großes Anliegen, mein Team und unsere Kunden zu unterstützen, neue potenziale zu entdecken und gemeinsam zu wachsen. Hier schreibe ich vor allem zur Entwicklung von Android und iOS-Apps mit Xamarin und .NET MAUI.
Voice input makes it possible to intuitively record food eaten and drunk without having to look at a device or tap. Instead of laboriously entering everything by hand, users can simply record their meals and snacks by voice command. This approach can lower the inhibition threshold and encourage users to continuously document their eating habits. This saves time and encourages regular documentation.
Mit dem nahenden Ende des Supports für Xamarin im Mai 2024 sind Entwickler damit beschäftigt, bestehende Xamarin.Forms-Projekte auf .NET MAUI als Nachfolger zu migrieren. Das tun wir natürlich auch. In diesem Artikel zeige ich 7 Schritte, die wir während des Übergangs immer machen mussten, um Ihnen den Umstieg auf .NET MAUI zu erleichtern.
UI testing is an essential part of mobile app development to ensure that the app delivers the best possible user experience and meets the needs and expectations of its users. But how do we do that in .NET MAUI when Xamarin.UITest is not fully compatible anymore? Let's look at 3 alternatives to Xamarin.UITest.