Wednesday, March 13, 2013

Android Low Energy Bluetooth or a feature that I would love to see in C#

Recently I have been playing with BLE on Android (Samsung S3). One of the issues is no exposed BLE API in the current version of Android. I said, exposed, because by reflection you can find them! Or at least some of them, in some cases, you need to access the BLE library that was supplied to the phone (but not connected to the Android OS.

 

The process is very simple to identified the library:

 

First, add the following to the manifest:

 

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <uses-library
        android:name="com.broadcom.bt.le"
        android:required="false" />
    <uses-library
        android:name="com.csr.bluetooth.BluetoothExt"
        android:required="false" />
    <uses-library
        android:name="com.htc.android.bluetooth.le"
        android:required="false" />
    <uses-library
        android:name="android.server.BluetoothGattService"
        android:required="false" />
    <uses-library
        android:name="com.samsung.bluetoothle.BluetoothLEClientProfile"
        android:required="false" />

 

Then create an enumeration. This is very much a Java enumeration (you don’t see this in C#)

 

public enum HardwareEnum {
    Motorola("android.server.BluetoothGattService"), Broadcom(
            "com.broadcom.bt.le.api.BleAdapter"), Csr(
            "com.csr.bluetooth.BluetoothExt"), Htc(
            "com.htc.android.bluetooth.le"), Samsung(
            "com.samsung.bluetoothle.BluetoothLEClientProfile"), None("");
    private final String value;

    HardwareEnum(String value) {
        this.value = value;
    }

    public String getClassName() {
        return value;
    }
}

Next you need to write a detector, which is very simple with the above enumeration.

 

public class HardwareDetector {
    /*
     * Each of the class names MUST be in the Manifest. as <uses-library
     * android:name="com.broadcom.bt.le" android:required="false" />
     */
    private static final String TAG = "HardwareDetector";

    public static HardwareEnum detectHardware() {

        HardwareEnum result = HardwareEnum.None;
        for (HardwareEnum item : HardwareEnum.values())
            try {
                Log.i(TAG, "testing:" + item);
                Class.forName(item.getClassName());// , false, null);
                result = item;
                Log.i(TAG, "found:" + item);
                return result;
            } catch (Exception exc) {
                Log.w(TAG, exc.toString());
            }
        return result;
    }
}

Now, you know the library that talks to the BLE on your phone. You may wish to use reflection to explore what is in the library and develop code from there.

 

What is the missing feature from C#?

Simple, being able to do enumerations consisting of objects. While working with Android code, I found that this pattern allowed massive simplification of code and much improved clarity…   Microsoft folks, please add something like this!

2 comments: