{"id":13801296,"url":"https://github.com/Elvincth/capacitor-plugin-lightsensor","last_synced_at":"2025-05-13T11:31:01.795Z","repository":{"id":47685474,"uuid":"324485035","full_name":"Elvincth/capacitor-plugin-lightsensor","owner":"Elvincth","description":"Lightsensor plugin for capacitor","archived":false,"fork":false,"pushed_at":"2024-09-27T06:20:24.000Z","size":211,"stargazers_count":3,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-11-18T16:57:23.584Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Elvincth.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2020-12-26T05:02:23.000Z","updated_at":"2024-03-21T10:10:26.000Z","dependencies_parsed_at":"2024-01-05T21:15:15.861Z","dependency_job_id":null,"html_url":"https://github.com/Elvincth/capacitor-plugin-lightsensor","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":"capacitor-community/.github","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Elvincth%2Fcapacitor-plugin-lightsensor","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Elvincth%2Fcapacitor-plugin-lightsensor/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Elvincth%2Fcapacitor-plugin-lightsensor/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Elvincth%2Fcapacitor-plugin-lightsensor/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Elvincth","download_url":"https://codeload.github.com/Elvincth/capacitor-plugin-lightsensor/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253932797,"owners_count":21986448,"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","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":[],"created_at":"2024-08-04T00:01:21.323Z","updated_at":"2025-05-13T11:31:00.080Z","avatar_url":"https://github.com/Elvincth.png","language":"Java","funding_links":[],"categories":["Other plugins"],"sub_categories":["Specialized Hardware"],"readme":"\u003cp align=\"center\"\u003e\u003cbr\u003e\u003cimg src=\"https://i.imgur.com/VbLeXoQ.png\" width=\"128\" height=\"128\" /\u003e\u003c/p\u003e\n\u003ch3 align=\"center\"\u003eCapacitor Lightsensor Plugin\u003c/h3\u003e\n\u003cp align=\"center\"\u003e\u003cstrong\u003e\u003ccode\u003ecapacitor-plugin-lightsensor\u003c/code\u003e\u003c/strong\u003e\u003c/p\u003e\n\u003cp align=\"center\"\u003e\nThis plugin get the illuminance level on the device\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"https://img.shields.io/maintenance/yes/2021?style=flat-square\" /\u003e\n  \u003ca href=\"https://github.com/capacitor-community/example/actions?query=workflow%3A%22CI%22\"\u003e\n  \u003ca href=\"https://www.npmjs.com/package/capacitor-plugin-lightsensor\"\u003e\u003cimg src=\"https://img.shields.io/npm/l/capacitor-plugin-lightsensor?style=flat-square\" /\u003e\u003c/a\u003e\n\u003cbr\u003e\n  \u003ca href=\"https://www.npmjs.com/package/capacitor-plugin-lightsensor\"\u003e\u003cimg src=\"https://img.shields.io/npm/dw/capacitor-plugin-lightsensor?style=flat-square\" /\u003e\u003c/a\u003e\n  \u003ca href=\"https://www.npmjs.com/package/capacitor-plugin-lightsensor\"\u003e\u003cimg src=\"https://img.shields.io/npm/v/capacitor-plugin-lightsensor?style=flat-square\" /\u003e\u003c/a\u003e\n\u003c!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section --\u003e\n\u003ca href=\"#contributors-\"\u003e\u003cimg src=\"https://img.shields.io/badge/all%20contributors-1-orange?style=flat-square\" /\u003e\u003c/a\u003e\n\u003c!-- ALL-CONTRIBUTORS-BADGE:END --\u003e\n\u003c/p\u003e\n\n## Maintainers\n\n| Maintainer | GitHub | Social |\n| -----------| -------| -------|\n| Elvin Chu | [Elvincth](https://github.com/elvincth) | [@elvincth](https://twitter.com/elvincth) |\n\n## Installation\n\n\u003cb\u003e Step 1 \u003c/b\u003e\n\n```bash\nnpm install capacitor-plugin-lightsensor --save\nnpx cap sync\n```\n\n\u003cb\u003e Step 2 \u003c/b\u003e\n\nIOS Platform: No further action required.\n\n\u003cb\u003eAndroid Platform: Register the plugin in your main activity (MainActivity.java):\u003c/b\u003e\n\n```java\nimport com.gren.plugin.lightsensor.LightSensor; //ADD this line\n\npublic class MainActivity extends BridgeActivity {\n\n  @Override\n  public void onCreate(Bundle savedInstanceState) {\n    super.onCreate(savedInstanceState);\n    this.init(\n        savedInstanceState,\n        new ArrayList\u003cClass\u003c? extends Plugin\u003e\u003e() {\n\n          {\n             add(LightSensor.class); //ADD this line\n          }\n        }\n      );\n  }\n}\n```\n\n\u003c!-- ## Configuration TODO --\u003e\n\n\n\n## Supported methods\n\n| Name                | Android | iOS | Web |\n| :------------------ | :------ | :-- | :-- |\n| init                | ✅      | ❌  | ✅  |\n| registerListener    | ✅      | ❌  | ✅  |\n| unregisterListener  | ✅      | ❌  | ✅  |\n| getInfo             | ✅      | ❌  | ❌  |\n| isAvailable         | ✅      | ❌  | ✅  |\n\n\n## Usage\n\n### Getting illuminance level\n\n```javascript\nimport { SensorManager } from \"capacitor-plugin-lightsensor\";\nimport { Plugins } from \"@capacitor/core\"; \nconst { LightSensor } = Plugins;\n\nasync function getLux() {\n  try {\n    //Initialize the sensor with some settings\n    await LightSensor.init({\n      SensorDelay: SensorManager.SENSOR_DELAY_UI, //Optional, for android only Default is SENSOR_DELAY_NORMAL\n    });\n\n    //Register onLightSensorChanged listener\n    await LightSensor.registerListener();\n\n    //Listen for the sensor change\n    window.addEventListener(\"onLightSensorChanged\", (evt) =\u003e {\n      console.log(\"value\", evt.value); //The illuminance (lux) level \n      console.log(\"accuracy\", evt.accuracy); //Android only the accuracy of this event, for web return -1\n      console.log(\"timestamp\", evt.timestamp); //For android in nanoseconds, For web in millisecond\n      \n      console.log(\"onLightSensorChanged\", JSON.stringify(evt));  \n      //{\"isTrusted\":false,\"accuracy\":3,\"timestamp\":58769305913765,\"value\":281.9115905761719}\n    });\n\n  } catch (error) {\n    console.log(\"Error occur:\", error);\n  }\n}\n```\n\u003cbr\u003e\n\n### Pause the sensor after registered\n\n```javascript\nimport { SensorManager } from \"capacitor-plugin-lightsensor\";\nimport { Plugins } from \"@capacitor/core\"; \nconst { LightSensor } = Plugins;\n\nasync function pause() {\n  try {\n   \n    //Pause the sesnor\n    await LightSensor.unregisterListener();\n    \n    //To resume, run register again\n    //await LightSensor.registerListener();\n\n  } catch (error) {\n    console.log(\"Error occur:\", error);\n  }\n}\n```\n\n\u003cbr\u003e\n\n\n### Check the availability of light sensor \n```javascript\nimport { SensorManager } from \"capacitor-plugin-lightsensor\";\nimport { Plugins } from \"@capacitor/core\"; \nconst { LightSensor } = Plugins;\n\nasync function checkSensor() {\n  const isSensorAvailable = await LightSensor.isAvailable();\n\n  if (isSensorAvailable.status) {\n    console.log(\"There is light sensor on this device\");\n  } else {\n    console.log(\"No light sensor on this device\");\n  }\n  \n  console.log(\"isSensorAvailable\", JSON.stringify(isSensorAvailable)); // {\"status\":true} or {\"status\":false}\n}\n\n```\n\n\u003cbr\u003e\n\n### Getting the light sensor information (Android ONLY)\n```javascript\nimport { SensorManager } from \"capacitor-plugin-lightsensor\";\nimport { Plugins } from \"@capacitor/core\"; \nconst { LightSensor } = Plugins;\n\nasync function getInfo() {\n  try {\n    const sensorInfo = await LightSensor.getInfo();\n\n    //For android only, if web all number will return -1\n    \n    console.log(\"vendor\", sensorInfo.vendor); //vendor string of this sensor\n    console.log(\"version\", sensorInfo.version); //version of the sensor's module\n    console.log(\"type\", sensorInfo.type); //generic type of this sensor\n    console.log(\"maxRange\", sensorInfo.maxRange); //maximum range of the sensor in the sensor's unit.\n    console.log(\"resolution\", sensorInfo.resolution); //resolution of the sensor in the sensor's unit.\n    console.log(\"power\", sensorInfo.power); //the power in mA used by this sensor while in use\n    console.log(\"minDelay\", sensorInfo.minDelay); //the minimum delay allowed between two events in microsecond or zero if this sensor only returns a value when the data it's measuring changes.\n    console.log(\"minDelay\", sensorInfo.maxDelay); //The max delay for this sensor in microseconds.\n\n    console.log(\"sensorInfo\", JSON.stringify(sensorInfo));\n    //{\"vendor\":\"OnePlus\",\"version\":1,\"type\":5,\"maxRange\":4096,\"resolution\":1,\"power\":1.7049999237060547,\"minDelay\":0,\"maxDelay\":0}\n  } catch (error) {\n    console.log(\"Light sensor not available\");\n  }\n}\n```\n\u003cbr\u003e\n\n## API\n\n### Methods\n\n#### init(...)\n\nInitialize the light sensor with settings (See example [here](#getting-illuminance-level) )\n\n| Param   | Type    | Description            |\n|---------|---------|------------------------|\n| **`option`**   | `object`  | See option table |\n\nOption\n\n| Key         | Type                             | Description                                                            |\n|-------------|-------------------------------------|------------------------------------------------------------------------|\n| SensorDelay | [ Enum (SensorManager)](#sensormanager) | Optional, for android only Default is SensorManager.SENSOR_DELAY_NORMAL |\n\n\n**Returns:** \u003ccode\u003ePromise\u003c/code\u003e\n\n\u003cbr\u003e\n\n#### registerListener()\n\nRegister [onLightSensorChanged listener](#onLightSensorChanged) (To start or resume the sensor, see example [here](#getting-illuminance-level) )\n\n**Returns:** \u003ccode\u003ePromise\u003c/code\u003e\n\n\u003cbr\u003e\n\n#### unregisterListener()\n\nUnregister [onLightSensorChanged listener](#onLightSensorChanged) (To stop or pause the sensor, see example [here](#pause-the-sensor-after-registered) ) \n\n**Returns:** \u003ccode\u003ePromise\u003c/code\u003e\n\n\u003cbr\u003e\n\n\n#### isAvailable()\n\nCheck if the device have a light sensor or not (See example [here](#check-the-availability-of-light-sensor) )\n\n**Returns:** \u003ccode\u003ePromise\u003c/code\u003e of following object structure:\n\n| Key    | Type    | Description        |\n|--------|---------|--------------------|\n| status | Boolean | Have light sensor? |\n\n\n\u003cbr\u003e\n\n#### getInfo()\n\nGet light sensor information (See example [here](#getting-the-light-sensor-information-android-only) )\n\u003cp\u003eMore information in https://developer.android.com/reference/android/hardware/Sensor#getVendor()\u003c/p\u003e\n\u003cp\u003eNOTE: Web didn't support this method it will return -1 for all numbers and Unknown for string\u003c/p\u003e\n\n**Returns:** \u003ccode\u003ePromise\u003c/code\u003e of following object structure:\n\n\n| Key        | Type    | Description                                                                                                                                  |\n|------------|---------|----------------------------------------------------------------------------------------------------------------------------------------------|\n| vendor     | object  | Vendor string of this sensor                                                                                                                 |\n| version    | Number  | Version of the sensor's module                                                                                                               |\n| type       | Number  | Generic type of this sensor                                                                                                                  |\n| maxRange   | Number  | Maximum range of the sensor in the sensor's unit                                                                                             |\n| resolution | Number  | Resolution of the sensor in the sensor's unit                                                                                                |\n| power      | Number  | The power in mA used by this sensor while in use                                                                                             |\n| minDelay   | Number  | The minimum delay allowed between two events in microsecond or zero if this sensor only returns a value when the data it's measuring changes |\n| maxDelay   | Number  | The max delay for this sensor in microseconds                                                                                                |\n\n\u003cbr\u003e\n\n### Events\n\n#### onLightSensorChanged \n\u003cp\u003eMore information in https://developer.android.com/reference/android/hardware/SensorEvent#timestamp\u003c/p\u003e\n\nSee example [here](#getting-illuminance-level)\n\n**Returns:** \u003ccode\u003eobject\u003c/code\u003e of following object structure:\n\n| Key       | Type    | Description                                                                                                        |\n|-----------|---------|--------------------------------------------------------------------------------------------------------------------|\n| isTrusted | Boolean | Returns true if event was dispatched by the user agent, and false otherwise. (Capacitor thing you can ignore this) |\n| accuracy  | Boolean | Android only the accuracy of this event, for web return -1                                                         |\n| timestamp | Number  | For android in nanoseconds, For web in millisecond                                                                 |\n| value     | Number  | The illuminance (lux) level                                                                                        |\n\n\u003cbr\u003e\n\n### Enums\n\n#### SensorManager \n\u003cp\u003e (For android only, see more in: https://developer.android.com/reference/android/hardware/SensorManager#SENSOR_DELAY_FASTEST ) \u003c/p\u003e\n\u003c!-- https://www.tablesgenerator.com/markdown_tables --\u003e\n\n| Members              | Value | Description                                   |\n|----------------------|-------|-----------------------------------------------|\n| SENSOR_DELAY_FASTEST | 0     | Get sensor data as fast as possible           |\n| SENSOR_DELAY_GAME    | 1     | Rate suitable for games                       |\n| SENSOR_DELAY_UI      | 2     | Rate suitable for the user interface          |\n| SENSOR_DELAY_NORMAL  | 3     | Normal rate (Default)                         |\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FElvincth%2Fcapacitor-plugin-lightsensor","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FElvincth%2Fcapacitor-plugin-lightsensor","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FElvincth%2Fcapacitor-plugin-lightsensor/lists"}