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

BLE devices are retuning device name NULL during scanning process on android 13 #660

Open
BasuHuddar opened this issue Mar 20, 2023 · 36 comments · May be fixed by #667
Open

BLE devices are retuning device name NULL during scanning process on android 13 #660

BasuHuddar opened this issue Mar 20, 2023 · 36 comments · May be fixed by #667

Comments

@BasuHuddar
Copy link

We are not getting the BLE device name while scanning the Bluetooth devices in android 13.

Steps to reproduce

  1. Scan any BLE devices in android 13

Expected behavior

We should get the device name in the scanned result

Actual behavior

The device name is null

Configuration

Version of the Plugin: 2.1.3

Platform: e.g. Android .. (including version!!! e.g. Android 13)

Device: e.g. Samsung, Redmi

@smsissuechecker
Copy link

Hi @BasuHuddar,

I'm the friendly issue checker.
Thanks for using the issue template 🌟
I appreciate it very much. I'm sure, the maintainers of this repository will answer, soon.

@janusw
Copy link
Member

janusw commented Mar 20, 2023

Similar observations have been reported before, e.g. in #456.

Is the device name missing for all devices that are returned by the scan, or only some of them?

@BasuHuddar
Copy link
Author

@janusw Yes, it is missing all the device's names in android 13.

@sisaacks
Copy link

sisaacks commented Mar 29, 2023

I am able to see the device name and device Id for some but not for others. HOWEVER since the latest Android update with Android 13, if a device drops connection, I cannot seed the device again unless I restart the app. Then the device is seen an automatically connects again.

when the device drops connection I have tried stopping and restarting the scanning but the device is never found again. Other devices are, just not the device that lost connection. I have tested this with a variety of products.

I guess I should submit a bug for this as well
#665

@BasuHuddar
Copy link
Author

@janusw, i got the fix for this, should we get access to the code to edit and generate a nupkg?

@janusw
Copy link
Member

janusw commented Apr 3, 2023

@BasuHuddar If you have a fix, you could either just post a patch/snippet here or ideally open a Pull Request.

@sisaacks
Copy link

sisaacks commented Apr 3, 2023

@janusw @BasuHuddar I would be curious if it fixes the issue I listed in #665

@BasuHuddar
Copy link
Author

if (string.IsNullOrEmpty(BluetoothDevice.Name))
{
ByteBuffer order = ByteBuffer.Wrap(AdvertisementData).Order(ByteOrder.LittleEndian);
while (order.Remaining() > 2)
{
byte b = (byte)order.Get();
if (b != 0)
{
if (order.Get() != 9)
{
order.Position((order.Position() + b) - 1);
}
else
{
byte[] bArr2 = new byte[(b - 1)];
order.Get(bArr2);
try
{
Name = (System.Text.Encoding.UTF8.GetString(bArr2)).Trim();
}
catch (UnsupportedEncodingException unused)
{
Trace.Message("Error occured while converting byte to string", unused.Message);
}
}
}
}
}
else
{
Name = BluetoothDevice.Name;
}
@janusw here is the code base we need to chage it device class in android

@sisaacks
Copy link

sisaacks commented Apr 3, 2023 via email

@janusw
Copy link
Member

janusw commented Apr 4, 2023

@janusw here is the code base we need to chage it device class in android

Have you actually tried if this fixes the problem? I haven't looked at it very closely yet. Where exactly does this code need to go?

What you should do to go forward with this is:

  • fork this repo and clone it (if you haven't done that)
  • create a new branch, commit your changes and push it to your fork
  • open a PR from the branch

The PR will actually generate a nupkg automatically. Then others can try and review it.

@BasuHuddar
Copy link
Author

@janusw Yes it's fixed the problem for me, From which branch should i create the branch?

@janusw
Copy link
Member

janusw commented Apr 5, 2023

master, please :)

@BasuHuddar
Copy link
Author

Screenshot 2023-04-05 at 18 25 46

Unable to build it getting errors, but I'm able to build tag 2.1.3, i tested here it's working
Same code i will add them to the master fork and i will create the pr to it.

@janusw are you ok with this?

@BasuHuddar
Copy link
Author

