Java Keytool and Xamarin – Painful

Although there are more irritating things to have to wade through (a good example being nuget package dependency untangling, we’ve all been there. And if you haven’t yet, good for you), figuring out how to use the Java keytool is high on the list.

I found I needed it to get started using Google APIs for Xamarin.Form.Maps. Although the guide provided by Xamarin looks simple enough….

“Obtaining a Maps API v2 key involves the following steps:

  1. Retrieve the SHA1 fingerprint of the keystore that is used to sign the application.
  2. Create a project in the Google APIs console.
  3. Obtaining the API key.”

…do not be fooled, wary traveler. Retrieving the elusive SHA1 fingerprint is not as simple as you think. You fool, you.

And this is where the Keytool comes in. The Xamarin guide will explain that you need to use this tool to get your fingerprint from a keystore.

“By default the keystore that is used to sign debug versions of a Xamarin.Android application can be found at the following location:

  • WindowsC:\Users\[USERNAME]\AppData\Local\Xamarin\Mono for Android\debug.keystore
  • OSX/Users/[USERNAME]/.local/share/Xamarin/Mono for Android/debug.keystore

Information about a keystore is obtained by running the keytool command from the JDK. This tool is typically found in the Java bin directory:

  • OSX/System/Library/Java/JavaVirtualMachines/[VERSION].jdk/Contents/Home/bin/keytool
  • WindowsC:\Program Files (x86)\Java\jdk[VERSION]\bin\keytool.exe

 

Both of which are easy enough.

image1

Found you!

image2

And you too! Don’t think you can hide from me!

And here is where morale begins to ebb away. It closes down the moment you open it. Hmm.

It actually is working. Its just that you send it commands, and they run immediately, shutting down once done. That is why you’ll need to use the command prompt for it to stay open and you to see what the dang is going on. And the suggested route of:”keytool -list -v -keystore /Users/[USERNAME]/.local/share/Xamarin/Mono\ for\ Android/debug.keystore -alias androiddebugkey -storepass android -keypass android” does not work as easy as one would think.

Rather, the following steps will get it for you:

  1. Start Menu/Search Programs/Command Prompt
  2. Type and enter “cd C:\Program Files (x86)\Java\jre1.8.0_91\bin” (or the location of your computers Java keytool, if its a different version, which doesn’t matter at all)
  3. keytool -list -v -keystore “%USERPROFILE%\AppData\Local\Xamarin\Mono for Android/debug.keystore” -alias androiddebugkey -storepass android -keypass android
  4. Yay! I see it!
  5. Right click and select all, and then CTRL-C. Paste it all in a notepad, and you have you fingerprint!

image3

I have you now Skywalker….

It took me an hour and a half to figure this out. Now, I may be stoopid, but judging by how many stackoverflow SOS messages there seem to be on this topic, it is not so simple a quest.

Hopefully this makes it easier.

Now I’m off to go get my maps!

Advertisements

Debugging Xamarin through your Android device

Xamarin Player is fast to start, easy to setup, and at a price tag of free (the best price), is a great way to simulate and test your freshly minted Android apps. This could be why we have never really heard of any updates for it since, well….ever. It works great. Between development and the final product, however, you need to test in the real world. You need to test your app on a real device in the wild.

One reason to try do this quickly is that performance on an emulated phone and on a real phone is sometimes very different, and after emulating for so long, you might begin to get the nagging feeling that you are not seeing your app operate as it truly looks and feels. Sizes change, fonts change, animations run differently, colors on your computer screen are surprisingly different from a phone, and those PNGs you thought were high-res enough, in reality, are BUM UGLY.

But have no fear: if you’ve never done it before, its easier than you think.

Development Mode! New Badge Unlocked!

The first step is to get your phone into Development mode.

Android 3.2 – Application/Settings/Applications/Development and simply pressing a checkbox “USB debugging”. On anything less than Android 4.1, this entails going to settings, Development, and again, checking “USB debugging”.

Myeh. That was unsatisfyingly uncomplicated.

Ah, but Android 4.2 and above… well, its a delicious, almost spiritual experience.

