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 isandroidPlugin_.Net4.x.unitypackage
. (be sure to set the Target API Level to 28) - For
.Net Framework
, the import package name isandroidPlugin_.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 isandroidPlugin_.NetStandard2.1.unitypackage
.(be sure to set the Target API Level to 31)
Import the plugin
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
Import the appropriate package based on your project's Api Compatibility Level.
Select all options and click import.
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"
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
Description:
Get the singleton instance of LovenseAndroidSDK
Parameters: None
Return:
LovenseAndroidSDK instance
SearchToys
Description:
Search for the toys, when a toy is discovered, the OnSearchToyListener listener will be triggered.
Parameters:
Name Type Default Required Description listener OnSearchToyListener yes callback listener Return: None
ConnectToys
Description:
Connect the toys, when a toy's Connection status is changed, the OnConnectListener listener will be triggered.
Parameters:
Name Type Default Required Description toyIds List<string> yes The toys you want to connect listener OnConnectListener yes callback listener Return: None
DisConnectToys
Description:
DisConnect the toys.
Parameters:
Name Type Default Required Description toyIds List<string> yes The toys you want to disconnect Return: None
SendFunctions
Description:
Send function commands to the toys.
Parameters:
Name Type Default Required Description toyIds List<string> yes The toys you want to control commands List<LovenseCommand> yes See LovenseCommand timeSec float yes The total runtime in second, "0" = indefinite time length. loopRunningSec float 0
no time running for each command loop in second loopPauseSec float 0
no time 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 invalues
with the interval ofintervalsMs
.Parameters:
Name Type Default Required Description toyIds List<string> yes The toys you want to control types List<LovenseCommandType> yes See LovenseCommandType strengthValues List<int> yes The order of the levels timeSec float yes The total runtime in second, "0" = indefinite time length. intervalsMs int 150 no The level interval in milliseconds, should be greater than 100, default 150 Return: None
SendPreset
Description:
Send a preset command to the toys.
Parameters:
Name Type Default Required Description toyIds List<string> yes The toys you want to control, mode int yes The preset name to send, we provide four preset patterns: 0: earthquake
,1:fireworks
,2:pulse
,3:wave
timeSec float yes The total runtime in second, "0" = indefinite time length. Return: None
StopAll
Description:
Send a stop command to the toys.
Parameters:
Name Type Description toyIds List<string> The toys you want to stop Return: None
AddSendMultipleCommandListener
Description:
Add callback listening with multiple command executions
Parameters:
Name Type Description callbackFunc UnityAction<string,string> the parameters are toyId and message - Return: None
RemoveSendMultipleCommandListener
Description:
Remove callback listening with multiple command executions
Parameters:
Name Type Description callbackFunc UnityAction<string,string> the parameters are toyId and message Return: None
GetSupportCommandsByType(string type)
Description:
Obtain Command types supported by toys through their types
Parameters:
Name Type Description type string the 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:
Name Type Description id string the id of toy commands List<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:
Name Type Description toyId string the toy's id version int the toy's version type string the toy's type status int the toy's connection status: 1 connected -1 dis connected other:unknown battery int the toy's battery rssi int the toy's rssi deviceName string the toy's name uuid string the toy's uuid deviceType string the toy's deviceType macAddress string the toy's macaddress
LovenseCommand
Description:
The toy control command
Properties:
Name Type Description type LovenseCommandType See below value int Function level
Function Level:
Type | Range |
---|---|
VIBRATE | 0 - 20 |
VIBRATE1 | 0 - 20 |
VIBRATE2 | 0 - 20 |
VIBRATE3 | 0 - 20 |
ROTATE | 0 - 20 |
PUMP | 0 - 3 |
FINGERING | 0 - 20 |
THRUSTRING | 0 - 20 |
DEPTH | 0 - 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:
Name Type Description onErrorEvent UnityAction<string,string> error callback function onSearchToyEvent UnityAction<LovenseAndroidToy> get callback function onFinishSearch UnityAction completed callback function
Method
initCallback
Description:
init callback.
Parameters:
Name Type Default Required Description searchErrorFunc UnityAction<string, string> yes callback, params is code and message toyGetFunc UnityAction<LovenseAndroidToy> yes callback searchCompletedFunc UnityAction yes callback Return: None
OnConnectListener
Description:
The callback listener for connect toys.
Properties:
Name Type Description onConnectFunc UnityAction<string,string> callback function onErrorFunc UnityAction<string,string> callback function
Method
initCallback
Description:
init callback.
Parameters:
Name Type Default Required Description funConnect UnityAction<string, string> yes callback function, params is toyId and status funError UnityAction<string, string> yes callback function, params is code and message Return: None
OnCallBackBatteryListener
Description:
The callback listener for obtains toy's battery.
Properties:
Name Type Description onCallBackBatteryListener UnityAction<string,int> callback function
Method
initCallback
Description:
init callback.
Parameters:
Name Type Default Required Description func UnityAction<string, int> yes callback function, params is toyId and battery Return: None
OnCallBackDeviceTypeListener
Description:
The callback listener for obtains toy's battery.
Properties:
Name Type Description onCallBackDeviceTypeListenerFunc UnityAction<string,LovenseAndroidToy> callback function
Method
initCallback
Description:
init callback.
Parameters:
Name Type Default Required Description func UnityAction<string,LovenseAndroidToy> yes callback 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