Monday, March 25, 2013

Configure Google Nexus 7 to work with Android SDK Tools / ADB.exe

I wanted to deploy my first “hello world” Android application from Eclipse on Windows 8 to my Google Nexus 7 tablet. I plugged the Nexus 7 into my development PC using the USB cable and then I used adb.exe to list the devices, but no devices were found.

The summary of steps I took to get the device to show with ADB:

  1. Enabled the USB debugging setting on the tablet
  2. Changed the USB connection mode to Camera (PTP)
  3. Updated the Google USB Driver in Windows

Initially Adb.exe shows no devices:

01-adb-shows-no-devices

If the Android emulator is running, it shows as the only device:

02-adb-shows-only-emulator

The first step is to enable the Developer options the Nexus 7. Go to Settings –> About tablet. Then tap the “Build number” 7 times!

02-tap-build-number-7-times

Now you will see the “Developer options” setting. Go into Developer options and enable the “USB debugging” setting:

02-enable-usb-debugging

When you get the “USB debugging connected” notification, tap the “Connected as a media device” option.

02-usb-connected

The default USB mode is “Media device (MTP)”, change this to “Camera (PTP)”:

02-select-camera-ptp-mode

Install the Google USB Driver using Eclipse / ADT: Window –> Android SDK Manager. In the Android SDK Manager, select Extras –> Google USB Driver. This installs the driver to your android SDK folder under sdk\extras\google\usb_driver.

Now update the device driver using Device Manager in Windows 8:

03-open-device-manager-win8

Find the “Nexus 7” device in the Device Manager under “Other devices”, then right click and select “Update Driver Software…”:

04-device-manager

Provide the path to the Google USB Driver:

05-select-driver

Select “Always trust software from Google Inc” and install the driver:

06-install-driver

Now the “Android Composite ADB Interface” will show in the Device Manager:

07-android-device-in-device-manager

Run ADB.exe again and confirm the new device is available. It may show as offline:

08-adb-shows-new-device

If the device shows as “offline”, then make sure to confirm on the tablet that you allow USB debugging:

08-allow-usb-debugging

Now the device is online:

09-nexus-online

That’s it!

I’m using Windows 8 64bit, Android Developer Tools (ADT) v21.1.0-569685 and the Nexus 7 is running Android 4.2.2. I used the information on this StackOverflow post to figure everything out.

Sunday, March 24, 2013

Asus Google Nexus 7 3G – Setting up O2 Ireland mobile broadband micro SIM

When I got my new Google Nexus 7 32GB/3G tablet, I immediately went to the O2 store in town and bought a prepay data Micro SIM for €20. (I actually first went to Carphone Warehouse, but they don’t have data-only Micro SIMs for O2). I went to the O2 website and activated the free 24 hour pass.

First thing I did was, I used the wee SIM ejection tool which was included with the Nexus, and plugged in the new SIM. Not too surprisingly, the mobile broadband didn’t just start working by itself. The Nexus showed the “H” mobile icon (HSPA) but the internet wasn’t working. I expected to receive an SMS text message from O2 to active the SIM, but nothing happened after waiting a few hours.

The next day, it occurred to me to plug the SIM into my iPhone 4S to see if I receive the activation SMS that way, but still nothing. I contacted O2 customer service (twice) and tried to explain my situation. Even though the second support person was completely unfamiliar with using the data SIM in a tablet, he did give me a good tip: a mobile data-only SIM doesn’t work at all in an iPhone, so even if the SIM had been activated, it still wouldn’t have worked in my iPhone, secondly, he reset the SIM completely, at which point I finally received the SIM activation code SMS on my phone.  Luckily that was all it took, I went to the O2 website and activated the SIM using the code I received in the SMS, and put the SIM back into the Nexus tablet. But still no internet on the tablet.

(As a side note, after the SIM was activated, my iPhone automatically sent an SMS to some +44 international number, which cost me a few cents, at which point my €20 prepaid credit was no longer enough to activate a 30-day pass, so I had to buy €20 more of credit, but that’s another story).

Next, it occurred to me that I probably need to configure the internet settings specifically for O2 on the Nexus tablet (DNS, O2 Ireland APN and that stuff). After a little bit of Googling, I found a few APN tips for other types of modems and after some trial and error, this is what worked for me.

Go to Settings. Under “WIRELESS & NETWORKS” select “More…”, then select “Mobile networks”:

01-network-settings

Under “Mobile network settings” select “Access Point Names”:

02-select-access-point-names

Now create a new APN by clicking the “three dots” icon and select “New APN”:

03-new-apn

Enter the following fields:

  • Name: O2.ie (or any name you like)
  • APN: open.internet

04-o2-ie-internet

After adding the new APN and returning back to the “APNs” setting screen, I believe I had to check the radio button next to the new “O2.ie Internet” APN setting to enable it.

And that’s it! Now your O2 mobile broadband will work on your Nexus 7.

Other Android 4.2 tips:

  • To take the screenshots above on the Nexus 7, I pressed the power button and the "volume down” button simultaneously for about a second or two.
  • If you’re a developer (or even just a curious to play with the Android SDK tools), you will want to enable the Developer options.

Monday, March 11, 2013

Cargo cult unit testing

Writing unit tests is really time consuming. We strive for 100% code coverage. I often spend more time writing unit tests than I did writing the functionality itself. (And yes, we write unit tests afterwards, we don't do strict TDD, but that's another story).

Sometimes I feel like my unit testing is just cargo cult behaviour. Am I just blindly following best practices like unit testing because the experts told me so?

Then occasionally I experience that happy moment when a old unit test catches a stupid mistake I just introduced into the code, and my faith is reaffirmed.

Overall I can definitely see an improvement in code quality over the crappy code I used to write years ago, so the tests are definitely helping, but I can't help but think sometimes if there isn't another (more productive) way to go?

I'm tempted to wonder: wouldn't higher level testing (integration testing) yield the same quality but at less cost? For example, isn't it sufficient to test the acceptance criteria using BDD style tests? I've experimented with SpecFlow previously and found it a very natural way to test. I’d like to give it a try again one day…

One example of the side effects of writing testable code is that you’re forced to keep the code cohesive. You really start thinking about coding principles and clean code. If you don’t keep the code clean (cohesive, loosely coupled), you’re going to struggle to write (and worse, maintain) those unit tests. Follow the SOLID principles (e.g. Single Responsibility principle) and your code is going to get so much easier to test. When you’re testing a class that has multiple responsibilities, e.g. a ViewModel (MVVM) in WPF/Silverlight, you easily end up multiplying test cases. For example, when you split a large ViewModel with multiple responsibilities into smaller sub-ViewModels, you’ll likely end up with a smaller number of test cases at the end.

If you don’t test everything, every scenario, you’ll loose track of what functionality is actually tested, and you will make a small change to the code, run all tests, the tests pass. But does that really mean that your new change is correct or not? You’ll become complacent and assume you can rely on the unit tests when it’s convenient, but you won’t really know if your code is stable. You need to maintain 100% coverage. As they say “test coverage starts with 100%”