Bug 1154136 - Update Bluetooth connection state when BT stack updates device properties. r=shuang
authorJamin Liu <jaliu@mozilla.com>
Tue, 14 Apr 2015 18:46:23 +0800
changeset 257976 8aaa0ee014a45fdaeeae14ec409d03d9fa2fb943
parent 257975 87aecb3f996e28bb31ffb5e23e81a76579c93d70
child 257977 3bcff646a5f31ba5a47bc5995878fa71da2ee247
push id8007
push userraliiev@mozilla.com
push dateMon, 11 May 2015 19:23:16 +0000
treeherdermozilla-aurora@e2ce1aac996e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersshuang
bugs1154136
milestone40.0a1
Bug 1154136 - Update Bluetooth connection state when BT stack updates device properties. r=shuang
dom/bluetooth/bluedroid/BluetoothServiceBluedroid.cpp
dom/bluetooth/bluedroid/BluetoothServiceBluedroid.h
dom/bluetooth/bluetooth1/BluetoothDevice.cpp
--- a/dom/bluetooth/bluedroid/BluetoothServiceBluedroid.cpp
+++ b/dom/bluetooth/bluedroid/BluetoothServiceBluedroid.cpp
@@ -1971,16 +1971,53 @@ BluetoothServiceBluedroid::ToggleCalls(B
 uint16_t
 BluetoothServiceBluedroid::UuidToServiceClassInt(const BluetoothUuid& mUuid)
 {
   // extract short UUID 0000xxxx-0000-1000-8000-00805f9b34fb
   uint16_t shortUuid;
   memcpy(&shortUuid, mUuid.mUuid + 2, sizeof(uint16_t));
   return ntohs(shortUuid);
 }
+
+bool
+BluetoothServiceBluedroid::IsConnected(const nsAString& aRemoteBdAddr)
+{
+  MOZ_ASSERT(NS_IsMainThread());
+  nsString connectedAddress;
+
+  // Check whether HFP/HSP are connected.
+  BluetoothProfileManagerBase* profile;
+  profile = BluetoothHfpManager::Get();
+  if (profile && profile->IsConnected()) {
+    profile->GetAddress(connectedAddress);
+    if (aRemoteBdAddr.Equals(connectedAddress)) {
+      return true;
+    }
+  }
+
+  // Check whether OPP is connected.
+  profile = BluetoothOppManager::Get();
+  if (profile->IsConnected()) {
+    profile->GetAddress(connectedAddress);
+    if (aRemoteBdAddr.Equals(connectedAddress)) {
+      return true;
+    }
+  }
+
+  // Check whether A2DP is connected.
+  profile = BluetoothA2dpManager::Get();
+  if (profile->IsConnected()) {
+    profile->GetAddress(connectedAddress);
+    if (aRemoteBdAddr.Equals(connectedAddress)) {
+      return true;
+    }
+  }
+
+  return false;
+}
 #endif
 
 //
 // Bluetooth notifications
 //
 
 /* |ProfileDeinitResultHandler| collects the results of all profile
  * result handlers and calls |Proceed| after all results handlers
@@ -2480,16 +2517,20 @@ BluetoothServiceBluedroid::RemoteDeviceP
       BT_APPEND_NAMED_VALUE(props, "UUIDS", uuidsArray);
     } else if (p.mType == PROPERTY_UNKNOWN) {
       /* Bug 1065999: working around unknown properties */
     } else {
       BT_LOGD("Other non-handled device properties. Type: %d", p.mType);
     }
   }
 
+  // BlueDroid wouldn't notify the status of connection, therefore, query the
+  // connection state and append to properties array
+  BT_APPEND_NAMED_VALUE(props, "Connected", IsConnected(aBdAddr));
+
   if (sRequestedDeviceCountArray.IsEmpty()) {
     // This is possible because the callback would be called after turning
     // Bluetooth on.
     return;
   }
 
   // Use address as the index
   sRemoteDevicesPack.AppendElement(
--- a/dom/bluetooth/bluedroid/BluetoothServiceBluedroid.h
+++ b/dom/bluetooth/bluedroid/BluetoothServiceBluedroid.h
@@ -500,15 +500,17 @@ protected:
   static bool EnsureBluetoothHalLoad();
 
   static void ClassToIcon(uint32_t aClass, nsAString& aRetIcon);
 
   static ControlPlayStatus PlayStatusStringToControlPlayStatus(
     const nsAString& aPlayStatus);
 
   uint16_t UuidToServiceClassInt(const BluetoothUuid& mUuid);
+
+  static bool IsConnected(const nsAString& aRemoteBdAddr);
 };
 
 END_BLUETOOTH_NAMESPACE
 
 #endif
 
 #endif
--- a/dom/bluetooth/bluetooth1/BluetoothDevice.cpp
+++ b/dom/bluetooth/bluetooth1/BluetoothDevice.cpp
@@ -45,16 +45,18 @@ NS_IMPL_RELEASE_INHERITED(BluetoothDevic
 BluetoothDevice::BluetoothDevice(nsPIDOMWindow* aWindow,
                                  const nsAString& aAdapterPath,
                                  const BluetoothValue& aValue)
   : DOMEventTargetHelper(aWindow)
   , BluetoothPropertyContainer(BluetoothObjectType::TYPE_DEVICE)
   , mJsUuids(nullptr)
   , mJsServices(nullptr)
   , mAdapterPath(aAdapterPath)
+  , mConnected(false)
+  , mPaired(false)
   , mIsRooted(false)
 {
   MOZ_ASSERT(aWindow);
 
   const InfallibleTArray<BluetoothNamedValue>& values =
     aValue.get_ArrayOfBluetoothNamedValue();
   for (uint32_t i = 0; i < values.Length(); ++i) {
     SetPropertyByValue(values[i]);