Step one: sit down on a perfectly comfortable chair, in front of you a virgin, spotless table, preferably with soft natural light beaming in, in a cool room where you can feel the warmth of the light on your skin. Pop your knuckles and start breathe slowly. Think about each breath, imagine each molecule entering your lungs and being absorbed into your bloodstream with each breath. Clear your mind.

Step two: Place your Android Phone on the table, perpendicular to the edge, with a space of about two centimeters between the phone and the edge. This step is very important. Its actually not that important.

Step three: Open Settings.

Step four: Open About.

Step five: Scroll to Build Number. Are you still focusing on your breathing?

Step six: Look out the window, and ponder for a moment on the great legacy of men and women of science who have brought us to this point. This stage of history where, a man, a simple man, can in a world where nothing is surprising or special anymore, still perform the last true sorcery, can summon a magic that commands machines to begin thinking, that he can produce art, can build castles in the sky, can from code write beautiful poetry, can from nothing but the mind, make from nothing what cannot be touched, but is real and alive in every sense.

In a moment, you will be reborn as an app developer.

Step seven: Breathe again one last time. Tap Build Number 7 times.

nowdeveloper-100361671-large-idge
My phone said I’m a developer!

Magical.

Once you have come down again back to earth and realized that becoming an app developer is much easier said than done, you will notice a new panel has appeared in settings called Development, and in it you check “USB debugging”.

There are quite a few hardware and display options, “Show Touches”, “Show Layout Boundaries”, and “Show Pointer Location” being the most immediately interesting.

Screenshot_2016-05-18-14-31-28

 

 

 

 

 

 

 

 

 

 

Screenshot_2016-05-18-14-31-46

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Groovy.

You now should plug the phone into your computer with its USB cable.

Assuming Windows is in a good mood, the drivers should install themselves within a minute. If not, you might have to lookup what it takes for the particular phone you want to test, and download the correct drivers.

And if your computer is seeing your phone, Visual Studio sees it almost immediately. Making sure that the .Droid part of your project is set as the startup project, you are ready to go.

Debugging

When you Build your Project, it will compile, deploy the app to the phone (another way of saying load and install), and will begin debugging.

The App that refused to change its mind

One irritating problem that raised its ugly (but-very-small-and-hard-to-spot) head was that of redeploying properly. I would build and debug, make some changes, build again, and find no changes, repeat, furrow my eyebrows, repeat, question my ability, rebuild again, switch to the simulator, find my changes all working, feel a bit better but still wonder deep down if I shouldn’t have just stayed in electronics and English teaching, return to the phone and restart it, plug it in again, rebuild and see no changes, say something foul and wonder what the devil is going on.

Even forcing a deploy from the .Droid project would fail to make the stubborn little app change its point of view. Eventually I would uninstall the app manually each time I had finished debugging, and would then redeploy with changes when I built the solution. Non-ideal, to say the least.

The real solution involves opening the properties of the Android Project, and deselecting “Use Fast Deployment”.

debuggin2

It adds a terribly slow…6 seconds to the app startup time when you run off your phone. A veritable microcentury, but it will reliably make you changes in the app reflect themselves.

Ideally, this function should not work like this on most phones, so it is a bug of some kind. Just remember to switch it back when you use Xamarin Player or other simulators, as it will be needlessly slower each time.

For a new developer, getting your app running on your phone is an enormous confidence boost. It will also let you see and fix cosmetic issues you might not have seen on the simulator, and see things more from the point of view of the end user.

An important milestone towards launching your app into space – go try it out!

Light Sensor

The world's first autonomous giant-hand-shaped cloud sensing facility.
The world’s first autonomous giant-hand-shaped cloud sensing facility.

Here is my second assignment for the Interface With Arduino course I am doing on Coursera, the little CDS photocell light sensor.

“Build a circuit that lights an LED when it is sufficiently dark in a room. Demonstrate the circuit by covering the photoresistor to simulate darkness. ”

Here is the Dropbox link.

Please drop me line if the link is broken!

My lovely little circuit

