Unable to toggle on and off location service using Appium

I need to enable and disable location service in Android before my test. I have tried the following solutions but nothing worked at all.

Scenario : 1 ADB commands

adb shell settings put secure location_providers_allowed +network,gps adb shell settings put secure location_providers_allowed -network,gps

These commands working at times but not all the times so my test fails lot of times for disabling.

Scenario : 2 toggleLocationService() Method

public static void enableAndDisableLocation(AppiumDriver driver) {
((AndroidDriver) driver).openNotifications();
((AndroidDriver) driver).toggleLocationServices();
Are there any other solution to enable and disable Location service in Android using Appium. Also I want to know the status of the Location before enabling and disabling.

I am using Java client 5.0.4 Testing in Android Nougat.

Those commands work fine on my Android 7. Have you run them directly in terminal and see what happens on device?

Yeah it is working sometimes and not all the times when running the command manually in terminal

In some phone: which has the Arrow next to the Location Header, the command to ToggleLocationServices() is clicking on the arrow due to which it fails to perform its basic functionality, to Turn On or OFF the location Services.