1

im currently working on a xamarin.forms app (only android at the moment). This app should perform some tasks if a bluetooth device is connected (app is not running). The task is to collect some data from sensors and write it do sqlite database.

I managed to catch bluetooth events using broadcastreceivers ...

My question is How should i collect and write those data (GPS) ?

Can i just call them through some events in PCL ? Or do i need some services ? Or should i create that funcionality in .android project ?

Im a newbie in xamarin and android so i appreciate any help :)

Thanks

boris_dv
  • 123
  • 1
  • 9

1 Answers1

0

My question is How should i collect and write those data (GPS) ? Can i just call them through some events in PCL ? Or do i need some services ? Or should i create that funcionality in .android project ?

There are several ways to get the sensors data, you could so this in your PCL.

Method 1 :

When you catch bluetooth events in your BroadcastReceiver, you could get sensors data in the OnReceive method.

Method 2 :

You could use DependencyService to get the device sensors data and then write it do sqlite database. As the document said :

Xamarin.Forms allows developers to define behavior in platform-specific projects. DependencyService then finds the right platform implementation, allowing shared code to access the native functionality.

About DependencyService usage you could read the document or refer to my answer.

Method 3 :

You could use DeviceMotionPlugin to get these sensors data. When you use this plugin, you can call CrossDeviceMotion.Current from any project or PCL to gain access to APIs.

Update :

You could do implement this function in your PCL. When you use method 2 or method 3, you could get the sensors data in PCL and then you could store these into your sqlite. Still use native Android API to get sensors data in native Android, but you could use these data in PCL.

Community
  • 1
  • 1
York Shen
  • 9,014
  • 1
  • 16
  • 40
  • thanks a lot for your answer , so i need to move the gps functionality (geolocator plugin) and sqlite functions to .android project. Call these in OnReceive() in background and when the app is running call them using DependencyService. Right ? – boris_dv Dec 05 '17 at 10:07
  • @boris_dv, I have update my answer, if there is any problem, please feel free to ask. – York Shen Dec 05 '17 at 11:56