Will Write our Own App to Trigger Mi Band

10371404_10206191040355262_3352352559338119016_n
Xiaomi Mi Band is the world’s cheapest and branded fitness tracker. So why don’t we do some experiments so that we can make the Mi Band do what we say for a change?

I’ll be doing this code session for Android using Android Studio, hope others can understand the basics

Step 1
Firs of all we start a new empty project. And add 4 buttons. This is to test 4 basic functions of the Mi Band. Then initialize the buttons and ready the the setOnclick listners.

Step 2
Add Bluetooth Permissions in the Manifestfile, else you wont be able to connect to the band 😀

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /

Step 3
Add the Xiaomi Mi Band dependencyto the gradle and sync it

compile 'com.zhaoxiaodan.miband:miband-sdk:1.1.2'

Step 4

In the oncreate you have to create and initialize an instance of the MiBand Class

private MiBand miband;
miband = new MiBand(this);

Step 5

I have not done the pairing part in the code, but I assume the Mi band of yours is already paired to the device. If you have paired more than one device you can populate all the paired devices and allow the user to select one. But for the demonstration purpose I have only paired with my Mi Band so it is the one and only device returns to me

Object[] devices = BluetoothAdapter.getDefaultAdapter().getBondedDevices().toArray();
final BluetoothDevice device = (BluetoothDevice) devices[0];

So I am getting my paired device from the available devices, as I only got my Mi band paired I get the 0th device. hope you people got that part 😀

Step 6
No you have to connect to the paired device

miband.connect(device, new ActionCallback() {
    @Override
    public void onSuccess(Object data) {
        pd.dismiss();
        Log.d(TAG, "Success !!!");
        miband.setDisconnectedListener(new NotifyListener() {
            @Override
            public void onNotify(byte[] data) {
                Log.d(TAG, "Disconnected!!!");
            }
        });
    }
    @Override
    public void onFail(int errorCode, String msg) {
        pd.dismiss();
        Log.d(TAG, "connect fail, code:" + errorCode + ",mgs:" + msg);
    }
});

Step 7

So if you have successfully connected to the device. You can start invoking functions of the MiBand in the button clicks
For example : You can make it Vibrate using this code snippet

miband.startVibration(VibrationMode.VIBRATION_WITH_LED);

You can check out some more commands in the sample code available in Git HubgitHub-download-button

Advertisements

A Myth comes true with Google Cloud Vision API

Cloud Vision API by Google Cloud is the latest addition for the Google Cloud platform. Last week it has been made a beta release and been allowed for the developers around the world to try and experience it. 2 month of free usage is offered by the Google at the moment as a promotion.

This API is already being used in the Google Photo app. You may have already experienced it’s power. Analyzing objects in a photo, face detection, geographical location detection and fast search are some of their features.

Features

So I was able to register for it lat week and was already able to build an app. But as it was still in the beta. Faced some problem(Can be found with solution in Stack overflow) on creating the API key for Android. But was able to find a quick fix as many people are facing the issue.

But when I was going through the API and I found many awesome feature that just a myth till today. Still there is no proper documentation but you can find some of the popular features and getting started docs listed here.

Here are the High Lighted APIs listed down
vision

You can also try Google Cloud Vision API here

Pricing is also seems reasonable compared to the amount of processing that they have promised to do. It will be a big break through in the history of the image processing technology.

You can check out the Android app I have developed using the ‘FACE_DETECTION’ API, if you love selfie you will Love it. Download the Selfie Mood app here
selfie mood

Application of Locke’s Goal Setting Theory in Agile Methodology

A theory I came across recently looked to adhere to some concepts of the Agile methodology. And I felt it seems to fill some kind of a black hole in the Agile methodology. The theory is called be Locke’s Goal Setting Theory.

Locke’s Goal setting theory is about setting up goals in a way, that when they are achieved they will also turn into a great motivator to the person/team who follows it accordingly.

In my working place we follow Agile methodology to develop software. I know some of the basic Agile process. So the basic concept of Agile according to my understanding is a recursive process of software development which goes under many changes and iterations to provide what the clients really wants. Also the process is transparent to the client so the client is always updated. This methodology is developed to create a better understanding between the client and the developers so that they will come up with the exact product that the client wants, including the changes through up to the final product release.

When you try to compare the 5 key factors of the Locke’s Goal Setting Theory with Agile process, you will see that some are very much similar to each other.

Will see how much are they related to each other

Setting Goals and trying to achieving them, does not always works well for everyone. Some people achieve it and some people get stressed out or stuck and instead of getting motivated they get demotivated due to many reasons. In Agile we can consider it to the user stories created by clients , which we add into sprints as tasks and try to implement them withing that sprint time.

The 5 Factors that are need to affect the motivations according to Locke’s Goal setting theory are

Clear and Specific Goals

Challenging Goals

Handling Complex Problems

Commitment

Feedback

1 – The Goal has to be clear and specific, so in Agile we meet the client and their work environment and understand it and then we gather user stories which are then explained by the client to the developing team. This ensure that the requirement we have gathered are clear and specific.

2 – The next one is Challenge, we create sprints with user stories provided by the client, which we think we can achieve within the next sprint and we define our self a deadline. Easy challenges won’t be motivating, because they won’t feel important. So we select a set of challenging stories to achieve within the sprint. Achieving them gives us(developers) a big motivation.

3 – Handling complex problems, often there is chances of estimations made by the developers go wrong. So some times developers tend to get stuck on a problem that blocks the whole process, which leads to stress and the developers get demotivated. At times like this in Agile we split the complex task in to many sub tasks to visualize the problem in a better way so that we can handle them individually and solve them.

4 – Commitment is next, it is really important when it comes to team, we have to work together to achieve the goals. Software modules normally will be developed by different developers, which will at the end works together to provide a solution. So being committed to the goals that’s assigned to you will pave way to the ultimate team goal. Commitment is also affected by various kinds of internal and external causes. But keeping the developers committed is something that has to be handled by  the management or the team lead.

5 – Feedback is an important element. As a team lead, it’s important to give feed backs on the goals achieved by the team members and in which area they can improve. Not only the Team Lead, the client must be also trained in a way so that they give positive feedback about  the developers when they are present in a meeting or a scrum. This will enable the team members to work more efficiently and happily in future. Developers often consider positive feedback as another hidden goal. So it is the responsibility of the leads not to spoil it for the developers.

Even though the companies following Agile methodology these days does motivate the employees/developers by giving the team a lunch out or a pay increase/bonus. The motivation part is not mentioned theoretically any where. So I think if we can apply the Locke’s Goal setting theory into the Agile, we can get a better performance from the developer team with Good customer satisfaction.

My Experience with Xamarin Android

Hi folks, I have been working on Xamarin Android for the last 6 months. I normally don’t go for a third party development tools to make Android or iOS apps(I am not talking about Xamarin Mono but Xamarin Android). But the project requirement was to develop in Xamarin. The reason client said was that we can have the service layer for both Android and iOS as same. Ok that’s cool most of the service calls are handled by the service layer which is developed by our back end developer.

Some people had this mind set when it comes to Xamarin, where they compare it with other hybrid platforms like Phonegap. But the reality is Xamarin is a framework designed specially for the people who knows C#, so that they can write the Android code in C# and not have to worry about Java.  And the out put is not a web based solution, it is a native app that Xamarin out puts so it is faster like all native apps. Many C# developer I know have tried to develop apps using Xamarin and ended up complaining that, ‘Hey man its the same android code that we have to write but in C# so whats the point in it’ . So here what I am trying to say is, even though it looks or give a comfortable feel, for the C# people when they start developing they will come to understand that they need at least some basic knowledge on Android.

I tried to give an overview of what people think and what I think of Xamarin. Lets see how it feels when you try to develop and publish and app. So for C# developer its good if they knew some basic Android. Else you can still follow the Xamarin tutorials and figure out things. For Android developers if they have some basic knowledge on C# language it would be enough.