Screenshot_20230405_184349

@janusw I tested in Tag 2.1.3 it working, PFA

@janusw
Copy link
Member

janusw commented Apr 6, 2023

Unable to build it getting errors, but I'm able to build tag 2.1.3, i tested here it's working Same code i will add them to the master fork and i will create the pr to it.

@janusw are you ok with this?

We will not have another 2.x release, so in the end it will need to be applied to master. If you have trouble building master, you can also start with a branch based on 2.1.3 and we forward-port it from there.

Or you port your code to master yourself. The CI will tell you if there are build errors.

@BasuHuddar
Copy link
Author

@janusw Here is my commit, check this and let me know if it's working or not.

d452eea

@janusw
Copy link
Member

janusw commented Apr 6, 2023

@janusw Here is my commit, check this and let me know if it's working or not.

d452eea

Thanks for the commit. It seems you're taking the device name from advertisement records of type CompleteLocalName, right?

I think the idea as such is pretty good, but the technical implementation needs some adjustment IMHO:

  • Instead of parsing the raw advertisment data yourself, you could use the AdvertisementRecords member of the Device class (which is generated by the method ParseScanRecord). Then you don't need to add a new member AdvertisementData.
  • You could set the name based on the advertisement in the Device constructor already, and possibly update it via the NativeDevice's name if that is non-empty (in the Update method).

@BasuHuddar
Copy link
Author

@janusw I dint get it, but i checked the code in the parseScanrecord you are using for getting the advertisement data, but how should we use it to assign the name of the BLE device?

One more before parsing the advertising byte we were setting the name in the update method.
If you know very well, could please apply this code there?

@janusw
Copy link
Member

janusw commented Apr 7, 2023

@janusw I dint get it, but i checked the code in the parseScanrecord you are using for getting the advertisement data, but how should we use it to assign the name of the BLE device?

Let me take care of molding this into the right form. I'll also create a PR from it. Maybe you can test once more after I'm done ...

@janusw janusw linked a pull request Apr 7, 2023 that will close this issue
@janusw
Copy link
Member

janusw commented Apr 7, 2023

@janusw I dint get it, but i checked the code in the parseScanrecord you are using for getting the advertisement data, but how should we use it to assign the name of the BLE device?

Let me take care of molding this into the right form. I'll also create a PR from it. Maybe you can test once more after I'm done ...

See #667.

@janusw
Copy link
Member

janusw commented Apr 9, 2023

@BasuHuddar Two more questions here:

  • Are you only seeing this with version 2.x, or also with 3.0 beta?
  • On which host device do you observe the missing device names?

@BasuHuddar
Copy link
Author

@janusw Yes I'm getting the device name null in the 2.x and 3.0 beta version
Android 13(Samsung M33 5G)

@janusw
Copy link
Member

janusw commented Apr 17, 2023

I recently noticed that in the developer settings (networking section), there is an option to show Bluetooth devices without names (Mac addr only). @BasuHuddar Could you check if you have that switch enabled?

@BasuHuddar
Copy link
Author

@janusw I tried but no luck on that as well

@janusw
Copy link
Member

janusw commented Apr 18, 2023

@janusw I tried but no luck on that as well

What do you mean? You could not check it? Or "show devices without names" is not enabled?

If the developer mode is not enabled, try this:
https://developer.android.com/studio/debug/dev-options#enable

@BasuHuddar
Copy link
Author

@janusw I tried but no luck on that as well

What do you mean? You could not check it? Or "show devices without names" is not enabled?

If the developer mode is not enabled, try this: https://developer.android.com/studio/debug/dev-options#enable

I enable the developer mode and switched enabled for the Bluetooth devices without names (Mac address only), but it's not working

@janusw
Copy link
Member

janusw commented Apr 18, 2023

I enable the developer mode and switched enabled for the Bluetooth devices without names (Mac address only), but it's not working

Well, of course. If that switch is enabled, then you will certainly not see any device names.

The question is: Do you still fail to see device names if the switch is off?

Do I understand correctly that the device names are only missing on one particular device (Samsung M33), but not on others?

@BasuHuddar
Copy link
Author

