Search code examples
c#xamarinraspberry-pi3gpioandroid-things

android.os.ServiceSpecificException Caller does not own BCM26 (code 13)


I have written a Xamarin.Android C# class that manages the io pins for the Android Things OS running on my Raspberry Pi 3. When I try to read the pin this exception is raised:

android.os.ServiceSpecificException: Caller does not own BCM26 (code 13)

from :

1   // setup 
2   var io = new PeripheralManagerService();
3   Gpio p = io.OpenGpio("BCM26");
4   p.SetDirection(Gpio.DirectionIn);
5
6   // later I read it 
7   if (p.Value == HIGH)
8      Console.WriteLine("Pin is high");
9   // Exception raised 

What should I do to resolve this issue?

edit: The code above will actually work. I tried to only extract the relevant parts into a small sample of code because the original was multi threaded and too complicated to post, which led to an answer why the exception was raised. It wasn't related to Android Services and Permissions. The pin I tried to read was closed after having its direction set. Removing the call to close resolved this issue.


Solution

  • Pin must remain open (don't close it until you've really finished with it) otherwise attempts to read the pin would raise the cryptic exception which confused me with Android permissions like internet-use or record-audio permissions.