简体   繁体   中英

How to get connection states for a bluetooth low energy device in android

Follwoing is the code android uses to get the connection summary(to display the connection status labels)

      private int getConnectionSummary() {
      ...........................
      ...........................
      for (LocalBluetoothProfile profile : cachedDevice.getProfiles()) {
            int connectionStatus = cachedDevice.getProfileConnectionState(profile);

            switch (connectionStatus) {
                case BluetoothProfile.STATE_CONNECTING:
                case BluetoothProfile.STATE_DISCONNECTING:
                    return Utils.getConnectionStateSummary(connectionStatus);

                case BluetoothProfile.STATE_CONNECTED:
                    profileConnected = true;
                    break;

                case BluetoothProfile.STATE_DISCONNECTED:
                    if (profile.isProfileReady()) {
                        if (profile instanceof A2dpProfile) {
                            a2dpNotConnected = true;
                        } else if (profile instanceof HeadsetProfile) {
                            headsetNotConnected = true;
                        }
                    }
                    break;
            }
        }

As you can see from the code above, they obtain the connection status for a Classic device using the following line of code:

int connectionStatus = cachedDevice.getProfileConnectionState(profile);

The android system calls the getConnectionSummary() method irrespective of whether a Classic or Bluetooth Low energy device is trying to connect; but unlike a classic device, since we do not have a method by which we can get the connectionStatus for a bluetooth Low energy device, we are unable to update the connection status correctly for a Low energy device.

Full source for this class can be found here

Any help is much appreciated.

What is the Bluetooth Low energy development kit you are using ?

As of now Android doesn't have native API's for Bluetooth Low energy devices. you may have to use third party APIs like the BLE From Broadcomm or TI or CSR.

http://code.google.com/p/broadcom-ble/

Motorola bluetooth Low energy APIs http://developer.motorola.com/docs/bluetooth-low-energy-api/

You can look at this page for more information on selecting a Development Kit. BTLE (Bluetooth Low energy) development kit - must have proximity profile

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM