Last updated

OBH Flutter SDK

Overview

The Flutter SDK is a wrapper around the iOS and Android SDKs, allowing seamless integration across both platforms while leveraging Flutter's cross-platform capabilities.


Requirements

The Flutter SDK requirements are both platforms requirements in addition of the flutter wrapper repository access.

To start, you'll need a github access to obh-mobility/obh_flutter_sdk repository. Please reach out to your OBH representative to do so.


IOS

  • Minimum ios deployment version : 13
  • You'll need a Github access to obh-mobility/obh_ios_sdk repository packages. Please reach out to your OBH representative to do so.

Android

  • Minimum android SDK version : 29
  • You'll need a Github access to obh-mobility/obh_android_sdk repository packages. Please reach out to your OBH representative to do so.

Installation

IOS

  1. Setup Podfile

Ensure the deployment version is set to a minimum of 13

platform :ios, '13.0'

Add the native library OBHSDK in the target Runner.

target 'Runner' do
  use_frameworks!
  use_modular_headers!

  ## Add the following line
  pod 'OBHSDK', :git => 'git@github.com:obh-mobility/obh_ios_sdk.git'

  flutter_install_all_ios_pods File.dirname(File.realpath(__FILE__))
  target 'RunnerTests' do
    inherit! :search_paths
  end
end
  1. Adding Permissions to Info.plist

Add the following entries to your Info.plist file:

<dict>
  <key>NSBluetoothPeripheralUsageDescription</key>
  <string>Used to connect to the base</string>
  <key>NSBluetoothAlwaysUsageDescription</key>
  <string>Used to connect to the base</string>
  <key>NSLocationWhenInUseUsageDescription</key>
  <string>This application uses location to scan nearby bluetooth devices</string>
</dict>

Android

The flutter android specific installation is similar to the Android SDK installation.

  1. Define min SDK to at least 31 in android/app/build.gradle.
android {
  defaultConfig {
    minSdkVersion 29
  }
}
  1. Add Github package repository in android/build.gradle under allprojects.repository section:
def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
    localPropertiesFile.withReader('UTF-8') { reader ->
        localProperties.load(reader)
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
        maven {
            url = uri("https://maven.pkg.github.com/obh-mobility/obh_android_sdk")
            credentials {
                username = localProperties.getProperty("gpr.user") ?: System.getenv("USERNAME")
                password = localProperties.getProperty("gpr.key") ?: System.getenv("GITHUB_TOKEN")
            }
        }

        maven {
            url 'https://jitpack.io'
        }
    }
}
  1. Add GitHub package credentials to local.properties or environment variables (See Generate GitHub token).

  2. Declare permissions in AndroidManifest.xml

<uses-feature
    android:name="android.hardware.bluetooth_le"
    android:required="true" />
<!-- Request legacy Bluetooth permissions on versions older than API 31 (Android 12). -->
<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.ACCESS_COARSE_LOCATION"
      />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"
  />

<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.BLUETOOTH_SCAN"
    android:usesPermissionFlags="neverForLocation"
    />
<uses-permission android:name="android.permission.INTERNET" />

Flutter

Add Github repository as project dependency in your pubspec.yaml:

dependencies:
  obh_flutter_sdk:
    git:
      url: git@github.com:obh-mobility/obh_flutter_sdk.git
      ref: <sdk_version>

Now, you can run dart pub get to fetch all dependencies.


Use

Entrypoint

The entrypoint of the SDK is the class ObhFlutterSdk. This class have 2 main parameters :

  • apiBaseUrl : The base url of the OBH api. This is used to select a specific environment.
  • token : Api access token

The class ObhFlutterSdk is immutable. Token can't be changed after instantiation. If you want to change the token/account used, create a new instance of ObhFlutterSdk.


Permissions

The OBH mount is connected with Bluetooth (BLE). Lots of SDK functions need to access device BLE. This implies to ask user the BLE access permission.

This SDK do not ask these permissions. For example, this can be done with the following dart method and package permission_handler:

flutter pub add permission_handler
Future<void> askPermission() async {
  await Permission.location.request();
  await Permission.bluetoothConnect.request();
  await Permission.bluetoothScan.request();
}

Application is responsible for the BLE availability. Be sure that BLE is turned on, available, and ask permission before calling any method of the SDK that need bluetooth. For further information, please have a look at the permission_handler package documentation.