'IOS Invoke SDK functions of SDK in capacitor plugin
Andoird was done without difficulty but I'm very new to IOS(OC/swift) Follow instruction of sdk doc, imported sdk into umbrella header Able to call sdk from main, The instruction was made with objectiveC, and I'm tryng invoke through swift instead, because the capacitor plugin seems requiring in swift?! However, the functions I trying to invoke seems not returning anything in console of Xcode, please help fix/debug, many thanks
import Foundation
import Capacitor
/**
* Please read the Capacitor iOS Plugin Development Guide
* here: https://capacitorjs.com/docs/plugins/ios
*/
@objc(ContactsPlugin)
public class ContactsPlugin: CAPPlugin {
@objc func echo(_ call: CAPPluginCall) {
let value = call.getString("value") ?? ""
let app = AppDelegate()
let initiate = app.application
print(initiate)
let sn = "TiHeSBp9BlLKFW6jgfondDy9ovkoUGwbsFLES3HYkRPQ48KL5aZe9dMtLCS/I44M/wVZWV0KUqUdJT1oq9FR//8F";
let v = app.scanAndConnect(sn:sn)
let c = app.lockerStatus(sn: sn)
print(c)
let pass = app.getPass(sn: sn)
call.success([
"value": pass
])
}
@objc func getContacts(_ call: CAPPluginCall) {
let value = call.getString("value") ?? ""
call.success([
"value": value
])
}
}
//import AirbnkSDK.h
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var sdk = AirbnkSDK() // AirbnkSDKDelegate()
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
AirbnkSDK.initAirbnkSDK("airbnb123456789")
return true
}
func scanAndConnect(sn:String) -> () {
AirbnkSDK.connect(toLock: sn)
}
func unlock(sn:String) -> (){
AirbnkSDK.unlock(sn)
}
func lockerStatus(sn:String) -> (){
AirbnkSDK.getLockStatus(sn)
}
func getPass(sn:String) -> (){
AirbnkSDK.getOneTimePassWord(sn)
}
}
about instruction for use sdk in IOS env:
Environmental requirements Minimum support for iOS10.0. iOS Static Library Integration Guide 1.Import the SDK Step 1: Prepare the header file and.A file. As shown in figure:
libAirbnkSDK.a is the static library, airbnksdk.h is the header file, lockrecord.h is a header file.
Step 2: Copy the file to the project directory, and check Destination. As shown in figure:
Step 3: Add SDK. The project directory is shown in the figure:
Set project properties Bluetooth permission Settings. You need to manually add the appropriate permissions in the info.plist file of the project, as shown in the figure:
Use the SDK in code The SDK needs to be initialized before invoking the functional interface.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[AirbnkSDK initAirbnkSDK];
return YES;
}
Set up the delegate where necessary
@interface LockViewController ()<AirbnkSDKDelegate>
@end
@implementation LockViewController
- (void)viewDidLoad {
[super viewDidLoad];
[AirbnkSDK handleDelegate:self];
}
Method invocation
Get the lock status:
[AirbnkSDK getLockStatus:SNINFO];
Lock status callback:
-(void) responeseLockStatus:(LockState) state {
if (state == LockStateLocked) {//door is locked
} else if(state == LockStateUnLock) {//door is unlocked
} else {//door lock not well
}
}
The function interface The SDK provides the mobile Bluetooth and Airbnk lock interface. The functions include connection lock, disconnection lock, access to lock state, access to lock power, unlock, lock, configure lock information, access to lock record and so on. The SDK interacts with the lock in an asynchronous callback mode. When the SDK calls the interface, if there is lock information and status return, the result will be returned in the proxy callback method. Sdkkey: the secret key of Airbnk lock SDK. The SDK needs to be called once during initialization. SNINFO: Airbnk lock unlocks the key. Each lock corresponds to a different SNINFO. The SDK needs to take SNINFO parameters when it interacts with the lock.
The connection lock method is called before any interaction with the lock. Connect the lock
+(void) connectToLock:(nullable NSString*) sninfo;
Parameter description:
sninfo Airbnk locks decryption string
The callback function
-(void) didConnectLock:(nullable NSString*) sninfo;
Disconnect lock
+(void) disconnectToLock:(nullable NSString*) sninfo;
Parameter description:
sninfo Airbnk locks decryption string
The callback function
-(void) didDisConnectLock:(nullable NSString*) sninfo;
Get lock status
+(void) getLockStatus:(nullable NSString*) sninfo;
Parameter description:
sninfo Airbnk locks decryption string
The callback function
-(void) responeseLockStatus:(LockState) state;
Parameter description:
LockState:
LockStateLocked,
LockStateUnLock,
LockStateLockedOpen,
LockStateLockedClose,
LockStateUnLockOpen,
LockStateUnLockClose,
LockStateJammed
Get lock power
+(void) getLockVoltage:(nullable NSString*) sninfo;
Parameter description:
sninfo Airbnk locks decryption string
The callback function
-(void) responseLockVoltage:(LockVoltage) voltage;
Parameter description:
LockVoltage:
LockVoltageHigh,
LockVoltageMiddle,
LockVoltageLow,
LockVoltageEmpty
Unlock
+(void) unlock:(nullable NSString*) sninfo;
Parameter description:
sninfo Airbnk locks decryption string
The callback function
-(void) responeseLockStatus:(LockState) state;
Parameter description:
LockState:
LockStateLocked,
LockStateUnLock,
LockStateLockedOpen,
LockStateLockedClose,
LockStateUnLockOpen,
LockStateUnLockClose,
LockStateJammed
Lock
+(void) lock:(nullable NSString*) sninfo;
Parameter description:
sninfo Airbnk locks decryption string
The callback function
-(void) responeseLockStatus:(LockState) state;
Parameter description:
LockState:
LockStateLocked,
LockStateUnLock,
LockStateLockedOpen,
LockStateLockedClose,
LockStateUnLockOpen,
For your reference, I past my implementations of Android part:
package com.magiclockers.contacts;
import android.app.Activity;
import android.os.Bundle;
import android.os.Looper;
import com.airbnk.sdk.MainApi;
import com.airbnk.sdk.callback.IConnectDeviceCallback;
import com.airbnk.sdk.callback.IDeviceStatusCallback;
import com.airbnk.sdk.callback.IUnlockCallback;
public class Api extends Activity {
private MainApi mainApi;
public String value = "init";
String key = "airbnb123456789";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extras = getIntent().getExtras();
//if (extras != null) {
final String sn = extras.getString("sn");
//}
try {
mainApi = new MainApi(Api.this, sn, key);
if (mainApi != null) {
System.out.print(mainApi);
mainApi.connect(new IConnectDeviceCallback() {
@Override
public void onSuccess() {
value = "connect succeed";
mainApi.unlock(sn, new IUnlockCallback() {
@Override
public void onSuccess() {
value = "unlock succeed";
if (mainApi != null) {
mainApi.disconnect();
}
}
@Override
public void onFailed(String errorMsg) {
value = errorMsg;
if (mainApi != null) {
mainApi.disconnect();
}
}
});
}
@Override
public void onFailed(String errorMsg) {
value = errorMsg;
if (mainApi != null) {
mainApi.disconnect();
}
}
}, new IDeviceStatusCallback() {
@Override
public void states(int i) {
value = "connect failed";
if (mainApi != null) {
mainApi.disconnect();
}
}
});
}
///
new android.os.Handler(Looper.getMainLooper()).postDelayed(
new Runnable() {
public void run() {
Api.this.finish();
if (mainApi != null) {
mainApi.disconnect();
}
}
},
7000);//force close activity and connection after a while
///
} catch (Exception e) {
Api.this.finish();
if (mainApi != null) {
mainApi.disconnect();
}
e.getCause();
e.printStackTrace();
}
//connect end
}
}
package com.magiclockers.contacts;
import android.app.Activity;
import android.content.Intent;
import com.getcapacitor.JSObject;
import com.getcapacitor.NativePlugin;
import com.getcapacitor.Plugin;
import com.getcapacitor.PluginCall;
import com.getcapacitor.PluginMethod;
import com.airbnk.sdk.callback.IGetDynamicPasswordCallback;
import com.airbnk.sdk.MainApi;
class ApiPass extends Activity {
}
@NativePlugin(
requestCodes = {ContactsPlugin.REQUEST_CONTACTS}
)
public class ContactsPlugin extends Plugin {
protected static final int REQUEST_CONTACTS = 12345; // Unique request code
private MainApi mainApi;
public String value = "init";
String sn = "TiHeSBp9BlLKFW6jgfondDy9ovkoUGwbsFLES3HYkRPQ48KL5aZe9dMtLCS/I44M/wVZWV0KUqUdJT1oq9FR//8FWVldClKlHSU9aKvRUf//BVlZXQpSpR0lPWir0VH//wVZWV0KUqUdJT1oq9FR//igVOBiVnz7W2LScEbB3TdUWjQH+gTO5sfaexY2pOf+39SoNpk07DhbbEfkoqemCgrwp++6XTAcupECUJbt9NgQ4qZlVxTNm7OgQ8TBSIt71KxQfNH0iMUHJ0KRMwBVXlysgpW7Tzt+sl3GoX3zF6n/BVlZXQpSpR0lPWir0VH//wVZWV0KUqUdJT1oq9FR//8FWVldClKlHSU9aKvRUf//BVlZXQpSpR0lPWir0VH//wVZWV0KUqUdJT1oq9FR//8FWVldC";
String key = "airbnb123456789";
@PluginMethod()
public void echo(PluginCall call) {// connect bluetooth and unlock
value = call.getString("value");
Intent intent = new Intent(this.getContext(), new Api().getClass());
intent.putExtra("sn", sn);
getActivity().startActivity(intent);
JSObject ret = new JSObject();
ret.put("value", value);
call.success(ret);
}
@PluginMethod()
public void getContacts(PluginCall call) {// get password
value = call.getString("value");
ApiPass api = new ApiPass();
mainApi = new MainApi(api, sn, key);
mainApi.getDynamicPassword(sn, new IGetDynamicPasswordCallback() {
@Override
public void onSuccess(String password) {
value = password;
}
@Override
public void onFailed(String errorMsg) {
value = errorMsg;
}
});
JSObject ret = new JSObject();
ret.put("value", value);
call.success(ret);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|