Ah, if only our feelings could be controlled so easily...
Ah, if only our feelings could be controlled so easily…

Here is an assignment for the Interface With Arduino course I am doing on Coursera.

“Build a circuit that contains two pushbuttons, an LED, and any other basic components you think you need. The LED should only light when both pushbuttons are pressed. Only use your Arduino for power and ground. ”

Here is the Dropbox link.

I hope this works.

Please drop me line if the link is broken!

Learning Arduino on Coursera: a quick and simple way to start

For a long time, Coursera and other online learning sites didn’t offer much in the way of electronics courses, and there was nothing much related to embedded. That has changed, and now quite a few courses are not only available, but are self-paced, making them more attainable for busy people.

Its not as if the Arduino is new, and its also not as if there is a lack of material to learn how to use it – whether you buy a book, rely simply on the examples provided with the IDE, watch Youtube, get someone to teach you, build things from Instructables, all are good ways to learn Arduino.

The particular course I am wrapping up is “The Arduino Platform and C Programming”, and it can be found here. The professor (Ian Harris) who made the videos and material is funny, he explains clearly, and is easy to listen to.

Ian Harris explaining debugging
Ian Harris explaining debugging

The beauty with with these courses is that they have more structure, and they do include some details that someone teaching themselves might miss. For example, even amongst Hello Worlds of blinking LEDs that anyone who has had an Arduino for more than six months will find a bit stale, the code compile process is explained nicely, he answers the question of what code Arduino really uses, gives a quick primer in object orientation, and explains the danger of global variables.

(To which my father screamed in defiance: “Dip me in a tank of red ants! You will never take away our global variables! We will never stop it with the global variables! You’ll take them from my dead, ice-cold laptop!”)

Debug and serial are explained in depth in the fourth part, and to be honest, even after two years of messing around with Arduino, much of this was new to me, and I was very happy to have waded though blinking LEDs and other fiddly stuff up till that point.

Arduino Platform and C is one of several in a playlist of courses (a specialization), ending with a project and a more robust, accredited certificate. Other interesting ones I would like to do in the future are:

  • Introduction to the Internet of Things and Embedded Systems
  • Interfacing with the Arduino (quite nice – it introduces basic electronics in a very simple and clear way)
  • The Raspberry Pi Platform and Python Programming for the Raspberry Pi
  • Interfacing with the Raspberry Pi
  • Programming for the Internet of Things Capstone (the final project)

Specializations at Coursera are mostly paid, a nice little money-roller that sits and people slowly do. Whether this kind of certificate could get you a job in the first place is, in my opinion, debatable.  Taking online education may be of more common in the future, but difference in work opportunities between those with an expensive education and driven self-taught is still a large divide, and the world has wanted it that way for a long time.

If, though, you want to hone skills and learn new things, some of these courses are a good intro. Best of all: mostly free.

I would recommend them to anyone not sure about how to get started in Arduino and overwhelmed by the mass of information available.

Graph Arduino with Processing!

Having just started getting the feel for programming C/C++ with Arduino, I always wanted to give Processing a shot, being vaguely aware that Processing has the ability to catch serial from Arduino, and put it on a pretty graph. It’s just that Processing is Java, entirely new ground. And for many months the thought of starting on ….jAvA… just gave me the heebiddy jeebiddies. So I steamed on with C/C++

But! As I worked more with Arduino, I really wanted to graph something, and so the day finally came!

For reasons unclear to  me, the most basic Graph example (THE most official, and so one would think, THE easiest), did not work. As HAL would say “Well, I don’t think there is any question about it. It can only be attributable to human error.” So the answer as to why my graph fails to graph is obviously…stupidity. I am stupid and have probably made some programming mistake somewhere. And so have …missed the plot somewhere (did you see what I did there?).

But using a longer way around, I eventually did get a lovely graph happily graphing. The problem seemed to be that the original example used a SerialEvent () function instead of putting code in the draw () function. In this example, everything is in draw (), and it seems to run fine (credit to Eric Forman).

Getting a graph running from Arduino to a computer is an important thing to do, because it is the start of learning to catch data from sensors and send it places, store it, alert you about something, or even link with a cellphone app.

