Got Stuck?
Forum for Lovense Developers
Support
Document Feedback

Unity Plugin - Android SDK

Introduction

This Unity plug-in can be applied to Android platform for connecting Lovense toys and sending control commands.

Compatibility

Unity version>= 2019.4.0

Api Compatibility Level:

  • For .Net 4.x, the import package name is androidPlugin_.Net4.x.unitypackage. (be sure to set the Target API Level to 28)
  • For .Net Framework, the import package name is androidPlugin_.NetFramework.unitypackage. (be sure to set the Target API Level to 31)
  • For .Net Standard 2.0, the import package name isandroidPlugin_.NetStandard2.0.unitypackage. (be sure to set the Target API Level to 28)
  • For .Net Standard 2.1, the import package name is androidPlugin_.NetStandard2.1.unitypackage.(be sure to set the Target API Level to 31)

Import the plugin

  1. Download the plugin package from here. The latest version of the current plugin is V1.3 click to view version records

    androidPlugin_.Net4.x.unitypackage

    androidPlugin_.NetFramework.unitypackage

    androidPlugin_.NetStandard2.0.unitypackage

    androidPlugin_.NetStandard2.1.unitypackage

  2. Import the appropriate package based on your project's Api Compatibility Level.

    image

  3. Select all options and click import.

    image

  4. After importing, you need to click Tools ->LovenseAndroidDevelopment on the toolbar, enter the developer token, click "Save", and click "Copy and modify AndroidManifest.xml and gradle file"

    imageimage

  5. The file Assets/StreamingAssets/LovenseToySupportConfig.json is the configuration file for the toy support directive, and the version field represents the version of the information. The current latest version is 1.3. If it is older than this version, it is recommended to download and replace the file.

Dowload: LovenseToySupportConfig.json

The LovenseDemo folder is an example that you can refer to. Removing it won't affect the functionality.

Guide

1. Search toys.

public class YourLovenseToyUI: MonoBehaviour {
    private OnSearchToyListener onSearchToyListener;;
	 // do search
   private void DoSearch() {
       LovenseAndroidSDK.GetInstance().SearchToys(onSearchToyListener);
   }

    private void OnSearchErrorFunc(string code,string msg){
        //code runs when search toys has Error.
    }

    private void OnSearchToysFunc(LovenseAndroidToy toys){
        //code runs when lovense toy is found.
    }

    private void OnFinishFunc(){
        //code runs when search toys is completed.
    }

   void Awake(){
        // Add listener
       onSearchToyListener = new OnSearchToyListener();
       onSearchToyListener.initCallback(OnSearchErrorFunc, OnSearchToysFunc, OnFinishFunc);
   }
}

2. Stop searching for toys

public class YourLovenseToyUI: MonoBehaviour {
	 // do stop search
   private void Dostop() {
      LovenseAndroidSDK.GetInstance().StopSearching();
   }
}

3. Connect or DisConnect Toys

public class YourLovenseToyUI: MonoBehaviour {
   private OnConnectListener onConnectListener;
	 // do connect
   private void Connect(List<string> toyIds) {
      LovenseAndroidSDK.GetInstance().ConnectToys(toyIds, onConnectListener);
   }
    //do disconnect
    private void DisConnect(List<string> toyIds) {
       LovenseAndroidSDK.GetInstance().DisConnectToys(toyIds); 
    }

    //callback function,
   private void OnConnectFunc(string toysId, string status){
        //code runs when toy's connection status has changed.
    }

    //callback function,
   private void OnConnectErrorFunc(string toysId, string status){
        //An error occurred while connecting
    }
  

   void Awake(){
        // Add listener
    	onConnectListener = new OnConnectListener();
       onConnectListener.initCallBack(OnConnectFunc, OnConnectErrorFunc);
       LovenseAndroidSDK.GetInstance().AddListener(toyId, onConnectListener);
   }

}

4. Battery monitoring

public class YourLovenseToyUI: MonoBehaviour {

    private OnCallBackBatteryListener onCallBackBatteryListener;
	 
    // do get toy's battery
   private void GetBattery(string toyId) {
       LovenseAndroidSDK.GetInstance().SendFunctionWithoutValue(toyId, LovenseCommandType.GET_BATTERY);
   }

    //callback function
   private void OnCallBackBatteryListenerFunc(string toyId, int battery){
        //code runs when Obtain battery info 
    }

   void Awake(){
        // Add listener
    	onCallBackBatteryListener = new OnCallBackBatteryListener();
       onCallBackBatteryListener.initCallBack(OnCallBackBatteryListenerFunc);
        LovenseAndroidSDK.GetInstance().AddListener(toyId, onCallBackBatteryListener);
   }

}

5. Obtain parameters such as device type

public class YourLovenseToyUI: MonoBehaviour {

    private OnCallBackDeviceTypeListener onCallBackDeviceType;
	 
    // do get toy's type
   private void GetDeviceType(List<string> toyIds) {
       LovenseAndroidSDK.GetInstance().SendFunctionWithoutValue(toyId, LovenseCommandType.GET_DEVICETYPE);
   }

    //callback function
   private void OnCallBackDeviceTypeListenerFunc(string toyId, LovenseAndroidToy lovenseToy){
        //code runs when obtain parameters such as device type
    }

   void Awake(){
        // Add listener
    	 onCallBackDeviceType = new OnCallBackDeviceTypeListener();
        onCallBackDeviceType.initCallBack(OnCallBackDeviceTypeListenerFunc);
        LovenseAndroidSDK.GetInstance().AddListener(toyId, onCallBackDeviceType);
   }

}

6. Send Functions

public class YourLovenseToyUI: MonoBehaviour {
	 
    // do send functions
   private void DoSendFunction(List<string> toyIds, List<LovenseCommand> commands, float timeSec, float loopRunningSec, float loopPauseSec) {
       LovenseAndroidSDK.GetInstance().SendFunctions(toyIds, commands,timeSec,loopRunningSec,loopPauseSec);
   }

    //callback function
   private void OnSendMultipleCommandsEvent(string toyid,string msg){
        //code to execute commands result
    }

  

   void Awake(){
        // Add listener
    	LovenseAndroidSDK.GetInstance().AddSendMultipleCommandListener( OnSendMultipleCommandsEvent);
   }

   void OnDestroy()
    {
        //Remove listener
        LovenseAndroidSDK.GetInstance().RemoveSendMultipleCommandListener(OnSendMultipleCommandsEvent);
    }
}

7. Send Patterns

public class YourLovenseToyUI: MonoBehaviour {
	
    //do 
   private void SendPattern(List<string> toyIds, List<LovenseCommandType> typeList, List<int> strengthValues, float timeSec,int intervalsMs) {
      LovenseAndroidSDK.GetInstance().SendPattern(toyIds, typeList, valueList, timeSec, intervalsMs);
   }

     //callback function
   private void OnSendMultipleCommandsEvent(string toyid,string msg){
        //code to execute commands result
    }

  void Awake(){
        // Add listener
    	LovenseAndroidSDK.GetInstance().AddSendMultipleCommandListener( OnSendMultipleCommandsEvent);
   }

  void OnDestroy()
    {
        //Remove listener
        LovenseAndroidSDK.GetInstance().RemoveSendMultipleCommandListener(OnSendMultipleCommandsEvent);
    }
}

8. SendPreset

public class YourLovenseToyUI: MonoBehaviour {
	
    //do 
   private void SendPreset(List<string> toyIds, int mode, float timeSec) {
      LovenseAndroidSDK.GetInstance().SendPreset(toyIds, mode, timeSec);
   }

     //callback function
   private void OnSendMultipleCommandsEvent(string toyid,string msg){
        //code to execute commands result
    }

  void Awake(){
        // Add listener
    	LovenseAndroidSDK.GetInstance().AddSendMultipleCommandListener( OnSendMultipleCommandsEvent);
   }

  void OnDestroy()
    {
        //Remove listener
        LovenseAndroidSDK.GetInstance().RemoveSendMultipleCommandListener(OnSendMultipleCommandsEvent);
    }
}

9. Stop Toys