When you create an android project. It creates the file structure exactly like in Eclipse or Android Studio giving you a good first impression. Then you can add Activities and Layouts as usual. Here the layout xmls come with the extension axml. The xml we create as resources are normal xmls. When you start developing you will start to see the differences.
For examples the edit text component has a method called setText

editText.setText("String to Show")

but in Xamarin android, its not a method but a property in that class, so you simply set the value like

editText.Text =  "String to Show";

These kind of changes, you will see a lot. And when it comes to libraries. You have to Go for the Xamarin Components. Where there is only a handful of components to choose from. Most of the very popular ones are being added by the Xamarin people and popular companies. But still, at some times you have to create a component project by looking into a java library project available for normal android. But you have lots of other NuGets to use  for other simple things you find hard to handle in Android.

And one of the important think on updates are, that Google go on updating it’s v4, v7  libraries and Xamarin is slow on updating their system to support it. So When you are using libraries or components, you have to have a good knowledge on the component and their dependencies to manage all of the features you want from them. So in some cases you have to use old versions of components.

I have no problems in building and running a debug apk, but when I had to release a release build I had to face a lot of problems. One of them is the problem with the dependency versions which I have explained above. On one occasion I have to remove all the Google Play Services components and have to find an alternative way to handle Google play services which I manages to do with some http get/post requests.

So what I will always recommend is, if you are an Android developer unless it is a must don’t go for the Xamarin. Else if you are a C# developer, you will get used to the Xamarin and hopefully Microsoft will acquire it and will make it more wonderful in future.

How to Generate a PDF in Xamarin Android

It may sound simple, but when it comes to Xamarin, all the problems starts to emerge. So basically Xamarin does not provide any special class for PDF generation.

Failed Solution 1
So we were let alone to use the Android PDFDocument class. Which Unlike in the Native Android did not work. Actually it was giving me a White and blank document. When searching for the Xamarin Documentation what I found was the Xamarin documentation having the Android Reference Code which is very disappointing (Link to Documentation)

Here is the Code snippet for it(I may have made a mistake here, let me know if you guys found a solution for using PDFDocument class )

private String sdCardPathforPDF;
private String filePath;
private FileStream stream;

sdCardPathforPDF = Environment.ExternalStorageDirectory.AbsolutePath;
filePath = Path.Combine(sdCardPathforPDF, "MyPDF/test5.pdf");
stream = new FileStream(filePath, FileMode.Create);

tView = new TextView(context);
tView.SetTextColor(Color.ParseColor(resource.GetString(Resource.Color.black)));
tView.Text = "Hello";
tView.SetHeight(50);
tView.SetWidth(50);

var document = new PdfDocument();
var pageInfo = new PdfDocument.PageInfo.Builder(612, 792, 1).Create();

var page = document.StartPage(pageInfo);
tView.Draw(page.Canvas);
document.FinishPage(page);

document.WriteTo(stream);

stream.Close();
document.Close();

Toast.MakeText(context, "PDF Generated", ToastLength.Short).Show();

Failed Solution 2
Then I searched for a plugin or a 3rd party library, ohhh my god, I found some expensive plugins

Capture

Who would love to buy a plugin for 1600 usd??? Apitron and XFINIUM are some of them

Failed Solution 3
I found some Free open source libraries for Android, so I thought of  Java Bind it, but when i tried to Java Bind them, but I ended up getting lots of errors that i cannot fix it unless I am the developer of that library. So I dropped that Idea as well.

Failed Solution 4
Then after the disappointment of JavaBind, I came across an article saying that the iText library has been published for .Net in the name of iTextSharp. But when I added that Nuget Package I came across an error saying that

System.IO.FileNotFoundException: Could not load assembly 'System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. Perhaps it doesn't exist in the Mono for Android profile?

so It says there is an assembly called System.Drawing dll is missing. I tried to manually add it but it did not work. When I researched on it, it said that Mono for Android(Xamarin Android Framework) does not include that ‘dll’ I dont know how come a popular ‘dll’ got missed out of their framework.

