Standard service - a fixed amount of work that is performed at a fixed price.  Post Service

  

Thursday, 14 March 2019 16:10

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

Written by 
Rate this item
(0 votes)

We always learn a “Hello World” program or app in any new technology. But when you are placed in real projects, you would have requirement like to read

data using wireless scanner or from weighscale and weighbridge. Life is cruel for beginners. ?

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. ?

Also ReadEverything about CDS (Core Data Services)

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”. You can read its documentation here.

Step 5

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

1
2
3
4
5
6
7
8
9
10
<code>//Getthepairedbluetoothdevicenameandaddressusing
bluetoothSerial.list(function(devices){
varoModelJson=newsap.ui.model.json.JSONModel(devices);
that.getView().setModel(oModelJson,"BluetoothDevices");
//Fortestingchoosethefirstdevice
that.device=devices[0];
},function(error){
sap.m.MessageToast.show(error);
});</code>
1
2
3
4
5
6
7
<code>//that.device.addressreferstothebluetoothaddressofthedevice
//that.device.namereferstothenameofthedevice
bluetoothSerial.connect(that.device.address,function(){
bluetoothSerial.read(function(data){
//Fromweighscaleyouwillgeteverymillisecondupdatedvaluesinarraysochoosetherecentone
data=data.split("\r\n");</code>
1
2
3
4
5
6
7
8
<code>//Heresubstringvaluestosplitarebasedonthedevicedataformat.
//Formeits2,10offsetvalues
alert("weight="+data[1].substring(2, 10).trim(););
});
},function(){
sap.m.MessageToast.show("FAILED TO CONNECT TO " + that.device.name);
});</code>
1
2
3
4
5
6
7
<code>//formeits2,10offsetvalues
alert("weight="+data[1].substring(2, 10).trim(););
});
},function(){
sap.m.MessageToast.show("FAILED TO CONNECT TO " + that.device.name);
});</code>

Also CheckHow to Consume Custom OData in SAPUI5 Application

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

1
2
3
<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

Also Read Lean SAP ABAP Programming for S/4HANA

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. I know, this is not a cup of tea for many. But, exception is the rule. The few, who need to develop similar application might refer to this article and deliver their project developments. Sagar means ‘Ocean‘ in Sanskrit, and SAPUI5 is an Ocean. This Sagar tried to explain a drop from the UI Ocean. Hope this helps.

I am working on a series on Offline Hybrid App. Please stay tuned. I would suggest, brush up your SAPUI5 skill soon. You do not need anything extra. Just install WebIDE and connect to your SAP System and start practicing.

Those who do not evolve, get extinct. You do not want to be one of them.

We have a very active Telegram (App) SAP Technical Group with more than 4200+ SAP Technical Practitioners from 6 Continents of the SAP World. Please join it using below link.
Telegram SAP Technical Discuss Group. You need to install the Telegram App first on your mobile device. Once you have it on your mobile, you can join the group and also access it from the Web on your computer and laptop.

Step by Step Tutorials on SAPUI5

Read 42 times

Leave a comment

Make sure you enter all the required information, indicated by an asterisk (*). HTML code is not allowed.