Hi all,
i am new in Xamarin an i tried to use Bluetooth in my Xamarin.Forms project. I have looked in the sample code from Monkey.Robotics project and have done the same.
But when i try
static IAdapter Adapter;
in App.cs, the Adapter is null and a exeption occurs when i use the Adapter.
Have anyone an idea?
Here is my code:
App.cs :
`using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xamarin.Forms;
using Robotics.Mobile.Core.Bluetooth.LE;
namespace AwigoWaageXamarinForms
{
public class App : Application
{
static IAdapter Adapter;
public App()
{
// The root page of your application
MainPage = new NavigationPage(new AwigoWaageXamarinForms.MainPage(Adapter));
}
public static void SetAdapter (IAdapter adapter)
{
Adapter = adapter;
}
...
}
}`
MainPage.xaml.cs :
`using System;
using System.Collections.Generic;
using Xamarin.Forms;
using Robotics.Mobile.Core.Bluetooth.LE;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Collections.ObjectModel;
namespace AwigoWaageXamarinForms
{
public partial class MainPage : ContentPage
{
IAdapter adapter;
ObservableCollection<IDevice> devices;
public MainPage (IAdapter adapter)
{
InitializeComponent ();
this.adapter = adapter;
this.devices = new ObservableCollection<IDevice> ();
listView.ItemsSource = devices;
// !!!!!!!! here occurs the Exeption, because adapter is null !!!!!!!!!!!!!!!!!!
adapter.DeviceDiscovered += (object sender, DeviceDiscoveredEventArgs e) => {
Device.BeginInvokeOnMainThread(() => {
devices.Add (e.Device);
});
};
adapter.ScanTimeoutElapsed += (sender, e) => {
adapter.StopScanningForDevices(); // not sure why it doesn't stop already, if the timeout elapses... or is this a fake timeout we made?
Device.BeginInvokeOnMainThread ( () => {
DisplayAlert("Timeout", "Bluetooth scan timeout elapsed", "OK", "");
});
};
}
...
`