0

I created a new Xamarin.Forms app to test basic Bluetooth functionality. I downloaded this plugin into both the Android project and the shared project:

https://github.com/aritchie/bluetoothle

I wrote this function in the shared project and am calling it from the OnCreate of my launch activity in my Android project:

public static async Task BroadcastBluetooth()
{
    // (I do not await this function when I call it)

    try
    {
        await Task.Delay(5000); // just to make sure we give enough time for all initialization to complete

        _server = CrossBleAdapter.Current.CreateGattServer();

        // exception thrown on this line
        await _server.Start(new AdvertisementData
        {
            LocalName = "TestServer",
            ServiceUuids = new List<Guid>()
        });
    }
    catch (Exception e)
    {

    }
}

It throws this exception:

{System.NullReferenceException: Object reference not set to an instance of an object. at Plugin.BluetoothLE.Server.GattServer.StartAdvertising (Plugin.BluetoothLE.Server.AdvertisementData adData) [0x00095] in C:\dev\acr\bluetoothle\Plugin.BluetoothLE.Android\Server\GattServer.cs:135 at Plugin.BluetoothLE.Server.GattServer.Start (Plugin.BluetoothLE.Server.AdvertisementData adData) [0x00011] in C:\dev\acr\bluetoothle\Plugin.BluetoothLE.Android\Server\GattServer.cs:70 at App2.App+d__7.MoveNext () [0x00097] in C:\Projects\app2\App2\App2\App.xaml.cs:49 }

I'm only doing something really basic so I must be missing something? The exception is referencing a directory path of the plugin's developer's machine (C:\dev\acr...) so either this plugin is broken or I'm doing something really wrong?

Drake
  • 2,679
  • 4
  • 45
  • 88

1 Answers1

0

I am using the same plugin in a current project of mine and it is working fine. So I doubt the issue is within the plugin.

Some thoughts about what could cause the issue:

  1. Are you testing this code on a real device that is capable of performing BLE Advertisement?
  2. Do you have set the permissions accordingly in your android project?
  3. Does BLE Advertising work when you use the native android apis?

It also would be helpful, if you could attach a repository with which I can reproduce the issue.

user2074945
  • 537
  • 2
  • 8
  • 22
  • I am testing it with a real device, Nexus 5. I copy/pasted all the permissions as instructed on the website. I will have to verify with the native android apis. Thanks I uploaded the project here: https://drive.google.com/file/d/14Cs4JE7p2pUurtHL6H5cUCijcwzqPWa5/view?usp=sharing – Drake Mar 23 '18 at 18:27
  • 1
    I didn't check out your project yet, but I found that the Nexus 5 might not be able to do Bluetooth Advertisement: https://stackoverflow.com/questions/26441785/does-bluetoothleadvertiser-work-on-a-nexus-5-with-android-5-0 – user2074945 Mar 23 '18 at 18:29
  • Interesting...is there another method to "advertise" so another BLE device can scan and detect my app? I mean is support discontinued in favor of another way? – Drake Mar 23 '18 at 18:38
  • 1
    Not that I am aware of.. If another device is capable of "advertising" the nexus could find it via "scanning" and then connect to it. It that case both devices could exchange data. However, I think there is no way for your Nexus to to be found by other devices. To be sure about this, you could download https://play.google.com/store/apps/details?id=com.radiusnetworks.locate&hl=de and try to transmit as a beacon with this app. If it doesn't work, you phone can not do it. I had the same problem and ended up buying a 30 dollar android phone that is able to advertise. – user2074945 Mar 23 '18 at 18:44
  • Thanks for the suggestion, I will go with that approach (the Nexus device will scan for an ID that is advertised by the other app). I hope this works out! – Drake Mar 23 '18 at 19:12
  • Do you know if iOS devices can scan for BLE devices while the app is in the background, and connect to it so it can write back to it? – Drake Mar 23 '18 at 19:13
  • 1
    iOS can scan for BLE devices while in the background, but with severe limitations. Here are more information about it: https://developer.apple.com/library/content/documentation/NetworkingInternetWeb/Conceptual/CoreBluetooth_concepts/CoreBluetoothBackgroundProcessingForIOSApps/PerformingTasksWhileYourAppIsInTheBackground.html What is possible however, is that your iOS application, once connected to a different BT device, can get notified while in background, which gives your ios app around 10 seconds to process the notification data until returned to background again. – user2074945 Mar 23 '18 at 19:29