Skip to content
This repository was archived by the owner on Feb 23, 2023. It is now read-only.

Commit a7df58d

Browse files
authored
Merge pull request #4 from plgd-dev/androidWrapper
implement android wrapper for ocfclient
2 parents 8288563 + 22c2289 commit a7df58d

6 files changed

Lines changed: 118 additions & 16 deletions

File tree

android/app/build.gradle

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,15 @@ flutter {
5959
source '../..'
6060
}
6161

62+
repositories {
63+
flatDir {
64+
dirs './libs'
65+
}
66+
}
67+
6268
dependencies {
6369
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
70+
implementation (name:'ocfclient', ext:'aar')
6471
testImplementation 'junit:junit:4.12'
6572
androidTestImplementation 'androidx.test:runner:1.1.1'
6673
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'

android/app/libs/.gitkeep

Whitespace-only changes.
Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,86 @@
11
package com.example.my_app
22

3-
import androidx.annotation.NonNull;
3+
import androidx.annotation.NonNull
44
import io.flutter.embedding.android.FlutterActivity
55
import io.flutter.embedding.engine.FlutterEngine
66
import io.flutter.plugins.GeneratedPluginRegistrant
7+
import io.flutter.plugin.common.MethodChannel
78

89
class MainActivity: FlutterActivity() {
10+
private val CHANNEL = "gocf.dev/sdk"
11+
private var sdkClient: ocfclient.Ocfclient_? = null;
12+
913
override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
10-
GeneratedPluginRegistrant.registerWith(flutterEngine);
14+
GeneratedPluginRegistrant.registerWith(flutterEngine)
15+
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler { call, result ->
16+
when (call.method) {
17+
"initialize" -> this.initializeOCFClient(result)
18+
"discover" -> this.discoverDevices(result)
19+
"own" -> this.ownDevice(call.arguments(), result)
20+
"onboard" -> this.onboardDevice(call.arguments(), result)
21+
"offboard" -> this.offboardDevice(call.arguments(), result)
22+
"disown" -> this.disownDevice(call.arguments(), result)
23+
else -> result.notImplemented()
24+
}
25+
}
26+
}
27+
28+
private fun initializeOCFClient(@NonNull result: MethodChannel.Result) {
29+
try {
30+
if (sdkClient != null) {
31+
return
32+
}
33+
sdkClient = ocfclient.Ocfclient_()
34+
sdkClient!!.initialize()
35+
result.success(true)
36+
} catch (e: Exception) {
37+
sdkClient = null
38+
result.error("-1", e.message, "")
39+
}
40+
}
41+
42+
private fun discoverDevices(@NonNull result: MethodChannel.Result) {
43+
try {
44+
var devices = sdkClient!!.discover()
45+
result.success(devices)
46+
} catch (e: Exception) {
47+
result.error("-1", e.message, "")
48+
}
49+
}
50+
51+
private fun ownDevice(args: Map<String,String>?, result: MethodChannel.Result) {
52+
try {
53+
sdkClient!!.ownDevice(args!!["deviceID"], args!!["accessToken"])
54+
result.success(Any())
55+
} catch (e: Exception) {
56+
result.error("-1", e.message, "")
57+
}
58+
}
59+
60+
private fun onboardDevice(args: Map<String,String>?, result: MethodChannel.Result) {
61+
try {
62+
sdkClient!!.onboardDevice(args!!["deviceID"], args!!["authorizationProvider"], args!!["cloudURL"], args!!["authCode"], args["cloudID"])
63+
result.success(Any())
64+
} catch (e: Exception) {
65+
result.error("-1", e.message, "")
66+
}
67+
}
68+
69+
private fun offboardDevice(args: Map<String,String>?, result: MethodChannel.Result) {
70+
try {
71+
sdkClient!!.offboardDevice(args!!["deviceID"])
72+
result.success(Any())
73+
} catch (e: Exception) {
74+
result.error("-1", e.message, "")
75+
}
76+
}
77+
78+
private fun disownDevice(args: Map<String,String>?, result: MethodChannel.Result) {
79+
try {
80+
sdkClient!!.disownDevice(args!!["deviceID"])
81+
result.success(Any())
82+
} catch (e: Exception) {
83+
result.error("-1", e.message, "")
84+
}
1185
}
1286
}

lib/models/deviceDetails.dart

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class DeviceDetails {
1616

1717
DeviceDetails.fromJson(Map<String, dynamic> json) {
1818
id = json['ID'];
19+
1920
device =
2021
json['Device'] != null ? new Device.fromJson(json['Device']) : null;
2122
isSecured = json['IsSecured'];
@@ -37,12 +38,25 @@ class DeviceDetails {
3738
}
3839
}
3940

41+
class LocalizedString {
42+
String language;
43+
String value;
44+
LocalizedString({
45+
this.language,
46+
this.value,
47+
});
48+
LocalizedString.fromJson(Map<String, dynamic> json) {
49+
language = json['language'];
50+
value = json['value'];
51+
}
52+
}
53+
4054
class Device {
4155
String id;
4256
List<String> resourceTypes;
4357
List<String> interfaces;
4458
String name;
45-
String manufacturerName;
59+
List<LocalizedString> manufacturerName;
4660
String modelNumber;
4761

4862
Device(
@@ -58,7 +72,12 @@ class Device {
5872
resourceTypes = json['rt'].cast<String>();
5973
interfaces = json['if'].cast<String>();
6074
name = json['n'];
61-
manufacturerName = json['dmn'];
75+
if (json['dmn'] != null) {
76+
manufacturerName = new List<LocalizedString>();
77+
json['dmn'].forEach((v) {
78+
manufacturerName.add(new LocalizedString.fromJson(v));
79+
});
80+
}
6281
modelNumber = json['dmno'];
6382
}
6483
}
@@ -114,7 +133,7 @@ class Resources {
114133
String deviceID;
115134
int instanceID;
116135
String title;
117-
String supportedContentTypes;
136+
List<String> supportedContentTypes;
118137

119138
Resources(
120139
{this.id,

lib/services/ocfClient.dart

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,21 @@ class OCFClient {
1616
}
1717
return false;
1818
}
19-
19+
2020
static Future<List<DeviceDetails>> getDevices() async {
2121
if (!_isInitialized) {
2222
print("OCF Client is not initialized");
2323
return null;
2424
}
25-
var devicesJSON = await _nativeChannel.invokeMethod('discover');
26-
var devicesJSONObjs = jsonDecode(devicesJSON) as List;
27-
try {
28-
return devicesJSONObjs.map((deviceJson) => DeviceDetails.fromJson(deviceJson)).toList();
29-
} catch (err) {
30-
print(err);
31-
}
32-
return [];
25+
var devicesJSON = await _nativeChannel.invokeMethod('discover');
26+
List devicesJSONObjs = jsonDecode(devicesJSON);
27+
try {
28+
return devicesJSONObjs
29+
.map((deviceJson) => DeviceDetails.fromJson(deviceJson))
30+
.toList();
31+
} catch (err) {
32+
print(err);
33+
}
34+
return [];
3335
}
34-
}
36+
}

native/build-android.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
CC=C:/Users/admin/AppData/Local/Android/Sdk/ndk/21.3.6528147/toolchains/llvm/prebuilt/windows-x86_64/bin/clang gomobile bind --target android -bundleid com.go-ocf -o ../android/libs/go.ocfclient.gojni.aar
1+
CC=C:/Users/admin/AppData/Local/Android/Sdk/ndk/21.3.6528147/toolchains/llvm/prebuilt/windows-x86_64/bin/clang gomobile bind -o ../android/app/libs/ocfclient.aar

0 commit comments

Comments
 (0)