At last a solution that Worked
So with all of the frustration I had, I did not give up on finding a solution till i came across problem where a guy has used ‘iTextSharp’ in Xamarin Android and its working. But he did not mention or reply my question in that thread. So I thought of giving a final look at the Nuget Manager. This time just like the previous time I searched for ‘iTextSharp’ but this time I went through each and every result the list showed.
And Hoooray I found It the one I am searching for, some one has made a nuget of ‘iTextSharp’ for Xamarin, its called “Xam.iTextSharpLGPL”

XamiTextSharp

And this worked like a Magic. And this library has more functionality than the default PDFDocument.
This library is based on iTextSharp 4.1.6  which means licensed under LGPL. Free to use and it is an Open source library.
Open source project can be found in Bit Bucket : https://bitbucket.org/smarongiu/xam.itextsharplgpl

So this is the Basic code of Writing a Simple PDF Doc

System.IO.FileStream fs = new FileStream(Server.MapPath("pdf") + "\\" + "First PDF document.pdf", FileMode.Create)

// Create an instance of the document class which represents the PDF document itself.
Document document = new Document(PageSize.A4, 25, 25, 30, 30);
// Create an instance to the PDF file by creating an instance of the PDF Writer class, using the document and the filestrem in the constructor.

PdfWriter writer = PdfWriter.GetInstance(document, fs);

Before we can write to the document, we need to open it.

// Open the document to enable you to write to the document

document.Open();

// Add a simple and well known phrase to the document in a flow layout manner

document.Add(new Paragraph("Hello World!"));

// Close the document

document.Close();
// Close the writer instance

writer.Close();
// Always close open file handles explicitly
fs.Close();

I grabbed this quick code snippet from Micke Blomquist

Hope this article will help people like me in future…!
See ya…!

Android Proguard a General Idea

What is Proguard on android and what does it do?

ProGuard is a tool that comes with the Android SDK. It  shrinks, optimizes, and obfuscates your code by removing unused code and renaming classes, fields, and methods with semantically obscure names. The result is a smaller sized .apk file that is more difficult to reverse engineer. Because ProGuard makes your application harder to reverse engineer, it is important that you use it when your application utilizes features that are sensitive to security.

How to enable it? And is it recommended?

ProGuard is integrated into the Android build system. And to enable it you just have to find the “project.properties” file in your android project and have to uncomment the line which sets the path for the ProGuard. Also Proguard slows the app building sequence.
So you do not have to invoke it manually. ProGuard runs only when you build your application in release mode, so you do not have to wait longer time when debugging the app. Having ProGuard run is completely optional, but highly recommended.

So is that all that we have to do to enable and use ProGuard?

Yes that’s the basic, and it only does the Shrinking, but there are 2 more functions that give more advantages in increasing the security and management of the build.
They are Obfuscation and Repackaging.

So what is Obfuscation and how does it Work

With tools available to extract the contents of APK’s, deodex, and read the class files, it’s important to obfuscate to protect the proprietary aspects of your codebase.

ProGuard generates a mapping file that allows you to map the stack traces of obfuscated code to actual methods. Basically it replaces the methods in meaning full name to something unguessable like a work or letter.

Original Code

public Data(int id, int result, String message) {
       mId = id;
       mResult = result;
       mMessage = message;
    }

Obfuscated Code

public a(int paramInt1, int paramInt2, String paramString)
    {
       this.a = paramInt1;
       this.b = paramInt2;
       this.c = paramString;
   }

 Hope you got an idea on how it works

So what is Repackaging?

Repackaging allows ProGuard to take externals jars and class files and move them to a single container with a common java package location.

For those of you building libraries, repackaging is extremely helpful if you choose to show a simple interface to third party developers while keeping a maintainable and well structured project hierarchy in the source repository. This can also be useful in organizing lower level packages while exposing well defined interfaces!

 Optimizing

There is another function of Proguard which Optimizes the code, where you can specify which part of the code, which type of code needs to be Optimized.  However, there are known incompatibility issues with various Dalvik versions.
But It is good to know this feature, which can be used without issues using some other tools.

Reference : http://www.crashlytics.com/blog/mastering-proguard-for-building-lightweight-android-code/, http://developer.android.com/tools/help/proguard.html