Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add whether Bluetooth scan is in traditional mode(ScanSettings.Builder.setLegacy()) #763

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

l0z0y
Copy link

@l0z0y l0z0y commented Mar 27, 2024

PixPin_2024-03-27_13-19-00
While using the application recently, I encountered an issue where the device couldn't be scanned. After investigating, I found that the device's advertising type is Bluetooth 5 Advertising Extension. Upon consulting the Android documentation, I discovered that the ScanSettings.Builder.setLegacy() method was introduced in Android 26 API.

Link to Android Documentation

setLegacy(false) is a method in ScanSettings.Builder that determines whether the scan operates in "legacy" mode. With Bluetooth 5, a new scanning mode was introduced to support additional features like extended advertising and long data packets. Conversely, "legacy" mode only supports traditional advertising data types and does not accommodate Bluetooth 5's extended advertising.

Hence, when you use setLegacy(false), you're instructing the system not to employ "legacy" mode, but rather to use the new mode, enabling scanning for Bluetooth 5's extended advertisements and other new features.

Based on this understanding, I propose adding a parameter isLegacy() to confirm whether to activate scanning in legacy mode. Below is the code I added:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    boolean isLegacy = obj.optBoolean(keyIsLegacy, true);
    scanSettings.setLegacy(isLegacy);
}

This change allows for flexible configuration of the scan mode, ensuring compatibility and functionality as required.

add isLegacy options
add isLegacy option
Add error handling
@l0z0y l0z0y closed this Mar 27, 2024
@l0z0y l0z0y reopened this Mar 27, 2024
@l0z0y
Copy link
Author

l0z0y commented Mar 27, 2024

I modified the code to add error catch, and I think it should default to setLegacy(false)

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
	boolean isLegacy = obj.optBoolean(keyIsLegacy, false);
	try {
	  scanSettings.setLegacy(isLegacy);
	} catch (IllegalArgumentException e) {
	}
      }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant