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 Hub
thanks in advance, feels a lot easier after reading but where did you get the api for mi band. i am not able to find any.
Hi, it was in the Github, here you go https://github.com/pangliang/miband-sdk-android
hey bro.. thanks for the info..
i have task to make this application but i can’t understand well if i just read, do you have some picture or video to explain the instruction? cause i am still newbie in andorid.. i hope you can help me 🙂
Hi Ray, did you try out the sample app, that I have shared from my Github. If you have a Mi Band you can run it and test the app. And it has the minimal amount of code, so it will be easy to understand. https://github.com/Batzee/Mi-Band-Android-Sample
thank’s bro..
but i have error at the package..
zhaoxiaodan.miband.ActionCallback
zhaoxiaodan.miband.MiBand
zhaoxiaodan.miband.listeners.NotifyListener
zhaoxiaodan.miband.model.VibrationMode
can you help me to get the package to fix it.. thank’s bro 😀
Hello, Can you please tell me, how can i write code for long press and short press of the mi band button.
Thanks in advance 🙂