grishka / NearDrop

An unofficial Google Nearby Share/Quick Share app for macOS
The Unlicense
4.13k stars 153 forks source link

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

Open aditya-mankal opened 5 months ago

aditya-mankal commented 5 months ago

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 commented 5 months ago

You need to advertise a BLE service with these parameters:

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 commented 5 months ago

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 commented 5 months ago

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

aditya-mankal commented 5 months ago

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 commented 5 months ago

Yes, to the one I already given

aditya-mankal commented 5 months ago

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 commented 5 months ago

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 commented 5 months ago

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 commented 5 months ago

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.

https://github.com/grishka/NearDrop/issues/154#issuecomment-2024693030

aditya-mankal commented 5 months ago

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

rickymohk commented 3 months ago

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

grishka commented 3 months ago

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