|
1 | 1 | package com.example.my_app |
2 | 2 |
|
3 | | -import androidx.annotation.NonNull; |
| 3 | +import androidx.annotation.NonNull |
4 | 4 | import io.flutter.embedding.android.FlutterActivity |
5 | 5 | import io.flutter.embedding.engine.FlutterEngine |
6 | 6 | import io.flutter.plugins.GeneratedPluginRegistrant |
| 7 | +import io.flutter.plugin.common.MethodChannel |
7 | 8 |
|
8 | 9 | class MainActivity: FlutterActivity() { |
| 10 | + private val CHANNEL = "gocf.dev/sdk" |
| 11 | + private var sdkClient: ocfclient.Ocfclient_? = null; |
| 12 | + |
9 | 13 | 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 | + } |
11 | 85 | } |
12 | 86 | } |
0 commit comments