{"id":14960463,"url":"https://github.com/magicleap/wifi-direct-plugin-sample","last_synced_at":"2025-10-24T18:31:49.126Z","repository":{"id":207005036,"uuid":"716769853","full_name":"magicleap/wifi-direct-plugin-sample","owner":"magicleap","description":"Sample Android Plugin for Unity to use Wi-Fi Direct Service Discovery. This project is an Android app harness written in Java and the plugin is an Android Activity contained in a Java Module.","archived":false,"fork":false,"pushed_at":"2024-06-14T23:15:23.000Z","size":505,"stargazers_count":6,"open_issues_count":0,"forks_count":1,"subscribers_count":8,"default_branch":"main","last_synced_at":"2025-04-07T02:51:15.703Z","etag":null,"topics":["android","augmentedreality","colocation","javasockets","magicleap","magicleap2","ml2","multi-user","p2p","peer-to-peer","peer2","peer2peer","sample-project","shared-experience","spatial-computing","unity","unity3d-plugin","wifi-direct","wifi-direct-android","wifi-direct-service-discovery"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/magicleap.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-11-09T20:49:19.000Z","updated_at":"2024-12-25T05:57:52.000Z","dependencies_parsed_at":"2024-06-15T00:39:16.531Z","dependency_job_id":null,"html_url":"https://github.com/magicleap/wifi-direct-plugin-sample","commit_stats":null,"previous_names":["magicleap/wifi-direct-plugin-sample"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/magicleap/wifi-direct-plugin-sample","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magicleap%2Fwifi-direct-plugin-sample","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magicleap%2Fwifi-direct-plugin-sample/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magicleap%2Fwifi-direct-plugin-sample/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magicleap%2Fwifi-direct-plugin-sample/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/magicleap","download_url":"https://codeload.github.com/magicleap/wifi-direct-plugin-sample/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magicleap%2Fwifi-direct-plugin-sample/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":280844629,"owners_count":26401020,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-24T02:00:06.418Z","response_time":73,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["android","augmentedreality","colocation","javasockets","magicleap","magicleap2","ml2","multi-user","p2p","peer-to-peer","peer2","peer2peer","sample-project","shared-experience","spatial-computing","unity","unity3d-plugin","wifi-direct","wifi-direct-android","wifi-direct-service-discovery"],"created_at":"2024-09-24T13:22:20.581Z","updated_at":"2025-10-24T18:31:46.902Z","avatar_url":"https://github.com/magicleap.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# WiFi Direct Service Discovery Sample Unity Plug-in\n\n\u003ctable cellspacing=\"0\" cellpadding=\"0\" border=\"0\"\u003e\n\u003ctr \u003e\n\u003ctd valign=top width=25%\u003e\n\u003cimg src=\"Readme-Imgs/WiFiDirectSampleUnityPluginSplash.png\"\u003e \n\u003c/td\u003e\n\u003ctd valign=top\u003e\t\nSample Android Plug-in for Unity to use WiFi Direct Service Discovery.\n\u003cbr\u003e\u003cbr\u003e\nThis project is an Android app harness and the plug-in code written in Java is an Android Activity contained in a Java Module.\n\n\u003cH3\u003ePlug-in provides functionality to:\u003c/H3\u003e\n\u003cul\u003e\n\u003cli\u003eConnect Android devices using WiFi Direct P2P services\n\u003cli\u003eCommunicate between connected devices over sockets with string-based messaging\n\u003c/ul\u003e\n\u003c/td\u003e\n\u003c/tr\u003e\n\u003c/table\u003e\n\n## Getting started\n\nThis Android Unity plug-in includes all of the Android WiFi Direct sample Java code used to create the plug-in that you are welcome to use as a starting point to create even more robust WiFi Direct plug-ins or projects of your own.\n\nHowever, it is not necessary to modify or even build this code if your only interest is to use this sample plug-in as is in your own unity project. This repository contains a build of the [WiFiDirectSDActivity-debug.AAR](/WiFiDirectSDActivity/build/outputs/aar/WiFiDirectSDActivity-debug.aar) file located in the project folder\n\n```\n\\WiFiDirectSDActivity\\build\\outputs\\aar\n```\n\nthat you can include in your Unity project and begin using right away.\u003cbr\u003e\n(See below for details on how to [include in your Unity project](#Using-the-Plug-in-in-a-Unity-project), and for this Unity plug-in's [APIs](#Plug-in-APIs))\n\n\u003e📝NOTE: This plug-in extends the `UnityPlayerActivity` class and contains Unity Editor version specific copies of `UnityPlayerActivity.java` and `classes.jar` files from Unity version: `2022.3.6f1`. To use this plug-in in other versions of Unity you may need to clone this project, replace those two files, and rebuild the AAR file to include in your Unity project. (See below for pointers on [rebuilding plug-in for other versions of Unity](#Rebuilding-Plug-in-for-other-versions-of-Unity))   \n\nFor those that are interested in working with the plug-in code, after cloning this respository to your local machine, open the project folder in Android Studio or the Android code editor of your choosing.\n\nThe `WiFiDirectSDActivity` module contains the source code for a basic Unity Plug-in with peer-to-peer support leveraging Android's flavor of WiFi Direct Service Discovery and a simple implementation of Java Sockets. \n\nWithin Android Studio, build the ARR file by first selecting the module in the `Project` pane, and then selecting the `Make Module 'WiFiDirectPluginHarness.WiFiDirectSDActivity'` from the `Build` menu. The ARR file will get generated in the output folder mentioned above.\n\n\u003e📝NOTE: Once the ARR file has been added into a Unity project, it is advised to close the Unity project, copy over any new build of the ARR file after each modify/rebuild of the plug-in in Android Studio, and then reopen the Unity project in order to see and test any changes. \n\n## The code modules in this plug-in\n\nThe code in this plug-in has been organized into five code modules:\n* WDPluginActivity.java\n\n    * Contains the majority of the plug-in logic and overrides the needed `Activity` events\n\n* WiFiDirectBroadcastReceiver.java\n    \n    * Overrides the needed `BroadcastReceiver` events to handle the relevant WiFi P2P Intents\n\n* ServerClass.java\n\n    * Socket Server class that also manages a multi-threaded array of `ClientHandler` objects when the current device is a WiFi Direct Host managing one or more connected WiFi Direct peer devices\n\n* ClientHandler.java\n\n    * `ClientHandler` that handles socket communication to a peer from the socket server \n\n* ClientClass.java\n\n    * Client class that handles socket communication to the socket server when the current device is a WiFi Direct peer connected to a WiFi Direct Host\n\n\n## Android's WiFi Direct Service Discovery Overview\n\n\n \u003eUnfortunately, clear documentation on how the Android WiFi Direct Service Discovery APIs work is hard to find.\n\u003e\n\u003eThe following interpretation is based on the research and experimention conducted while creating this sample, however, it should not be considered an authoritative source for how WiFi Direct Service Discovery is implemented in the Android or AOSP operating system.\n\n---\n### Basics Activity flow\nWiFi Direct Service Discovery involves one device registering and then broadcasting a service that other devices within range can then discover and connect to.\n\n### The plug-in is basically an Android `Activity` that uses the following activitiy lifecycle events in the following way:\n\n#### `onCreate`\n* add WiFi Direct P2P `intent-filters` to listen for WiFi Direct related broadcasts\n* initialize P2P services\n  * check device \u0026 hardware capabilities\n  * create P2P manager and primary objects\n  * warm up services\n  * standardize name of device\n* request needed permissions \n\n#### `onResume`\n* register broadcast receiver\n\n#### `onPause`\n* unregister broadcast receiver\n\n#### `onDestroy`\n* close any open connections with other devices\n  * update connection status\n\n### The Unity application consuming the plug-in can also initiate the following WiFi Direct actions:\n\n#### `StartHosting`\n* instantiate and start service\n\n#### `StartDiscovering`\n* initiate service discovery\n* listen for service broadcasts\n\n#### `ConnectToService`\n* device doing the discovery can initiate a connection to a discovered service host\n\n#### `StopHosting`\n* clear local services\n\n#### `StopDiscovering`\n* clear service discovery requests\n\n### Other code logic that plays an important role\n\n#### `BroadcastReceiver`\n* handles incomming notifications of\n  * device changed\n  * connection changed\n  * peers changed\n  * state changed\n\n#### `ConnectionInfoLister`\n* listens for connection update details\n\n\n## Sockets based messages\n\nThis plug-in includes a simple version of string-based messaging between peers using Java Sockets.\n\nThe WiFi Direct Service Discovery host device in the shared experience runs a socket server and manages a multi-threaded array of client handlers to deal with each peer. \n\nPeers, on the other hand, act as socket clients that connect and communicate to the host's socket server.\n\n\u003e📝NOTE: By default, the thread pool size has been set to handle 10 simultaneous client handlers which, as is, should accomodate up to 11 devices in a shared experience. However, WiFi Direct Service Discovery is capable of connecting many more P2P devices in a session if you are interested in creating a more robust threading solution to manage socket client handlers.\n\n\n\n\n\n\n\n\n\n\n***\n\n## Using the Plug-in in a Unity project\nTo use this plug-in:\n* Copy the .ARR file into your Unity project under\n    * Assets|Plugins|Android\n* Modify the package and activity sections of the Android `Manifest` file located in the same folder to include:\n\n```diff\n\n\u003cmanifest xmlns:android=\"http://schemas.android.com/apk/res/android\" package= \n+\"com.magicleap.samples.wifidirectsdactivity\"\n mlns:tools=\"http://schemas.android.com/tools\"\u0026gt\n  \u003capplication\u003e\n\n\t\u003cactivity android:name=\n+\"com.magicleap.samples.wifidirectsdactivity.WDPluginActivity\"\n\t android:theme=\"@style/UnityThemeSelector\"\u003e\n\t      \u003cintent-filter\u003e\n\t        \u003caction android:name=\"android.intent.action.MAIN\" /\u003e\n\t        \u003ccategory android:name=\"android.intent.category.LAUNCHER\" /\u003e\n\t      \u003c/intent-filter\u003e\n\t      \u003cmeta-data android:name=\"unityplayer.UnityActivity\" android:value=\"true\" /\u003e\n\t    \u003c/activity\u003e\n…\n```\n\n * Add a `gamecomponent` to your scene called \"`WifiDirectPluginManager`\" and attach a script that initializes the plug-in and then sets the Wi-Fi Direct Service Name for your application.\n\n```\n\t    AndroidJavaClass unityClass;    \n\t    AndroidJavaObject unityActivity;\n\t\t void Start()\n\t\t    {\n\t\t\n\t\t        unityClass = new AndroidJavaClass(\"com.unity3d.player.UnityPlayer\");\n\t\t        unityActivity = unityClass.GetStatic\u003cAndroidJavaObject\u003e(\"currentActivity\");\n                unityActivity.Call(\"SetServiceName\", Application.productName);\n\t\t\n\t\t    }\n```\n* Use the \"`unityActivity`\" instance to call plug-in functions\n\nfor example:\n```\npublic void StartHosting()\n    {\n        if (unityActivity != null)\n        {\n            unityActivity.Call(\"StartHosting\");\n        }\n    }\n```\n* Create public methods to handle plug-in callback\n\nfor example:\n```\npublic void DisplayHostServiceFriendlyName(string serviceFriendlyName)\n    {\n     // code to respond to plug-in call to display host service friendly name   \n    }\n```\nFor a full working sample of a Unity project that implements this plug-in, please refer to our WiFi-Direct-Shared-Experience-Sample code sample project for Unity.\n\n# Plug-in APIs\n---\n## Exposed by plug-in\n\n\n\n### Set Service Name\n```c#\n\tunityActivity.Call(\"SetServiceName\", newServiceName);\n```\n#### Passes: string containing the name of the Wi-Fi Direct Service to broadcast or to filter discovery for \n\u003e📝NOTE: By setting the service name to the application name (or any other valid unique name) your application will then have its own service to broadcast, discover, and connect to between peer devices. This simplifies the connection options for the user and makes it much more likely to connect to a compatible connection. This service name should be set at plug-in initialization and typically not changed.\n---\n\n### Get Service Name\n```c#\n\tstring serviceName = unityActivity.Call\u003cstring\u003e(\"GetServiceName\");\n```\n#### Returns: string containing the service name being used to broadcast, or to filter discovery of Wi-Fi Direct services\n---\n\n### Get Device Name\n```c#\n\tstring deviceName = unityActivity.Call\u003cstring\u003e(\"GetDeviceName\");\n```\n#### Returns: string containing the name of the current device\n---\n\t\t\n### Start Hosting Service\n```c#\n\tunityActivity.Call(\"StartHosting\");\n```\t\n---\n\n### Stop Hosting Service\n```c#\n\tunityActivity.Call(\"StopHosting\");\n```\t\n---\t\n### Start Discovery available services\n```c#\n\tunityActivity.Call(\"StartDiscovering\");\n```\n---\n### Stop Discovery available services\n```c#\n\tunityActivity.Call(\"StopDiscovering\");\n```\n---\n### Connect to service\n```c#\n\tunityActivity.Call(\"ConnectToService\", serviceList[index]);\n```\t\n#### Passes: string containing the name of host device broadcasting discovered service\n---\t\n### Send command to Peers\n```c#\n\tunityActivity.Call(\"SendCommandToPeers\", command);\t\n```\n#### Passes: string containing the user interaction command to share with connected peer devices\n---\t\n### Send test message to peers\n```c#\n\tunityActivity.Call(\"SendMsgToPeer\");\n```\t\n---\n\n## Plug-in callbacks to Unity - expects method implementations to handle:\n\n### Related to hosting service:\n\n### Display of the Host Service name/status that you are currently hosting:\n```java\n\t UnityPlayer.UnitySendMessage(\"WifiDirectPluginManager\",\"DisplayHostServiceFriendlyName\", friendlyServiceName);\n```\n#### Passes: string containing the friendly name of the hosted WiFi Direct Service\n\nUpdated as a result of: \n* StartHosting\n* StopHosting \n* Loss of connection\n#### Value of empty string (`\"\"`) indicates lost or no connection\n--- \n\n\n### Displaying a list of peers connected to the active hosted service\n```java\n\tUnityPlayer.UnitySendMessage(\"WifiDirectPluginManager\",\"DisplayConnectedDevices\", devices);\n```\n#### Passes: comma-delimited string list of device names\n\nUpdated from the following plug-in routines:\n\n* ConnectToService\n* Peer established connection\n* Loss of connection\n--- \n\n### Related to discovery:\n\n#### Displaying a list of available services that have been discovered\n```java\t\n    UnityPlayer.UnitySendMessage(\"WifiDirectPluginManager\",\"DisplayAvailableServices\", services);\n```\n#### Passes: comma-delimited string list of recently discovered services\n\nUpdated as a result of:\n\n* StopDiscovering\n* A service is discovered.\n\n--- \n\n#### Display of the Discovery status\n```java\t\n    UnityPlayer.UnitySendMessage(\"WifiDirectPluginManager\",\"DisplayDiscoveryStatus\",status);\n```\n#### Passes: string containing the service discovery status\n\nUpdated as a result of:\n* StartDiscoverying\n* StopDiscoverying\n* ConnectToService\n#### Value of empty string (`\"\"`) indicates Discovery off\n--- \n\n#### Display of the currently connected Discovered service\n```java\t\n    UnityPlayer.UnitySendMessage(\"WifiDirectPluginManager\",\"DisplayConnectedServiceFriendlyName\",friendlyServiceName);\n```\n#### Passes: string containing the friendly name of the WiFi Direct Service this device is connected to\n\nUpdated as a result of:\n\n* ConnectToService\n* Loss of connection\n#### Value of empty string (`\"\"`) indicates lost or no connection\n--- \n\n### Related to messaging:\n\n#### Display of incoming user or test messages to the user\n```java\t\n    UnityPlayer.UnitySendMessage(\"WifiDirectPluginManager\",\"DisplayIncomingMsg\",msg);\n``` \n#### Passes: string containing the user message from other peer device\n\nUpdated as a result of:\n\n* Incomming message received\n--- \n#### Display of incoming system information messages to the user\n```java\t\n    UnityPlayer.UnitySendMessage(\"WifiDirectPluginManager\",\"DisplaySystemMsg\",msg);\n```\n#### Passes: string containing the system message\n\nUpdated as a result of:\n\n* System message received\n--- \n#### Handle the processing of incoming shared user interaction commands\n```java\t\n    UnityPlayer.UnitySendMessage(\"WifiDirectPluginManager\",\"HandleIncommingCmd\",cmd);\n```\n#### Passes: string containing command and its arguments\n\nUpdated as a result of:\n\n*\tIncoming shared user interaction command received\n\u003e📝NOTE: specific shared user interaction command names, arguements, and formating to be defined by the Unity application that consumes the plug-in.\n\u003e\n\u003eRefer to the `Magic Leap Dev Sample`: WiFi-Direct-Share-Experience-Sample App for an example of one comma-delieted approach to sharing a number of common user interactions between devices.\n--- \n\n## Rebuilding Plug-in for other versions of Unity\n\nThis plug-in extends the `UnityPlayerActivity` class and contains Unity Editor version specific copies of `UnityPlayerActivity.java` and `classes.jar` files from Unity version: `2022.3.6f1`. \nTo use this plug-in from within projects based on other versions of Unity you may need to clone this project, replace those two files, and rebuild the AAR file to include in your Unity project.\n\nThe two files that need to be replaced in the WiFiDirectSDActivity library of the project are:\n```\nWiFiDirectSDActivity/src/main/java/com/magicleap/samples/wifidirectsdactivity/UnityPlayerActivity.java\n```\n```\nWiFiDirectSDActivity/libs/classes.jar\n```\nThey should be replaced with copies from your local installation of the Unity Editor that you are targeting. \nOn Windows installations the files are typically found under:\n\n```\nUnityPlayerActivity.java in\n C:\\Program Files\\Unity\\Hub\\Editor\\[specific version number]\\Editor\\Data\\PlaybackEngines\\AndroidPlayer\\Source\\com\\unity3d\\player  \n```\n\n```\nclasses.jar in\nC:\\Program Files\\Unity\\Hub\\Editor\\[specific version number]\\Editor\\Data\\PlaybackEngines\\AndroidPlayer\\Variations\\il2cpp\\Release\\Classes  \n```\n\nAfter replacing those two files, edit the first line of `UnityPlayerActivity.java` file to change the package name from \n```diff\n-package com.unity3d.player;\n```\nand replace it with\n```diff\n+package com.magicleap.samples.wifidirectsdactivity;\n```\n\nBuild the ARR file by first selecting the module in the `Project` pane, and then selecting the `Make Module 'WiFiDirectPluginHarness.WiFiDirectSDActivity'` from the `Build` menu. The ARR file will get generated in the output folder `\\WiFiDirectSDActivity\\build\\outputs\\aar`.\n\nUse this newly created version specific version of the AAR file in your Unity project of the same version by placing it in the `Assets|Plugins|Android` as described in the above section [Using the Plug-in in a Unity project](#Using-the-Plug-in-in-a-Unity-project).\n\n## Forums\nIf you have questions that are not covered here, please check the Magic Leap  2 Developer Forum: https://forum.magicleap.cloud/\n\n\n# Copyright\nCopyright (c) (2023) Magic Leap, Inc. All Rights Reserved. Use of this file is governed by the Developer Agreement, located here: https://www.magicleap.com/software-license-agreement-ml2 \n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmagicleap%2Fwifi-direct-plugin-sample","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmagicleap%2Fwifi-direct-plugin-sample","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmagicleap%2Fwifi-direct-plugin-sample/lists"}