public class YourLovenseToyUI: MonoBehaviour {
	
    
   private void YourMethod(List<string> toyIds) {
      LovenseAndroidSDK.GetInstance().StopAll(toyIds);
   }

     //callback function
   private void OnSendMultipleCommandsEvent(string toyid,string msg){
        //code to execute commands result
    }

  void Awake(){
        // Add listener
    	LovenseAndroidSDK.GetInstance().AddSendMultipleCommandListener( OnSendMultipleCommandsEvent);
   }

  void OnDestroy()
    {
        //Remove listener
        LovenseAndroidSDK.GetInstance().RemoveSendMultipleCommandListener(OnSendMultipleCommandsEvent);
    }
}

Interface

LovenseAndroidSDK

Method

GetInstance
SearchToys
  • Description:

    Search for the toys, when a toy is discovered, the OnSearchToyListener listener will be triggered.

  • Parameters:

    NameTypeDefaultRequiredDescription
    listenerOnSearchToyListeneryescallback listener
  • Return: None

ConnectToys
  • Description:

    Connect the toys, when a toy's Connection status is changed, the OnConnectListener listener will be triggered.

  • Parameters:

    NameTypeDefaultRequiredDescription
    toyIdsList<string>yesThe toys you want to connect
    listenerOnConnectListeneryescallback listener
  • Return: None

DisConnectToys
  • Description:

    DisConnect the toys.

  • Parameters:

    NameTypeDefaultRequiredDescription
    toyIdsList<string>yesThe toys you want to disconnect
  • Return: None

SendFunctions
  • Description:

    Send function commands to the toys.

  • Parameters:

    NameTypeDefaultRequiredDescription
    toyIdsList<string>yesThe toys you want to control
    commandsList<LovenseCommand>yesSee LovenseCommand
    timeSecfloatyesThe total runtime in second, "0" = indefinite time length.
    loopRunningSecfloat0notime running for each command loop in second
    loopPauseSecfloat0notime gap for each command loop in second
  • Return: None

SendPattern
  • Description:

    Send a series of functions to the toys, they will execute all the functions in types according to the order of the level in values with the interval of intervalsMs.

  • Parameters:

    NameTypeDefaultRequiredDescription
    toyIdsList<string>yesThe toys you want to control
    typesList<LovenseCommandType>yesSee LovenseCommandType
    strengthValuesList<int>yesThe order of the levels
    timeSecfloatyesThe total runtime in second, "0" = indefinite time length.
    intervalsMsint150noThe level interval in milliseconds, should be greater than 100, default 150
  • Return: None

SendPreset
  • Description:

    Send a preset command to the toys.

  • Parameters:

    NameTypeDefaultRequiredDescription
    toyIdsList<string>yesThe toys you want to control,
    modeintyesThe preset name to send, we provide four preset patterns: 0:earthquake,1:fireworks,2:pulse,3:wave
    timeSecfloatyesThe total runtime in second, "0" = indefinite time length.
  • Return: None

StopAll
  • Description:

    Send a stop command to the toys.

  • Parameters:

    NameTypeDescription
    toyIdsList<string>The toys you want to stop
  • Return: None

AddSendMultipleCommandListener
  • Description:

    Add callback listening with multiple command executions

  • Parameters:

    NameTypeDescription
    callbackFuncUnityAction<string,string>the parameters are toyId and message
    • Return: None
RemoveSendMultipleCommandListener
  • Description:

    Remove callback listening with multiple command executions

  • Parameters:

    NameTypeDescription
    callbackFuncUnityAction<string,string>the parameters are toyId and message
  • Return: None

GetSupportCommandsByType(string type)
  • Description:

    Obtain Command types supported by toys through their types

  • Parameters:

    NameTypeDescription
    typestringthe type of toy
  • Return: List<LovenseCommandType>

SendMultiplyCommands(string toyId,List<LovenseCommand> commands)
  • Description:

    Send multiply commands with different commands directly to the firmware. Currently, only Lapis toys support this function.

  • Parameters:

    NameTypeDescription
    idstringthe id of toy
    commandsList<LovenseCommand>Currently, only Lapics toys can call this function. The type attribute of LovenseCommand needs to be VIBRATE1, VIBRATE2, VIBRATE3, and the value attribute of LovenseCommand has a range of -1-20. Setting -1 means the previous value will not be changed.

