An open API service indexing awesome lists of open source software.

https://github.com/zalweny26/capacitor-battery

Capacitor plugin to get access to every info about the device battery
https://github.com/zalweny26/capacitor-battery

android battery capacitor capacitor-plugin ios kotlin plugin swift typescript web

Last synced: 2 months ago
JSON representation

Capacitor plugin to get access to every info about the device battery

Awesome Lists containing this project

README

        

# Capacitor Plugin - Battery

Get access to every info about the device battery!

Supported Android version: `21+`\
Supported iOS version: `Not supported`\
Supported Browsers: `Chromium-based`

## Install

```bash
npm install @danyalwe/capacitor-battery
npx cap sync
```

## Todos

- [ ] Add support for iOS

## Supported methods

| Name | Android | iOS | Web |
| :----------------- | :------ | :-- | :-- |
| start | ✅ | ❌ | ✅ |
| stop | ✅ | ❌ | ✅ |
| addListener | ✅ | ❌ | ✅ |
| removeAllListeners | ✅ | ❌ | ✅ |

## Supported properties

| Property | Android | iOS | Web |
| :-------------------- | :------ | :-- | :-- |
| **`currentCapacity`** | ✅ | ❌ | ❌ |
| **`totalCapacity`** | ✅ | ❌ | ❌ |
| **`realPercentage`** | ✅ | ❌ | ❌ |
| **`technology`** | ✅ | ❌ | ❌ |
| **`temperature`** | ✅ | ❌ | ❌ |
| **`voltage`** | ✅ | ❌ | ❌ |
| **`amperage`** | ✅ | ❌ | ❌ |
| **`wattage`** | ✅ | ❌ | ❌ |
| **`health`** | ✅ | ❌ | ❌ |
| **`status`** | ✅ | ❌ | ❌ |
| **`chargeMode`** | ✅ | ❌ | ❌ |
| **`level`** | ✅ | ❌ | ✅ |
| **`hasBattery`** | ✅ | ❌ | ✅ |
| **`isCharging`** | ✅ | ❌ | ✅ |
| **`chargingTime`** | ❌ | ❌ | ✅ |
| **`dischargingTime`** | ❌ | ❌ | ✅ |

## API

* [`start()`](#start)
* [`stop()`](#stop)
* [`addListener('batteryChange', ...)`](#addlistenerbatterychange)
* [`removeAllListeners()`](#removealllisteners)
* [Interfaces](#interfaces)
* [Enums](#enums)

Represents the BatteryPlugin interface.

### start()

```typescript
start() => Promise
```

Starts the battery plugin and returns the battery information.

**Returns:** Promise<BatteryInfos>

--------------------

### stop()

```typescript
stop() => Promise
```

Stops the battery plugin.

--------------------

### addListener('batteryChange', ...)

```typescript
addListener(eventName: 'batteryChange', listenerFunc: (event: BatteryInfos) => void) => Promise
```

Adds a listener for the 'batteryChange' event.

| Param | Type | Description |
| ------------------ | ------------------------------------------------------------------------- | --------------------------------------------------------------- |
| **`eventName`** | 'batteryChange' | The name of the event. |
| **`listenerFunc`** | (event: BatteryInfos) => void | The listener function to be called when the event is triggered. |

**Returns:** Promise<PluginListenerHandle>

--------------------

### removeAllListeners()

```typescript
removeAllListeners() => Promise
```

Removes all listeners for the 'batteryChange' event.

--------------------

### Interfaces

#### BatteryInfos

Represents the battery information of the device.

| Prop | Type | Description |
| --------------------- | --------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------- |
| **`level`** | number | The current battery level as a percentage (0-100). |
| **`hasBattery`** | boolean | Whether the device has a battery or not. |
| **`isCharging`** | boolean | Whether the device is currently charging or not. |
| **`chargingTime`** | number | The estimated time remaining until the battery is fully charged (in minutes). Only available when charging. |
| **`dischargingTime`** | number | The estimated time remaining until the battery is fully discharged (in minutes). Only available when discharging. |
| **`currentCapacity`** | number | The current battery capacity (in mAh). Only available on some devices. |
| **`totalCapacity`** | number | The total battery capacity (in mAh). Only available on some devices. |
| **`realPercentage`** | number | The real battery percentage, which may differ from the reported percentage. Only available on some devices. |
| **`technology`** | string | The technology used in the battery. Only available on some devices. |
| **`temperature`** | number | The current temperature of the battery (in Celsius). Only available on some devices. |
| **`voltage`** | number | The current voltage of the battery (in volts). Only available on some devices. |
| **`amperage`** | number | The current amperage of the battery (in amperes). Only available on some devices. |
| **`wattage`** | number | The current wattage of the battery (in watts). Only available on some devices. |
| **`health`** | BatteryHealth | The health status of the battery. Only available on some devices. |
| **`status`** | BatteryStatus | The status of the battery. Only available on some devices. |
| **`chargeMode`** | BatteryChargeMode | The charging mode of the battery. Only available on some devices. |

#### PluginListenerHandle

| Prop | Type |
| ------------ | ----------------------------------------- |
| **`remove`** | () => Promise<void> |

### Enums

#### BatteryHealth

| Members | Description |
| ------------------ | ----------------------------------------- |
| **`UNKNOWN`** | The health status is unknown. |
| **`COLD`** | The battery is cold. |
| **`DEAD`** | The battery is dead. |
| **`GOOD`** | The battery is in good health. |
| **`OVERHEAT`** | The battery is overheating. |
| **`OVER_VOLTAGE`** | The battery is experiencing over voltage. |
| **`FAILURE`** | The battery has failed. |

#### BatteryStatus

| Members | Description |
| ------------------ | ------------------------------ |
| **`UNKNOWN`** | The battery status is unknown. |
| **`NOT_CHARGING`** | The battery is not charging. |
| **`DISCHARGING`** | The battery is discharging. |
| **`CHARGING`** | The battery is charging. |
| **`FULL`** | The battery is full. |

#### BatteryChargeMode

| Members | Description |
| -------------- | -------------------------------------------------------- |
| **`UNKNOWN`** | The charging mode is unknown. |
| **`AC`** | The battery is being charged through an AC power source. |
| **`USB`** | The battery is being charged through a USB connection. |
| **`DOCK`** | The battery is being charged through a docking station. |
| **`WIRELESS`** | The battery is being charged wirelessly. |