flutter_aclas_scale_plus

Flutter plugin for ACLAS weight scales over Serial, USB, and BLE.

Android setup (ACLAS SDK)

The plugin uses the ACLAS scale SDK. You must add the SDK artifacts to the plugin's Android module:

  1. Copy AclasScaleSdk.jar from the ACLAS demo app (e.g. aclas/app/libs/AclasScaleSdk.jar) into:

    flutter_aclas_scale_plus/android/libs/
    

    Create the libs folder if it doesn't exist.

  2. Copy the jniLibs folder (native .so libraries) from the ACLAS demo (e.g. aclas/app/src/main/jniLibs/) into:

    flutter_aclas_scale_plus/android/src/main/jniLibs/
    

    So you have android/src/main/jniLibs/arm64-v8a/, armeabi-v7a/, x86/, x86_64/ with libAclasScaleLib.so in each.

  3. Verify native libs in the built APK (if the app crashes with UnsatisfiedLinkError or scale connect fails): after building (e.g. flutter build apk), check that the .so are included:

    unzip -l build/app/outputs/flutter-apk/app-release.apk | grep -E "lib/.*\.so"
    

    You should see entries like lib/arm64-v8a/libAclasScaleLib.so. If missing, run flutter clean && flutter pub get && flutter build apk and check again.

Permissions

The plugin's Android manifest declares Bluetooth and location permissions for BLE. For USB, the app may need to request USB permission when connecting (result -4 from openScale).

Usage

import 'package:flutter_aclas_scale_plus/flutter_aclas_scale_plus.dart';

final scale = FlutterAclasScalePlus();

// 1) Init for connection type: Serial, USB, or Bluetooth
await scale.initDevice(AclasConnectionType.Serial);

// 2) Optional: get device list to choose path / index / address
final devices = await scale.getDeviceList();

// 3) Connect (path for serial, index for USB, address for BLE)
int result = await scale.openScale(path: '/dev/ttyUSB0');
// result: 0 = success, -4 = USB permission needed

// 4) Listen to events (connected, disconnected, usb_attached, usb_detached, weight, error)
scale.eventStream.listen((event) {
  switch (event.type) {
    case AclasScaleEventType.connected:
      // connected
      break;
    case AclasScaleEventType.disconnected:
      // disconnected
      break;
    case AclasScaleEventType.weight:
      print(event.weightInfo?.weight);
      break;
    // ...
  }
});

// 5) Live weight: start to receive continuous weight events; stop to disable
await scale.startLiveWeight();
// ... weight events arrive on eventStream ...
await scale.stopLiveWeight();

// 6) One-shot and commands
final connected = await scale.isConnected();
await scale.zero();           // zero the scale (weight must be stable)
await scale.tare();           // tare (weight must be stable)
final w = await scale.getWeight();
await scale.setTarePre(3);    // preset tare value
await scale.setFrequency(0);  // frequency index

// 7) Close when done
await scale.closeScale();

API summary

Method Description
initDevice(type) Initialize for Serial, USB, or Bluetooth.
getDeviceList() List of paths (Serial), or addresses (Bluetooth), etc.
getConnectionType() Current connection type (Serial / USB / Bluetooth) set by initDevice.
openScale({path?, index?, address?}) Open connection. Returns 0 or -4 (USB permission).
closeScale() Close connection.
isConnected() Whether scale is connected.
startLiveWeight() Start streaming weight on eventStream.
startLiveWeightEnsuringConnection() If not connected, refresh device list and connect to first device, then start live weight.
stopLiveWeight() Stop streaming weight.
zero() Zero the scale.
tare() Tare the scale.
getWeight() Read current weight once.
getWeightEnsuringConnection() If not connected, refresh device list and connect to first device, then read weight.
setTarePre(value) Set preset tare (integer).
setFrequency(index) Set frequency index.
eventStream Stream of AclasScaleEvent (connected, disconnected, usb_attached, usb_detached, weight, error, crash).

Events

  • connected – Scale connected.
  • disconnected – Scale disconnected.
  • usb_attached – USB device attached.
  • usb_detached – USB device detached.
  • weight – New weight (only when live weight is started); payload in event.weightInfo.
  • error – Error; errorCode and errorMessage set.
  • crash – Native plugin exception; errorMessage, stackTrace, exceptionType for reporting (e.g. Crashlytics).

Reference

Based on the ACLAS demo: InitDevice, OpenScale/CloseScale, live weight via onRcvData, zero/tare, readWeight, tare pre, frequency, and connection/USB events.

Publishing

Before publishing to pub.dev (or a private registry):

  1. Set version in pubspec.yaml (e.g. 1.0.0).
  2. Update CHANGELOG.md for the release.
  3. Replace homepage and repository in pubspec.yaml with your actual URLs.
  4. Ensure ACLAS SDK artifacts are not shipped in the published package (they are optional; apps add them locally per README).
  5. Run flutter pub publish --dry-run to check for issues, then flutter pub publish when ready.