Integrate Digital scales with Flutter apps.
- Wuxianliang WXL-T12
- Update your app
android/app/build.gradle
and update minSdkVersion to at least 21
defaultConfig {
applicationId "com.example.app"
minSdkVersion 21
// ...
}
- Setup required permissions according to OS and technology:
- Add the following to your main
AndroidManifest.xml
. See Android Developers and this StackOverflow answer for more information about permission settings.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.flutter_label_printer_example">
<uses-feature android:name="android.hardware.bluetooth" android:required="true" />
<uses-permission android:name="android.permission.BLUETOOTH" android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.BLUETOOTH_SCAN"
android:usesPermissionFlags="neverForLocation" tools:targetApi="s" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"
android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"
android:maxSdkVersion="30" />
<!-- ... -->
</manifest>
- Choose an implementation that matches your digital scale device, and create an instance of it. i.e.
WXLT12()
- Connect to the digital scale. This will search for the scale nearby your scale and connect to it. Once connected the callback will be invoked.
scale.connect(const Duration(seconds: 30), () {
// do something
});
- To get the weight readings from your digital scale, use one of the following:
getWeightStream()
returns a dart Stream which continuously receive data from your scale. Oftentimes this is the real time output of the scale. Check with your manufacturer if this feature is supported and the exact behaviour.getWeight()
returns the weight value as measured at the moment of its call. However you may want to usegetStabilizedWeight
instead.getStabilizedWeight()
returns a weight that has been stabilized. In practice, when you use the scale by placing an object on it, the weight value will oscillate erratically before stabilizing due to the extra weight added by the action of placing the object. This function returns the weight of the object only when the weight reading has stopped moving. You can specify number of samples to wait until it is considered stabilized.
- Disconnect your scale when you are done with it using
disconnect()
. Otherwise there may be lingering connections.