Automate Bluetooth Switching

     We know that Bluetooth is a wireless technology that uses a radio frequency to share data over a short distance and it eliminating the need for wires. We can use Bluetooth on mobile device to share documents or to connect with other Bluetooth-enabled devices. In the era of Smartphone, the use of Bluetooth is less compare to feature phone; but most of the new file sharing application used the logic of Bluetooth to achieve their goals. Some of the mobile applications using the Bluetooth APIs to access the Bluetooth functionality in their mobile application based on the customer requirement.

     In manual testing, we usually enable/disable the Bluetooth from notification area or from the device settings and proceed the Bluetooth dependent features of the application. In case of the automation, we have to enable/disable the Bluetooth at run-time to test the Bluetooth dependent features. In this article, I would like to share a small logic to deal with Bluetooth for both Android and iOS devices.

Enable/Disable Bluetooth on Android

     In Android, the Bluetooth enable/disable actions achieved using KeyEvent, those events are triggered to Android device using ADB (Android Debug Bridge). Following are the important events to enable/disable the Bluetooth on Android device,

  • input keyevent 19 (KEYCODE_DPAD_UP)
  • input keyevent 23 (KEYCODE_DPAD_CENTER)
  • input keyevent 3 (KEYCODE_HOME)

     In the logic flow, first we will open the Bluetooth settings page on Android device, then apply KEYCODE_DPAD_UP event for a soft press at turn on/turn off area, next apply KEYCODE_DPAD_CENTER to do hard press to change the status to on/off. Finally, KEYCODE_HOME to keep the settings page background.

Following are the actual implementation for Android,

public void setBluetooth(AndroidDriver<MobileElement> driver, String statusToSet) {
		Capabilities cap = ((RemoteWebDriver) driver).getCapabilities();
		try {
			Runtime.getRuntime().exec("adb -s " + cap.getCapability(MobileCapabilityType.UDID).toString()
					+ " shell am start -a android.settings.BLUETOOTH_SETTINGS");
			MobileElement bluetoothIcon = driver.findElementById("com.android.settings:id/switch_text");
			String actualStatus = bluetoothIcon.getText();
			System.out.println(actualStatus);
			if (!actualStatus.equalsIgnoreCase(statusToSet)) {
				Runtime.getRuntime().exec("adb -s " + cap.getCapability(MobileCapabilityType.UDID).toString()
						+ " shell input keyevent 19");
				Runtime.getRuntime().exec("adb -s " + cap.getCapability(MobileCapabilityType.UDID).toString()
						+ " shell input keyevent 23");
				Runtime.getRuntime().exec("adb -s " + cap.getCapability(MobileCapabilityType.UDID).toString()
						+ " shell input keyevent 3");
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

     You can call the above function using setBluetooth(driver,”on”) to enable Bluetooth and setBluetooth(driver,”off”) to disable Bluetooth. Most of the Android device has the android.settings.BLUETOOTH_SETTINGS package name. If it is different then just update the package in the above code before using it. The Id of bluetoothIcon may vary based on Android devices. I have used com.android.settings:id/switch_text for One Plus 8 Pro; you can update it based on selected device.

Enable/Disable Bluetooth on iOS

     In case of iOS, we are directly dealing with different actions on layout instead of the sending KeyEvents. In this case open settings and switching.

By bluetoothTab = MobileBy.xpath("//XCUIElementTypeCell[@visible='true' and @name='Bluetooth']");
By bluetoothBtn = MobileBy.xpath("//XCUIElementTypeSwitch[@visible='true' and @name='Bluetooth']");
public void setBluetooth(boolean expectedStatus) {
    driver.findElement(bluetoothTab).click(); // To open Settings > Bluetooth
    if (isBluetoothEnabled() != expectedStatus) {
        driver.findElement(bluetoothBtn).click(); // To switch Bluetooth
    }
}
public boolean isBluetoothEnabled() {
    return driver.findElement(bluetoothBtn).getAttribute("value").equals("1");
}

     In the above code, tirst open the settings and navigate to Bluetooth page, then checking the current Bluetooth status using isBluetoothEnabled() function and based on the status applied to the setBluetooth() function, it will enable/disable the Bluetooth.

     I hope you really enjoyed to read this article and got some idea on Bluetooth switching for Android and iOS platforms. Try to implement the above logic from your end based on the automation requirement.

make it perfect!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a website or blog at WordPress.com

Up ↑

%d bloggers like this: