[Bluetooth] - fixing BLE device disconnect function
authorAndrzej Popowski <a.popowski@samsung.com>
Fri, 7 Aug 2015 08:35:53 +0000 (10:35 +0200)
committerAndrzej Popowski <a.popowski@samsung.com>
Fri, 7 Aug 2015 09:41:30 +0000 (11:41 +0200)
Change-Id: I8b2a01e5a000619779bd37a34d78fd7ed32dc4d5
Signed-off-by: Andrzej Popowski <a.popowski@samsung.com>
src/bluetooth/bluetooth_api.js
src/bluetooth/bluetooth_le_device.cc

index 6f90376..f813278 100755 (executable)
@@ -652,7 +652,7 @@ BluetoothLEDevice.prototype.disconnect = function() {
             nullable : true
         }
     ]);
-     var callback = function(result) {
+    var callback = function(result) {
         if (native.isFailure(result)) {
             native.callIfPossible(args.errorCallback, native.getErrorObject(result));
         } else {
@@ -660,7 +660,10 @@ BluetoothLEDevice.prototype.disconnect = function() {
         }
     };
 
-    native.call('BluetoothLEDevice_disconnect', {address : this.address}, callback);
+    var result = native.call('BluetoothLEDevice_disconnect', {address : this.address}, callback);
+    if (native.isFailure(result)) {
+        throw native.getErrorObject(result);
+    }
 };
 
 BluetoothLEDevice.prototype.getService = function() {
index bcc22c4..c4601eb 100755 (executable)
@@ -313,14 +313,28 @@ void BluetoothLEDevice::Disconnect(const picojson::value& data,
 
   const auto callback_handle = util::GetAsyncCallbackHandle(data);
   const auto& args = util::GetArguments(data);
-
   const auto& address = common::FromJson<std::string>(args, "address");
 
-  int ret = bt_gatt_disconnect(address.c_str());
+  bool connected = false;
+  int ret = bt_device_is_profile_connected(address.c_str(), BT_PROFILE_GATT, &connected);
+  if (BT_ERROR_NONE != ret) {
+    instance_.AsyncResponse(
+        callback_handle,
+        PlatformResult(ErrorCode::UNKNOWN_ERR, "Failed to disconnect."));
+    return;
+  }
+  if (!connected) {
+    ReportError(PlatformResult(ErrorCode::INVALID_STATE_ERR,
+                               "Bluetooth low energy device is not connected"),
+                &out);
+    return;
+  }
+
+  ret = bt_gatt_disconnect(address.c_str());
   if (BT_ERROR_NONE != ret) {
     instance_.AsyncResponse(
         callback_handle,
-        PlatformResult(util::GetBluetoothError(ret, "Failed to disconnect.")));
+        PlatformResult(ErrorCode::UNKNOWN_ERR, "Failed to disconnect."));
     return;
   }