Saturday, March 14, 2015

How to use Classic Bluetooth on iOS

Something like classic Bluetooth sounds like it should be simple enough to use on iOS. I recently falsely stated is was all there and ready to go in the Core Bluetooth APIs. Although Bluetooth LE is fully accessible, classic Bluetooth is not. But there are a few options developers can explore if they need to use Classic Bluetooth within an application.

The first thing to note is, pairing with a Classic Bluetooth device is handled at the OS level, so there isn’t any way we can start the connection or pairing process from within an app.

So assuming a device is already paired, we have a few options:


External Accessory Framework

The External Accessory Framework is used to communicate with devices that are Apple MFi certified. MFi stands for “Made For i(phone/pad/pod)”. These devices can be physically connected, or connected over Bluetooth. If your device is MFi certified, this is the way to go.

As developers, you’re not required to join the MFi Program, that’s only necessary if you intend to develop hardware.

And of course, the External Accessory Framework APIs will not work with non-MFi devices.


Bluetooth Manager Framework

The Bluetooth Manager Framework sounds like the perfect solution to communicate with non-MFi Bluetooth Devices. Unfortunately, it’s a private API, meaning if you want to get your app published on the App Store, it can’t be used.

However, it can do things like scan for local devices, manage connections, and communicate with paired devices. Because it’s a private API, you won’t find any official Apple documentation but a quick Google search should get you going. There’s a good Objective-C tutorial here:



If you’re looking to connect two iOS devices, GameKit has a straight forward solution. It’s a simple matter of starting up GameKit, searching for local devices, connect, and then send/receive data. However, this is only for iOS device-to-device communication:



If you’re willing to Jailbreak your device and write applications that will never make it onto the App Store, you can make use of BTstack. The packages are available on Cydia. I’ve personally never used it but you can find more information here:


Bluetooth 4.0

And finally, if your hardware is Bluetooth 4.0 compatible, you should probably consider moving away from Bluetooth Classic and using Bluetooth LE. Although BLE supports “casual” connections, it can also pair devices and establish secure connections.

This is also the path that Apple recommends, it’s well documented, stable on devices made in the last 2-3 years (iPhone 5 and up, iPad 3 and up), and fully defined in the CoreBluetooth APIs:

Monday, March 2, 2015

Using the Dell Venue 8-7000 for Android Development



I’ve been lucky enough to get my hands on a shiny new Dell Venue 8-7000 - and it’s a fantastic tablet. I’m slightly sad to say it’s going to be replacing my trusty Lenovo S8-50 tab. It’s been a fantastic device but you just can’t argue with the extra performance, or the 2560x1600 screen on the Dell. But this isn’t meant to be a review or a comparison. If you’re looking for a quick opinion, I’d absolutely recommend either tablet. The Venue 8-7000 is a beautiful high-end device, and the Lenovo S8-50 is shockingly good at its price point.

But the point of this post is to get to work (developing) on the Dell Venue 8-7000. Something that surprisingly doesn’t work “out of the box” on Windows.


The Problem? Drivers

To use an Android device for debugging on Windows, you need to have the correct drivers installed.  For Nexus devices, you can simply download the “Google USB Driver” via the Android SDK manager. And occasionally, such as with most Samsung Android devices, you need to download drivers directly from the developer’s website.

Unfortunately the Google USB Drivers don’t work, and when searching Dell’s website for drivers or downloads, there aren’t any.



The Fix? Hack the Google Drivers

Before I go on, there is a warning. You’ll need to configure your Windows system to allow unsigned drivers to be installed. This is because the checksum for the driver is no longer going to be correct after we edit it. I’m not going to cover that here, but a simple Google search will take you through the steps if you’re running Windows 8.1.

Step 1 – obtain a copy of the Google USB Drivers

If you’ve downloaded them already using the SDK manager, simply navigate to your installed location. If you’re a Xamarin developer it’s typically c:\Users\[user name]\AppData\Local\Android\android-sdk\extras\google\usb_driver. Remember that the “Local” folder is hidden by default.

Alternatively, you can download the driver directly from here:

Copy this folder to another location since we’re going to modify it.


Step 2 – Enable Developer Options and USB Debugging

  1. Open Settings on the Venue 8
  2. Scroll to the bottom and click “About tablet”
  3. Tap on the Build number 7 times (to enable the Developer options)
  4. Go back one screen to Settings – you should now see “Developer options” above “About tablet”
  5. Open Developer options and check “USB Debugging”  
Step 3 – Configure the Dell Venue 8-7000
  1. Open Settings
  2. Select “Storage”
  3. Tap on the context menu in the upper right corner (three vertical dots)
  4. Select “USB computer connection” (it’s likely the only option
  5. Change the “CONNECT AS” option to “Camera (PTP)”
Step 4 – Get your Dell Venue 8’s device IDs
  1. Plug in your Dell Tablet to your Windows PC
  2. Open the Control Panel
  3. Open the Device Manager
  4. Expand “Other devices” and select “Android”
  5. On the Details tab, change the property to Hardware Ids and save them both
  6. Repeat steps 4 & 5 for the “MTP” entry under “Other devices” and save/record the IDs




Step 5 – Modify the USB Drivers

  1. Open the location of your saved drivers
  2. Open “android_winusb.inf” with your favourite text editor
  3. Find the [Google.NTx86] section
  4. Add the two “Android” ids as %CompositeAdbInterface% entries
  5. Add the “MTP” ids as %SingleBootLoaderInterface% and %SingleAdbInterface%
  6. Scroll down to the [Google.NTamd64] and add the same four entries
  7. Save the inf file

It should look something like this:

;Dell Venue 8 7840
%CompositeAdbInterface% = USB_Install, USB\VID_8087&PID_0A5D&REV_FFFF
%CompositeAdbInterface% = USB_Install, USB\VID_8087&PID_0A5D
%SingleBootLoaderInterface% = USB_Install, USB\VID_413C&PID_B14D&MI_01
%SingleAdbInterface% = USB_Install, USB\VID_413C&PID_B14D&REV_FFFF&MI_01

Step 6 – Update the Drivers

  1. Open the Device Manager again on your PC with Tablet plugged in
  2. Right-click on the “Android” entry under “Other devices” and select “Update Driver Software”
  3. Browse to your modified driver and install
  4. Repeat for the “MDP” entry

*Remember – you must be able to install unsigned drivers


Step 7 – Run and Debug apps on your new tablet

You’ll now see two new entries in the device manager. Under “Portable Devices” you should see something like “Venue 8 7840” (the specific number will vary based on the model). You’ll also see an “Android ADB interface” entry, on my system it’s listed under “SAMSUNG Android Phone”.


If everything worked you should be ready to develop! My Venue 8-7000 is showing up correctly in Visual Studio 2013, Xamarin Studio for Windows, and Android Studio. 


Monday, February 9, 2015

Android SDK Manager Setup for Xamarin.Android and Xamarin Forms Development

Setting up the Android the SDK can be a bit overwhelming if you’re new to Android development. Especially compared to other mobile platforms like Windows Phone and iOS, where you just install the latest versions and start developing.
My Goal with this article is to clarify the purpose of the installable packages in the SDK manager as well as give you some guidance as to what you should install when developing Xamarin Apps.
If you don't want to read through all of the details, jump to the bottom to view my configuration.
More info:



The Xamarin installer will download and install all of the tooling you need to develop Xamarin.Android applications. And this includes the SDK Manager. You shouldn’t need to install the Android SDKs manually, but if you do you can check out Android’s documentation here:


How to Launch the SDK Manager

As Xamarin developers, the easiest way to open to Android SDK Manager is through your favorite IDE. Both Visual Studio and Xamarin Studio have launch points in their menus.
In Visual Studio, click on “Tools -> Android -> Android SDK Manager …”
In Xamarin Studio, click “Tools -> Open Android SDK Manager …”


Android SDK Manager Packages

The Android SDK Manager allows you to select which packages you want to install. I’ll discuss each option, working from the top to the bottom, and give you recommendations for what to install.



Let’s start in the Tools section.


Android SDK Tools

The first option you’ll see is the Android SDK Tools. These are the development and debugging tools for the Android SDK and you’ll need these installed in order to compile Xamarin.Android Applications. Install this and keep it up to date.
Recommendation: must install
More info:


Android SDK Platform-tools

The Platform tools contain additional tools for connecting to devices and emulators to deploy builds and debug applications. You should always have the latest version of the platform tools to ensure compatibility with the latest version of the Android APIs; so keep this up to date as well.
Recommendation: must install


Android SDK Build-tools

The Build Tools are required for actually building/compiling your source code into an application that can be run on the Android platform. You should AWLAYS install the latest version which will be used by default during compilation. If your project depends on an older version, the SDK Manager allows you to install and maintain separate versions. In my experience this is rarely necessary and I only install the latest version.
Recommendation: install latest version only
More info:


Android APIs

After the Tools section you’ll see sections for each Android API. Typically when a new version of the Android operating system is released, Google also updates the APIs which allow you to develop with the latest features and fixes. You’ll see the Android version listed (i.e. 5.0.1) along with the API level (API 21). There are a lot of options here, let’s look at each one and then discuss which version(s) of the API to install.


Documentation for Android SDK

There is documentation for each API level and this is written for Java Android developers. It can be installed but it’s not often used by Xamarin developers, and it’s not required.
Recommendation: don’t install


SDK Platform

The SDK Platform for a given API level allows you to compile against that version of Android. Newer platforms require newer versions of the SDK Platform-tools and SDK Tools (mentioned above) which is why we need to keep those up to date.
Recommendation: must install


Samples for SDK

These are Java samples included with each API level. These are not required and Xamarin developers don’t typically use these, but they can be examined and ported to C#.
Recommendation: don’t install unless you have a specific use or lots of storage space


System Images

These are system images that can be used with Android virtual devices. On later API levels, both ARM and x86 images are included. The x86 images will run significantly faster and should be preferred over the ARM equivalents. The images prefixed with “Google APIs” include the Google Play Services runtime and are useful for testing Google Play Services features such as mapping. These versions are recommended over the “plain” versions. Note – the System Images take up a significant amount of disk space so you should only install them if you’re using them.
Recommendation: don’t install if you’re using the Xamarin Android Player or Genymotion. Otherwise, install x86 Google API images as needed.


Google APIs

Most API levels include a package called Google APIs. This includes additional documentation, samples and libraries for the Google APIs for that version of Android for Java developers. These libraries are not needed if you’re using Xamarin even if coding against the Google APIs.
Recommendation: don’t install


Sources for Android SDK

For some API levels Google has released the source code. Unless you’re specifically interested in viewing the source code for the Android SDK, I wouldn’t recommend installing it, and it’s not needed for application development.
Recommendation: don’t install


*API Levels

Now that we understand what the individual pieces do, you’re probably asking, “which API levels do I install?”. In an ideal world you would just install the latest version, since you can still target older versions of Android with the latest SDK. However, APIs do change between versions and with that, you may find example code or example projects that are designed for, and targeting older versions. The good news is, each SDK Platform only uses about 100 megabytes of space. So for simplicity, you can safely install every SDK Platform (which will consumes ~2GB of space).
Recommendation: install each SDK Platform from API level 14 to the latest. If space allows, install every SDK Platform.



Our final section is Extras. The packages in this section include extra functionality and tools you can optionally use during development. I won’t cover all the packages, just the ones I recommend you install.


Android Support Library

The Android Support Library is a package that provides backwards compatibility for new API features on older devices. Additionally, Google releases some features and controls that are ONLY available in the support libraries. It’s considered “best practice” to use the Support Libraries for some features during development.
Recommendation: highly recommended to install
More info:


Google Play Services

The Google Play Services provide APIs to develop using extra Android features created by Google, this includes technologies such as Google Maps and Google+. Note – devices must have the Google Play Services Runtime installed to use these additional APIs. Almost all new Android devices include the runtime - notable exclusions include the Kindle Fire devices. Generally, Android emulators don’t include the Google Play Services runtime by default but it can be installed separately.
Recommendation: highly recommended to install
More info:


Google USB Driver

The Google USB Driver enables USB debugging for most Android devices from Windows. Note - for some devices, you may need to install device specific drivers provided by the manufacturer. USB debugging is somewhat inconsistent on Windows 8.x, it can be unreliable to connect and some devices tend to work more reliably than others (ie Nexus). The driver isn’t used on OSX.
Recommendation: install if using Windows


Quick Reference

If you don’t want to read all of the above – take a look at the image below to view my (Windows) configuration:

Saturday, January 24, 2015

How to update the Firmware on the RedBear BLE Mini


RedBear Labs has built a fantastic small profile, low cost, Bluetooth Low Energy board which is perfect for prototyping or hobby IoT projects:

From their website:

You can now incorporate Bluetooth 4.0 Low Energy (BLE) technology easily in your project with our BLE Mini. BLE Mini requires only a serial port for communication so it supports all major development platforms that have UART interface including Arduino, Raspberry Pi, Netduino, BeagleBone etc. In addition, we provide you with access to all Ti's CC2540 I/O pins and the ability to upload firmware via USB, making the BLE Mini an ideal development/prototyping board for your BLE project/accessory/Appcessory.

To get the most out of the BLE Mini you’re probably going to want to install the latest firmware.  It turns out it’s a fairly straight forward process, but there are a few things worth noting.

1st – Download the latest Firmware

You can download the latest firmware from GitHub here:

For the BLE Mini we’re looking for a file labelled “Biscuit-UART-xxxxxxxx.bin”.  At the time of writing it’s “Biscuit-UART_20140409.bin” which was built in April, 2014.

2nd – Find a Windows 7 PC

Unfortunately the firmware update cannot be updated from Windows 8/8.1.  In the RedBear forums it states it should work from a Windows 7 Virtual Machine.  However, I tried it from within a VMWare Player Windows 7 VM, and although I was able to reach the BLE Mini, I wasn’t able to update the firmware.

Fortunately I still have my trusty little Acer 1810T running Windows 7, which I used to update the firmware.

3rd – Connect the BLE Mini to the Win7 PC

There’s one small black button on the top the BLE Mini, close to the Micro USB port.  Hold this down while  connecting your Mini via a regular USB Micro cable.

The first time you connect the BLE Mini this way, Windows will probably need to search for drivers.  Once you see the “Installing device driver software” popup in the system tray, leave the BLE Mini connected but you can let go of the button.


Once it’s installed you’re ready to move on, and you should see a new disk in the file explorer with a capacity of 240 KB.


4th – Update the Firmware

Now it’s simply a matter of removing the old .bin file and copying in the new one.  You may want to back up the old version “just in case”.

Just be warned, when I updated my two BLE Minis, they both rebooted after every file operation.  And I needed to repeat the process of holding down the button and reconnecting the BLE Mini to continue.  Otherwise it was a smooth and simple process. 

Again just replace the old .bin file with the new one, no need to rename the file, etc.


5th – Test it Out

Disconnect the BLE Mini and then reconnect it to a power source (without holding the button).  If the green light comes on you should be all set.  RedBear also has free iOS and Android BLE Controller Apps you can use to test functionality. 

The behaviour of the new firmware seems to be basically the same, with improved stability and connectivity, but the broadcast name has been changed from “Biscuit” to “BLE Mini”.  All of the services and characteristic GUIDs should be the same.

Now If something goes horribly wrong, there are instructions for resetting and reformatting the BLE Mini in the RedBear Labs forums:

If you’re not sure where to go next, I have some sample code to connect to the BLE Mini from Windows Phone 8.1 here:

And here’s an article introducing BLE communication between a similar BLE board from Seeed Studios using Android: 

And finally, when you’re ready for a bigger task, check out the Monkey Robotics project.  It has a ton of great code including a walkthrough to build a BLE controlled car using the RedBear BLE Mini, and connecting from a Xamarin Forms project:


Tuesday, January 13, 2015

Open the Settings app Programmatically from Xamarin iOS


When Apple released iOS 8, it came with a massive API update.  They released a number of new frameworks and over 4000 new APIs.  So needless to say, I’m still discovering new toys and tricks that released with the latest version.

A really cool feature included in iOS 8 is the ability to launch the built in settings app from within our applications; something we haven’t been able to do since iOS 5 (and was removed again in iOS 5.1).

This is useful for multiple reasons.  First, you can add app preferences and expose them through the settings app: 

However, this isn’t all that common, primarily because it required the user to leave your app and search through the Settings app.

But it also means we have quick way to push users to toggle things like permission to use location data.

Fortunately it’s also fairly easy to do, we simply need to create and open a NSUrl with the correct string: {app-settings:}.  And this is conveniently available as a static property on the UIApplication class.

Do note, this only works in iOS8 so if you’re app is compatible with older versions of iOS, you should check you’re running on iOS8 first; this will cause an exception on older versions.

if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0))
var settingsString
= UIKit.UIApplication.OpenSettingsUrlString;
var url
= new NSUrl (settingsString);
UIApplication.SharedApplication.OpenUrl (url);