Unity Plugin - Mac

Introduction

This Unity plugin allows Mac devices to connect Lovense toys and send control commands.

Note: A Bluetooth adapter is required to use this plugin.

Compatibility

Unity version>= 2019.4.0

Api Compatibility Level:

  • For .Net 4.x, the import package name is macPlugin_.Net4.x.unitypackage.
  • For .Net Framework, the import package name is macPlugin_.NetFramework.unitypackage.
  • For .Net Standard 2.0, the import package name ismacPlugin_.NetStandard2.0.unitypackage.
  • For .Net Standard 2.1, the import package name is macPlugin_.NetStandard2.1.unitypackage.

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

    macPlugin_.Net4.x.unitypackage

    macPlugin_.NetFramework.unitypackage

    macPlugin_.NetStandard2.0.unitypackage

    macPlugin_.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. 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.2. 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 for attached toys.

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

   //
	private void OnGetToysEvent(LovenseOSXToy newToy) {
       // Show the search result in the UI
	}

   private void OnScanEnd() {
       // on scan ended.
   }

   void Awake(){
      // Add listener
		 LovenseOSXTools.onOSXGetToysEvent.AddListener(OnGetToysEvent);
   }

	void OnDestory() {
		// Remove listener
		LovenseOSXTools.onOSXGetToysEvent.RemoveListener(OnGetToysEvent);
	}
}

2. Connect or disconnect toys

public class YourLovenseToyUI: MonoBehaviour {
   
   //do search toys
   private void DoConnect(List<string> ids) {
        LovenseOSXTools.GetInstance().ConnectToys(ids);
   }

   private void DoDisConnect(List<string> ids) {
        LovenseOSXTools.GetInstance().DisConnectToys(ids);
   }

   private void onToysConnectEvent(string toyId,int status,string msg){
   
   }

   void Awake() {
      // Add listener
      LovenseOSXTools.onOSXConnectEvent.AddListener(onToysConnectEvent);
   } 
 
   void OnDestroy() {
       // Remove listener
     LovenseOSXTools.onOSXConnectEvent.RemoveListener(onToysConnectEvent);
   }
}

3. Send commands

3.1 Get battery

public class YourLovenseToyUI : MonoBehaviour {
   
  private void DoGetBattery(string id) {
    LovenseOSXTools.GetInstance().SendFunctionWithoutValue(id, LovenseCommandType.GET_BATTERY);
  }

  private void onCallBackBatteryListenerFunc(string toyId, int battery){
  
  }

  void Awake() {
    // Add listener
    LovenseOSXTools.onOSXGetBatteryEvent.AddListener(onCallBackBatteryListenerFunc);
  } 

  void OnDestroy() {
    // Remove listener:
     LovenseOSXTools.onOSXGetBatteryEvent.RemoveListener(onCallBackBatteryListenerFunc);
  }
}

3.2 Send function commands

public class YourLovenseToyUI : MonoBehaviour {
   
  private void DoSendCommands(List<string> toyIds, List<LovenseCommand> commands, float timeSec, float loopRunningSec, float loopPauseSec) {
     LovenseOSXTools.GetInstance().SendFunctions(toyIds,commands,timeSec,loopRunningSec,loopPauseSec);
  }

  private void onToysCommandEvent(string toyId, int status, string msg){
      //...
  }

  void Awake() {
    // Add listener
     LovenseOSXTools.onOSXCommandEvent.AddListener(onToysCommandEvent);
  }

  void OnDestroy() {
     // Remove listener:
     LovenseOSXTools.onOSXCommandEvent.RemoveListener(onToysCommandEvent);
  }
}

3.3 Send pattern commands

public class YourLovenseToyUI : MonoBehaviour {

  private void DoSendPattern(List<string> toyIds, List<LovenseCommandType> typeList, List<int> strengthValues, float timeSec, int intervalsMs) {
      LovenseOSXTools.GetInstance().SendPattern(toyIds, typeList, strengthValues, timeSec,timeMs);
  }

  private void onToysCommandEvent(string toyId, int status, string msg){
      //...
  }

  void Awake() {
    // Add listener
     LovenseOSXTools.onOSXCommandEvent.AddListener(onToysCommandEvent);
  }

  void OnDestroy() {
     // Remove listener:
     LovenseOSXTools.onOSXCommandEvent.RemoveListener(onToysCommandEvent);
  }
}