I enable the developer mode and switched enabled for the Bluetooth devices without names (Mac address only), but it's not working

Well, of course. If that switch is enabled, then you will certainly not see any device names.

The question is: Do you still fail to see device names if the switch is off?

Do I understand correctly that the device names are only missing on one particular device (Samsung M33), but not on others?

Yes, I haven't seen any device names, if it's switched off or switched on.

We are seeing this in Samsung M33 and Redmi phones(Android 13).

As I don't haves the other phone to check it here.

@janusw
Copy link
Member

janusw commented Apr 18, 2023

I enable the developer mode and switched enabled for the Bluetooth devices without names (Mac address only), but it's not working

Well, of course. If that switch is enabled, then you will certainly not see any device names.
The question is: Do you still fail to see device names if the switch is off?
Do I understand correctly that the device names are only missing on one particular device (Samsung M33), but not on others?

Yes, I haven't seen any device names, if it's switched off or switched on.

Ok, thanks for checking.

We are seeing this in Samsung M33 and Redmi phones(Android 13).

AFAICS it is not a general Android 13 problem, since my Samsung S21+ seems to show device names properly.

As I don't haves the other phone to check it here.

Hm, but in #667 (comment) you said that you see CompleteLocalName ad records on a Samsung M40 on Android 11, right?

I still wonder how the patch that you proposed above can work if you don't see any CompleteLocalName records on the M33? This can only mean that the parsing of those records somehow fails in Plugin.BLE. It would be great if you could verify/debug this hypothesis, otherwise we'll not be able to make progress here, unfortunately (I cannot reproduce the issue after all).

@janusw
Copy link
Member

janusw commented Apr 20, 2023

@BasuHuddar Could this possibly be related to #567? Do you see a System.ArgumentException during the parsing of the advertisement?

@BasuHuddar
Copy link
Author

BasuHuddar commented Apr 21, 2023

I enable the developer mode and switched enabled for the Bluetooth devices without names (Mac address only), but it's not working

Well, of course. If that switch is enabled, then you will certainly not see any device names.
The question is: Do you still fail to see device names if the switch is off?
Do I understand correctly that the device names are only missing on one particular device (Samsung M33), but not on others?

Yes, I haven't seen any device names, if it's switched off or switched on.

Ok, thanks for checking.

We are seeing this in Samsung M33 and Redmi phones(Android 13).

AFAICS it is not a general Android 13 problem, since my Samsung S21+ seems to show device names properly.

As I don't haves the other phone to check it here.

Hm, but in #667 (comment) you said that you see CompleteLocalName ad records on a Samsung M40 on Android 11, right?

Yes it's an Android 11

I still wonder how the patch that you proposed above can work if you don't see any CompleteLocalName records on the M33? This can only mean that the parsing of those records somehow fails in Plugin.BLE. It would be great if you could verify/debug this hypothesis, otherwise we'll not be able to make progress here, unfortunately (I cannot reproduce the issue after all).

Yes debugged here, but I'm not finding it completeLocalname

@BasuHuddar
Copy link
Author

@BasuHuddar Could this possibly be related to #567? Do you see a System.ArgumentException during the parsing of the advertisement?

**On My side I am not seeing System.ArgumentException during the parsing of the advertisement **

@janusw
Copy link
Member

janusw commented Apr 28, 2023

Actually I found one condition, under which I can reproduce missing device names on Android (v13) ...

If I use a scan filter, which excludes ShortLocalName and CompleteLocalName records (e.g. a ServiceDataFilter), then I actually see no devices names for the discovered devices.
If I disable the scan filter, I observe ShortLocalName and CompleteLocalName records coming in and the devices names are set correctly.

@BasuHuddar Could your issue be related to scan filtering? Did you use a scan filter in your tests? Can you reproduce the issue when no scan filter is in effect?

@paulobikiwelu

This comment was marked as off-topic.

@janusw
Copy link
Member

janusw commented Apr 28, 2023

:) 😀 please help.

Can't see any device during Scanning...

@paulobikiwelu Please open a new issue, this one is about missing device names.

@paulobikiwelu

This comment was marked as off-topic.

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

Successfully merging a pull request may close this issue.

5 participants