Get the charge level of connected smartwatch

Is it possible to get the charge level of connected smartwatch as part of API Wear? (It is preferable without having to deploy the actual wear component on the smart card and then transfer back and forth between the watch and the device). I saw some wearing apps that show the charge level of the watch on the watch itself, but I just wanted to know the current battery level using the phone.

+6
source share
2 answers

Most likely you will need a wearing app, but it will be very easy.

On the wearable, make a WearableListenerService . Ask the phone application to send a message (using Message APi ). This will launch the WearableListenerService on the watch. Ask the watch to get information about the battery and send it back to the phone using another message.

+1
source

Start by determining the current state of charge. BatteryManager transmits all battery and charging data in a sticky manner, which includes charging status.

 IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); Intent batteryStatus = context.registerReceiver(null, ifilter); 

You can extract the current state of charge in this way

 // Are we charging / charged? int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1); boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL; 

you can use this

 public static final String EXTRA_LEVEL 
 Added in API level 5 Extra for ACTION_BATTERY_CHANGED: integer field containing the current battery level, from 0 to EXTRA_SCALE. Constant Value: "level" 

You can find the current battery charge by extracting the current battery level and scale from the battery status, as shown below:

 int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1); float batteryPct = level / (float)scale; // your % 

so batteryPct is your battery% Percent

// you can show your percentage and then

More about BatteryManager from here

-2
source

Source: https://habr.com/ru/post/975397/


All Articles