strengthValues: Changes in intensity

intervalsMs: Time interval for intensity switching in milliseconds

timeSec: Total running time in seconds

3.4 Send preset commands

public class YourLovenseToyUI : MonoBehaviour {

   private void DoSendPreset(List<string> ids, int mode, float timeSec) {
      LovenseOSXTools.GetInstance().SendPreset(ids, mode, timeSec);
   }

  private void onToysCommandEvent(string toyId, int status, string msg){
      //...
  }

  void Awake() {
    // Add listener
     LovenseOSXTools.onOSXCommandEvent.AddListener(onToysCommandEvent);
  }

  void OnDestroy() {
     // Remove listener:
     LovenseOSXTools.onOSXCommandEvent.RemoveListener(onToysCommandEvent);
  }
}

mode: we provide four preset patterns:0 is earthquake,1 is fireworks,2 is pulse,3 is wave

3.5 Send stop command

public class YourLovenseToyUI : MonoBehaviour {

   private void DoSendStop(List<string> ids) {
       LovenseOSXTools.GetInstance().StopAll(ids);
   }

  private void onToysCommandEvent(string toyId, int status, string msg){
      //...
  }

  void Awake() {
    // Add listener
     LovenseOSXTools.onOSXCommandEvent.AddListener(onToysCommandEvent);
  }

  void OnDestroy() {
     // Remove listener:
     LovenseOSXTools.onOSXCommandEvent.RemoveListener(onToysCommandEvent);
  }
}

3.6 Add interface for Data Reporting

public class YourLovenseToyUI : MonoBehaviour {

   private void DoAddDataReporting(string toyId,bool open) {
       LovenseOSXTools.GetInstance().SetDataReportingMode(toyId,open); //Only by using this function and setting it to true will the toy data be reported
   }


   //callback function.
   private void OnGetReportingData(string id,LovenseDataReportingEventType type,int target,int value){
      //code 
   }

   void Awake() {
        LovenseOSXTools.GetInstance().AddDataReportingListener(toyId,OnGetReportingData);
   }

   void OnDestroy() {
    // Remove listener:
        LovenseOSXTools.GetInstance().RemoveDataReportingListener(toyId,OnGetReportingData);
   }
}

Interface

LovenseOSXTools

Method

GetInstance
SendFunctionWithoutValue
  • Description:

    Send commands that do not require numerical values.

  • Parameters:

    NameTypeDefaultRequiredDescription
    toyIdstringyesThe toy ID you want to obtain battery life
    typeLovenseCommandTypeyesOnly for those commands which do not require a numerical value, like LovenseCommandType.GET_BATTERY, LovenseCommandType.GET_DEVICETYPE,LovenseCommandType.CHECK_DATAREPORTING
  • Return: None

SearchToys
  • Description:

    Search for nearby toys, when the search is processing, the onOSXGetToysEvent event will be triggered,And the end of the scan will trigger [onGetToyScanEnd] event

  • Parameters: None

  • Return: None

SendFunctions
  • Description:

    Send Function commands to the toys.

  • Parameters:

    NameTypeDefaultRequiredDescription
    toyIdsList<string>yesThe toys you want to control
    commandsList<LovenseCommand>yesSee LovenseCommand
    timeSecfloatyesTotal running time in second, 0 represents no limit
    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
    toysList<string>yesThe toys you want to control
    modeintyesThe preset name to send, we provide four preset patterns: 0 is earthquake,1 is fireworks,2 is pulse,3 is 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

SetDataReportingMode
  • Description:

    Set the switch for toy data reporting function.

  • Parameters:

    NameTypeDefaultRequiredDescription
    toyIdstringyesThe toys you want to set
    openboolyesIs the toy enabled for data reporting
  • Return: None

AddDataReportingListener
  • Description:

    Add a data reporting listener.

  • Parameters:

    NameTypeDescription
    idstringThe toys you want to add data reporting listener
    eventUnityAction(string toyId,LovenseDataReportingEventType type,int target,int value)callback function
  • Return: None

RemoveDataReportingListener
  • Description:

    Remove a data reporting listener.

  • Parameters:

    NameTypeDescription
    toyIdsstringThe toys you want to remove data reporting listener
    eventUnityAction(string toyId,LovenseDataReportingEventType type ,int target ,int value)callback function
  • Return: None

GetSupportCommandsByType(string type)
  • Description:

    Obtain command types supported by toys.

  • Parameters:

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

Event

onOSXGetToysEvent
  • Description:

    Triggered when a new toy is discovered

  • Callback Parameters:

    NameTypeDescription
    toyLovenseOSXToythe new toy is discovered
onOSXGetTypeEvent
  • Description:

    Triggered after getting toy type is completed

  • Callback Parameters:

    NameTypeDescription
    toyLovenseOSXToythe toy's ID
    typestringthe toy's type
onOSXGetBatteryEvent
  • Description:

    Triggered when getting toy's battery is complete.

  • Callback Parameters:

    NameTypeDescription
    toyIdstringThe toy's ID
    batterystringThe toy's battery life. Will have a s prefix when the toy is in a status that the return value is not reliable. e.g. When the toy is viberating and toy's battery life is 90, the return value will be s90
onOSXConnectEvent
  • Description:

    Triggered when connected toy's connection status is changed.

  • Callback Parameters:

    NameTypeDescription
    toyIdstringThe toy's ID
    statusintConnection status
    msgstringerror messaqge
  • status:

    ValueDescription
    -1Toy connect breaked
    0Toy connect failed
    1Toy connect success
onOSXCommandEvent
  • Description:

    Triggered when an error occurs during connection or control

  • Callback Parameters:

    NameTypeDescription
    idstringtoy's ID
    statusintstatus
    msgmsgerror msg
  • status:

    ValueDescription
    -1execute error,and msg is error tip.
    0execute error,and msg is command type.
    1execute success,and msg is command type.
onGetToyDataReportingEnabled
  • Description:

    Triggered when send function SendFunctionWithoutValue(id,LovenseCommandType.CHECK_DATAREPORTING)

  • Callback Parameters:

    NameTypeDescription
    idstringtoy's ID

LovenseOSXToy

  • Description:

    The information of the Lovense toy

  • Properties:

    NameTypeDescription
    namestringThe toy's name
    identifierstringThe toy's ID
    typestringThe toy's type
    versionstringThe toy's version
    macAddressstringThe toy's macaddress
    isConnectedboolThe toy's connection status
    rssiintThe toy's rssi
    batteryintThe toy's battery level, from 0 to 100 ,SendFunctionWithoutValue(id,LovenseCommandType.GETBATTERY) needs to be called to update
    isSupportDataReportingboolDetermine if the toy currently support data reporting. Need the toy to be connected in order to synchronize.

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
THRUSTING0 - 20
FINGERING0 - 20
SUCTION0 - 20
DEPTH0 - 3
GETBATTERY

LovenseCommandType

  • Description:

    The enum of the toy function type.

  • Enum:

    Value
    VIBRATE
    VIBRATE1
    VIBRATE2
    VIBRATE3
    ROTATE
    PUMP
    THRUSTING
    FINGERING
    DEPTH
    GET_DEVICETYPE
    GET_BATTERY
    CHECK_DATAREPORTING

The DEPTH command is a setting command that requires the THRUSTRING command to take effect.

LovenseDataReportingEventType

  • Description:

    The enum of the toy data reporting event type.

  • Enum:

    ValueDescription
    KEY_DOWNTrigger when a key is pressed
    KEY_UPTrigger when a key is up
    VIBTATETrigger when the toy vibrates
    ROTATETrigger when the toy rotate
    SHAKETrigger when the toy shake
    MOVEMENTTrigger when the toy movement
    DEEPTrigger when the toy vibrates
    AIR_INTrigger when the toy is inflated
    AIR_OUTTrigger when the toy deflates
    AIR_CLOSETrigger when inflating or deflating is turned off
    AIR_OPENTrigger when inflating or deflating is turned on
    BATTERYTrigger when the battery level changes
    SHAKE_TIMESTrigger when user shakes the toy every time

Version Records

[Version1.3] - 2023-11-13

  • 1.Command sending optimization.

[Version1.2] - 2023-10-20

  • 1.Add Vibrate3 command.

[Version1.1] - 2023-09-28

  • 1.Add shake event.

[Version1.0] - 2023-09-21

  • 1.Functions

  • 2.Pattern

  • 3.Preset

  • 4.Connect/DisConnect

  • 5.Type/Battery

  • 6.Data Reporting Interface