Search code examples
xamarin.androidbluetoothandroid-bluetooth

Checking bluetooth IsEnabled is crashing my app


I am creating an AndroidBluetoothService to handle Bluetooth Interactions from my Xamarin application.

In this service, I use a private class variable:

    private BluetoothAdapter ServiceAdapter;

Then I initialize it in the constructor:

        ServiceAdapter = BluetoothAdapter.DefaultAdapter;

Then, when I check to see if it's enabled:

            bool enabled = ServiceAdapter.IsEnabled;

I get an exception that crashes my app:

System.NotSupportedException
  Message=Could not activate JNI Handle 0xbef405b8 (key_handle 0xdb031b8) of Java type 'crc6402525acf0275fedf/MainActivity' as managed type 'ArduinoSerial.MainActivity'.

Where ArduinoSerial is the name of my project/app and MainActivity is the activity being run. I have commented and uncommented code to find out exactly which line is blowing up. Not sure how to proceed from here, but am skipping the line for now and just assuming it's enabled.


Solution

  • It appears that the problem was caused by the app not having permissions for bluetooth. I added

    to AndroidManifest.xml and it fixed the problem.

    Way to create an obtuse error for a simple problem. :( Hope this helps someone else.