LovenseAndroidToy

  • Description:

    The information of the Lovense toy

  • Properties:

    NameTypeDescription
    toyIdstringthe toy's id
    versionintthe toy's version
    typestringthe toy's type
    statusintthe toy's connection status: 1 connected -1 dis connected other:unknown
    batteryintthe toy's battery
    rssiintthe toy's rssi
    deviceNamestringthe toy's name
    uuidstringthe toy's uuid
    deviceTypestringthe toy's deviceType
    macAddressstringthe toy's macaddress

LovenseCommand

  • Description:

    The toy control command

  • Properties:

    NameTypeDescription
    typeLovenseCommandTypeSee below
    valueintFunction level

Function Level:

TypeRange
VIBRATE0 - 20
VIBRATE10 - 20
VIBRATE20 - 20
VIBRATE30 - 20
ROTATE0 - 20
PUMP0 - 3
FINGERING0 - 20
THRUSTRING0 - 20
DEPTH0 - 3
GET_DEVICETYPE
GET_BATTERY

LovenseCommandType

  • Description:

    The enum of the toy function type.

  • Enum:

    Value
    VIBRATE
    VIBRATE1
    VIBRATE2
    VIBRATE3
    ROTATE
    PUMP
    THRUSTRING
    FINGERING
    DEPTH
    GET_DEVICETYPE
    GET_BATTERY

OnSearchToyListener

  • Description:

    The callback listener for search toys.

  • Properties:

    NameTypeDescription
    onErrorEventUnityAction<string,string>error callback function
    onSearchToyEventUnityAction<LovenseAndroidToy>get callback function
    onFinishSearchUnityActioncompleted callback function

Method

initCallback
  • Description:

    init callback.

  • Parameters:

    NameTypeDefaultRequiredDescription
    searchErrorFuncUnityAction<string, string>yescallback, params is code and message
    toyGetFuncUnityAction<LovenseAndroidToy>yescallback
    searchCompletedFuncUnityActionyescallback
  • Return: None

OnConnectListener

  • Description:

    The callback listener for connect toys.

  • Properties:

    NameTypeDescription
    onConnectFuncUnityAction<string,string>callback function
    onErrorFuncUnityAction<string,string>callback function

Method

initCallback
  • Description:

    init callback.

  • Parameters:

    NameTypeDefaultRequiredDescription
    funConnectUnityAction<string, string>yescallback function, params is toyId and status
    funErrorUnityAction<string, string>yescallback function, params is code and message
  • Return: None

OnCallBackBatteryListener

  • Description:

    The callback listener for obtains toy's battery.

  • Properties:

    NameTypeDescription
    onCallBackBatteryListenerUnityAction<string,int>callback function

Method

initCallback
  • Description:

    init callback.

  • Parameters:

    NameTypeDefaultRequiredDescription
    funcUnityAction<string, int>yescallback function, params is toyId and battery
  • Return: None

OnCallBackDeviceTypeListener

  • Description:

    The callback listener for obtains toy's battery.

  • Properties:

    NameTypeDescription
    onCallBackDeviceTypeListenerFuncUnityAction<string,LovenseAndroidToy>callback function

Method

initCallback
  • Description:

    init callback.

  • Parameters:

    NameTypeDefaultRequiredDescription
    funcUnityAction<string,LovenseAndroidToy>yescallback function
  • Return: None

Version Records

[Version1.3] - 2023-11-21

  • 1.Add Depth command.

[Version1.2] - 2023-11-01

  • 1.Add Vibrate3 command and Multiply(The Lapics toy is the only one that is currently supported) command.

[Version1.1] - 2023-09-12

  • 1.Add config file about toy support instructions. And add a function that can query toy support instructions

  • 2.Change type of 'strengthValues' int[] to List<int> in pattern function

  • 3.Change time type int to float.

[Version1.0] - 2023-08-20

  • 1.Functions

  • 2.Pattern

  • 3.Preset

  • 4.Connect/DisConnect

  • 5.Type/Battery