The Grayzone

Detecting Connection in Blackberry

This post is in response to an e-mail I received from Joerg. He was asking if I knew how to automatically switch on/off Bluetooth when a Blackberry is connected to an in-car charger.

I’m not sure if there has been a change in the most recent API but when I tried something similar using v4.5.0 of the Blackberry API it wasn’t possible to detect whether the handset is connected to an in-car charger. The way that I got round this is to detect whether the Blackberry is charging, this can be via normal charger or in-car charger.

To detect whether the handset is connected you can use the DeviceInfo class. I used the Java XOR operator (^), which returns 0 if both bits of the comparison are the same.

Below is some code to check if the battery is charging or using external power (fully charged and plugged in).

boolean charging = (DeviceInfo.getBatteryStatus() ^ DeviceInfo.BSTAT_CHARGING) == 0);
boolean pluggedIn = (DeviceInfo.getBatteryStatus() ^ DeviceInfo.BSTAT_IS_USING_EXTERNAL_POWER) == 0);

Share this: