How to Get Weight from the Weigh Scale/Weigh Bridge Bluetooth Device using SAPUI5 Hybrid App?

In this article, I would try to provide you a real project example of fetching data from a Bluetooth device using UI5 Hybrid Application. And, no SAPUI5 trainer or training course would cover this.

Note: In this tutorial I wont share screen shots of weigh scale/bluetooth mac address due to security reasons.

Even the advanced UI developers might find this article a hard nut to crack. But, I have tried to break them into step by step so that even the beginners might understand. You would definitely need a cup of coffee or any drink of your choice which can keep your brain up and running. Remember, do not drink anything which might slow down your thinking power and make you drowsy.

Enough of prep-talks. Let’s get into action.

Step 1

Since most of you would not have a weigh scale to test, I would suggest you to install the app Bluetooth Serial from the Playstore

Step 2

Connect to the weigh scale bluetooth adapter using the mobile bluetooth and send the values using the playstore app bluetooth serial.

Step 3

Send the values like “us0004455. kg” since from weigh scale the data flows in this manner based on the device you have purchased.

Step 4

Build an SAPUI5 hybrid app with the plugin “cordova-plugin-bluetooth-serial”.

Step 5

Connect to the weigh scale bluetooth adapter using the below function and get weight of it

<code>//Get the paired bluetooth device name and address using
 bluetoothSerial.list(function(devices) {
                 var oModelJson = new sap.ui.model.json.JSONModel(devices);
                 that.getView().setModel(oModelJson, "BluetoothDevices");
                 //For testing choose the first device
                 that.device  = devices[0];
             }, function(error) {
                 sap.m.MessageToast.show(error);
             });</code>
<code>//that.device.address refers to the bluetooth address of the device
//that.device.name refers to the name of the device
 bluetoothSerial.connect(that.device.address, function(){
 bluetoothSerial.read(function(data) {
//From weigh scale you will get every milli second updated values in array so choose the recent one
 data =  data.split("\r\n");</code>
<code>//Here substring values to split are based on the device data format.  
//For me  its 2,10 offset values
 alert("weight="+data[1].substring(2, 10).trim(););
 });
 }, function(){
 sap.m.MessageToast.show("FAILED TO CONNECT TO " + that.device.name);
 });</code>
<code>// for me its 2,10 offset values
 alert("weight="+data[1].substring(2, 10).trim(););
 });
 }, function(){
 sap.m.MessageToast.show("FAILED TO CONNECT TO " + that.device.name);
 });</code>

Above was the trailer. You get an idea, what we are trying to accomplish. Now let me jot down the steps in more details. Let’s start the development one more time.

Step 1: Hit the Run “cmd” on windows

Step 2: Enter the cmd “Cordova create weigh com.weigh weigh ”

Step 3: Move to directory and add the platform android

<code>cmd "cd weigh"
cmd " cordova platform add android "</code>

Step 4: Add the plugin cmd “cordova plugin add cordova-plugin-bluetooth-serial”

Step 5: Create a sample project in webide and place two buttons in view

Step 6 : Create a fragment to view the connected devices (Paired in mobile)

For this experiment i have paired with the only one bluetooth adapter device of weigh scale.

Step 7: Press the get devices to view the paired device name and address with this function call

Step 8: Select a device from the list.

I have used table column list press event and have invoked below function.

Step 9: Get the weight from the device

For this connect the bluetooth weigh scale adapter with serial bluetooth google play app for testing since we don’t have expensive weigh scale at home. Once connected send the values like us12345678.kg

Step 10: Press on the get weight button in view and invoke the below function

How to develop APK File?

Now you know the functionality. It’s time to develop APK file. Follow the below steps.

Place the sapui5 code in the WWW folder

Add 2 lines in the index.html with online resource link.

Finally hit the cmd “cordova build” and “cordova run”

Phewwww!! Finally we are done.