The circuit was some simple feed from two gas sensor, and MQ-3 alcohol sensor, and an MQ-2 flammable gas sensor (it too can smell alcohol).

Sniff sniff
You’re drunk programmer. Go home.

On the Arduino side:

/*
MQ-3 Alcohol Sensor
MQ-2 H2, LPG, CH4, CO, Alcohol, Smoke or Propane
MQ-135 NH3,NOx, alcohol, Benzene, smoke,CO2
MQ-8 Hydrogen Sensor

*/
long Sensor1Val;
long Sensor2Val;

void setup() {
Serial.begin(9600);
Sensor1Val = 0;
Sensor2Val = 0;
}
void loop() {
for(int x = 0 ; x < 150 ; x++)
{
Sensor1Val = Sensor1Val + analogRead(A0);
}
Sensor1Val = Sensor1Val/150.0;

for(int x = 0 ; x < 150 ; x++)
{
Sensor2Val = Sensor2Val + analogRead(A1);
}
Sensor2Val = Sensor2Val/150.0;

Serial.print(Sensor2Val);
Serial.print(“,”);
Serial.println(Sensor1Val);
//delay(1);

// delay in between reads for stability
}

 

I did see afterward that there was another way with Arduino using code originally designed for the Java, and it likely is better if you want to scale up and add more sensors (here).

And on the Processing side:

import processing.serial.*;

Serial myPort;

int numValues = 2; // number of input values or sensors

// rename these variables for your input:
float A = 0; // red value
int Amin = 0;
int Amax = 1023;

float B = 0; // green value
int Bmin = 0;
int Bmax = 1023;

float partH; // partial screen height

int xPos = 1; // horizontal position of the graph
void setup() {
size(800, 600);
partH = height / numValues; // to divide screen up to draw the number of sensors

println(Serial.list());// List all the available serial ports
myPort = new Serial(this, “COM4”, 9600); // don’t generate a serialEvent() unless you get a newline character:
myPort.bufferUntil(‘\n’);

background(0);
}

void draw() {
// in this example, everything happens inside serialEvent()
// but you can also do stuff in every frame if you wish
// get the ASCII string:
String inString = myPort.readStringUntil(‘\n’);
//println(inString); // < – uncomment this to debug serial input from Arduino

if (inString != null) {
// trim off any whitespace:
inString = trim(inString);
// split the string on the commas and convert the resulting substrings into an integer array:
int[] values = int(split(inString, “,”));
// if the array has at least the # of elements as your # of sensors, you know
// you got the whole data packet. Map the numbers and put into the variables:
if (values.length >= numValues) {
// map them to the range of partial screen height:
A = map(values[0], Amin, Amax, 0, partH);
// A = A*4;
B = map(values[1], Bmin, Bmax, 0, partH);
println(A + “\t” + B); // < – uncomment this to debug mapped values

// draw lines:
stroke(255, 0, 0); // red
line(xPos, partH, xPos, partH – A);
stroke(0, 255, 0); // green
line(xPos, partH*2, xPos, partH*2 – B);

// draw dividing lines:
stroke(255);
line(0, partH, width, partH);
line(0, partH*2, width, partH*2);

// if at the edge of the screen, go back to the beginning:
if (xPos >= width) {
xPos = 0;
background(0); // erase screen with black
} else {
xPos++; // increment the graph’s horizontal position
}
}
}
}

When you run, you get…

Nope. Nope. Nope. No...woah! I smell something! Is that soju?
Nope. Nope. Nope. No…woah! I smell something! Is that soju?

Lovely. Any ideas on different ways to get a graph going? Grid  lines would be great.

Arduino Guided Tour

Hum. Or maybe a walkthrough.

This is a very cute and easy top understand video on what an Arduino is, and what it contains (here). It is produced by the YouTube engineerzerochannel. They have several other videos, most of which are run-throughs of small, easy tp program Arduino functions and projects (autofade lamp… tea dunking machine..?)

“Remember – no matter how big or small, Arduinos are FUN!”