Blackberry determines if external power is used

Is there a way to determine if the Blackberry is connected to the cable or not? (Power / USB)

I have tried several things so far ...

if(DeviceInfo.BSTAT_IS_USING_EXTERNAL_POWER > 0) { // Plugged in // TODO : Do something }else{ // Not plugged in // TODO: Do something else } 

This seems to be dead code, and it doesn't work at all.

However, I was lucky with the following:

 if((DeviceInfo.getBatteryStatus() ^ DeviceInfo.BSTAT_IS_USING_EXTERNAL_POWER) != 0) { // Plugged in // TODO : Do something }else{ // Plugged in // TODO : Do something else } 

Unfortunately, it is only effective if the battery is at 100%. As soon as it falls below, it has the opposite effect.

The latter was compiled using a related problem on SO, however it does not have the desired results, as suggested there.

0
source share
2 answers

This is what I used in the past:

  private boolean isBatteryCharging(){ int battst = DeviceInfo.getBatteryStatus(); if(((battst & DeviceInfo.BSTAT_IS_USING_EXTERNAL_POWER) != 0) || ((battst & DeviceInfo.BSTAT_CHARGING) != 0) || ((battst & DeviceInfo.BSTAT_AC_CONTACTS) != 0)){ return true; } return false; } 

Hope this helps.

+1
source

Are you sure you should use the xor operator? You might want to use a binary file instead.

Something like this maybe?

 if((DeviceInfo.getBatteryStatus() & DeviceInfo.BSTAT_IS_USING_EXTERNAL_POWER) != 0) { // Plugged in // TODO : Do something }else{ // Not plugged in // TODO : Do something else } 
0
source

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


All Articles