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

Discovery of Android devices from Windows - without Google Files or Activity Intent #154

Open
aditya-mankal opened this issue Mar 26, 2024 · 12 comments

Comments

@aditya-mankal
Copy link

Hello @grishka,

Kudos on the amazing solution for content sharing between MacOS and Android!

I am attempting to build a similar solution for Windows, but I need a little help in sending the signal from Windows to make the nearby Android devices visible. I don't want to use the Google Files app or trigger the actvity-intent (com.google.android.gms.RECEIVE_NEARBY).

Can you help me understand how I can send this signal? Any sample code on how the message can be constructed would be very helpful!

image

Thank you!

@grishka
Copy link
Owner

grishka commented Mar 26, 2024

You need to advertise a BLE service with these parameters:

  • Service UUID = fe 2c
  • Service data = fc 12 8e 01 42 00 00 00 00 00 00 00 00 00 [10 random bytes]

If you're using win32 APIs, this looks like a good starting point (it's unreasonably messy though).

For UWP/WinRT, use this.

(sorry, I'm too lazy to set up Visual Studio on my VM to provide you with a minimal working code example for this specific case)

@aditya-mankal
Copy link
Author

Thank you, @grishka!
I have broadcasted a BLE service as you have suggested, but the Android device is still not discoverable until I fire the Activity Intent or use an older version of the Google Files app and tap "receive".

Can you please tell me if I am missing something here? Thank you!

` public sealed class BLEAdvertisementService
{
private BluetoothLEAdvertisementWatcher watcher;
private BluetoothLEAdvertisementPublisher publisher;

    // Service UUID: fe2c
    private const string ServiceUuid = "0000fe2c-0000-1000-8000-00805f9b34fb";

    // Service Data: fc 12 8e 01 42 00 00 00 00 00 00 00 00 00 [10 random bytes]
    private readonly byte[] serviceData = { 0xFC, 0x12, 0x8E, 0x01, 0x42, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };

    public void StartAdvertising()
    {
        publisher = new BluetoothLEAdvertisementPublisher();

        // Create the service data section of the advertisement
        var serviceDataSection = new BluetoothLEAdvertisementDataSection();
        var writer = new DataWriter();
        writer.WriteBytes(serviceData);
        serviceDataSection.Data = writer.DetachBuffer();

        // Add the service data section to the advertisement
        publisher.Advertisement.DataSections.Add(serviceDataSection);

        // Set the UUIDs of the services being advertised
        publisher.Advertisement.ServiceUuids.Add(new Guid(ServiceUuid));

        // Start advertising
        publisher.Start();
    }

    public void StopAdvertising()
    {
        publisher?.Stop();
        publisher = null;
    }
}`

@grishka
Copy link
Owner

grishka commented Mar 27, 2024

You need to append 10 random bytes to the service data.

@aditya-mankal
Copy link
Author

Excuse my naivety here. Can you please explain what the service data should be? Is it any random 10 bytes? Or should I append 10 random bytes to the service data that you have already given - fc 12 8e 01 42 00 00 00 00 00 00 00 00 00

@grishka
Copy link
Owner

grishka commented Mar 27, 2024

Yes, to the one I already given

@aditya-mankal
Copy link
Author

Thank you, @grishka! Can you please tell me if I can run this BLE service as a separate process (independent of my actual UWP app) for now? I have a few build issues with my UWP app as of now. I want to quickly establish if the whole approach works for me.

And in case you have a publicly available Windows equivalent of this project of yours, it would be a great inspiration to me!

@grishka
Copy link
Owner

grishka commented Mar 27, 2024

You can. It doesn't matter which process you run it in, that doesn't change the broadcasts that go out. I was able to make them with "nRF Connect" Android app, for example. That app is also how I determined the format.

@aditya-mankal
Copy link
Author

aditya-mankal commented Mar 28, 2024

Hello @grishka,

Despite broadcasting the said signal, my Android device is still not visible without using an older version of the Google Files app and tapping "receive".

Here is my broadcast code:

namespace App1.Source
{
    using System;
    using Windows.Devices.Bluetooth.Advertisement;
    using Windows.Storage.Streams;
    public sealed class BLEAdvertisementService
    {
        private BluetoothLEAdvertisementPublisher publisher;
        private string serviceUuid = "0000fe2c-0000-1000-8000-00805f9b34fb";
        private byte[] serviceData = { 0xFC, 0x12, 0x8E, 0x01, 0x42, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x40 };

        public void StartAdvertising()
        {
            publisher = new BluetoothLEAdvertisementPublisher();

            // Create the service data section of the advertisement
            var serviceDataSection = new BluetoothLEAdvertisementDataSection();
            var writer = new DataWriter();
            writer.WriteBytes(serviceData);
            serviceDataSection.Data = writer.DetachBuffer();

            // Add the service data section to the advertisement
            publisher.Advertisement.DataSections.Add(serviceDataSection);

            // Set the UUIDs of the services being advertised - NOT WORKING
            //publisher.Advertisement.ServiceUuids.Add(new Guid(serviceUuid));

            // Start advertising
            publisher.Start();
        }

        public void StopAdvertising()
        {
            publisher?.Stop();
            publisher = null;
        }
    }
}

Edit: @grishka, if I set the serviceUuid, then I see an exception "Value does not fall within the expected range" when the publisher starts. I tried to understand the significance of Service Uuid and the Service Data, but I am just getting started.
Any help from your end to correctly broadcast would be critical in me succeeding with this project!

Thanks a lot!

@aditya-mankal
Copy link
Author

Hello @grishka, hope you are doing great! I spent some more time debugging my code and gained a deeper understanding. Updated my previous question. Please consider responding when feasible.

#154 (comment)

@aditya-mankal
Copy link
Author

Hello @grishka, hope you are doing great! Looking forward to your inputs in fixing this.
Thanks a lot!

@rickymohk
Copy link

Why would you want to do that while the official quick share for windows solution exists?

@grishka
Copy link
Owner

grishka commented May 22, 2024

By the way, you can use this app to see what you're actually sending out

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

No branches or pull requests

3 participants