I am developing a cross platform app using Xamarin forms.
I have an ObserveableCollection and I want to populate it with bluetooth devices that have been found during a search.
The search is/has to be platform specific and is performed through a DependencyService. I then want to display this ObserveableCollection in a Xamarin.Forms ListView, so the user can see all the found bluetooth devices. In this case, I'm interested in the Android implementation.
I have a basic goal: Discover Bluetooth devices and display them in a Xamarin forms ListView.
Here is what I have:
In BluetoothPage.xaml.cs
using Phone_App.Services;
if (BT_Switch.IsToggled) //if Bluetooth is switched on, scan for devices
{
Bluetooth_Device_ListView.ItemsSource = DependencyService.Get().BTDeviceScan(); //populate the Bluetooth device ListView with the found devices
}
The code above means that when BT_Switch is flipped, the app should start scanning for Bluetooth Devices and populate the Bluetooth_Device_ListView with the result.
In BluetoothServices.cs
namespace Phone_App.Services
{
public interface BluetoothServices
{
void InitializeBluetooth(); // Initialises bluetooth adapter settings
bool CheckAdapterStatus(); // Returns true/false if bluetooth is already active
void ToggleAdapter(bool switchState); // Toggles the bluetooth adapter on/off
ObservableCollection BTDeviceScan(); // Scans for available bluetooth devices
}
}
This serves as the DependencyService Interface to the Android specific code
In BluetoothServices.Android.cs
[assembly: Dependency(typeof(BluetoothServicesAndroid))]
namespace Phone_App.Services.Droid
{
public class BluetoothServicesAndroid : BluetoothServices
{
// Declare class members
private static BluetoothAdapter adapter;
public static ObservableCollection BluetoothDeviceList;
public void InitializeBluetooth() // Initialises bluetooth adapter settings
{
adapter = BluetoothAdapter.DefaultAdapter;
BluetoothDeviceList = new ObservableCollection();
}
...
public ObservableCollection BTDeviceScan() // Scans for available bluetooth devices
{
adapter.StartDiscovery();
return BluetoothDeviceList;
}
}
}
From MainActivity.cs
public class MainActivity : Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
BluetoothDeviceReceiver BluetoothReceiver;
protected override void OnCreate(Bundle bundle)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(bundle);
Xamarin.Forms.Forms.Init(this, bundle);
LoadApplication(new App());
BluetoothReceiver = new BluetoothDeviceReceiver();
RegisterReceiver(BluetoothReceiver, new IntentFilter(BluetoothDevice.ActionFound));
}
}
public class BluetoothDeviceReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
if (intent.Action == BluetoothDevice.ActionFound)
{
if (BluetoothServicesAndroid.BluetoothDeviceList.Contains(intent.GetParcelableExtra(BluetoothDevice.ExtraDevice)))
{
BluetoothServicesAndroid.BluetoothDeviceList.Add((BluetoothDevice)intent.GetParcelableExtra(BluetoothDevice.ExtraDevice));
}
}
}
}
This code seems (mostly) correct to me, but the list view is not being populated with anything. Ideally I'd like it to be populated in real time, as each device is found. Can anyone offer help or tell me what I need to change? Thanks.
PS: you can assume the adapter is enabled and ready to use.