简体   繁体   中英

Xamarin forms and Android Bluetooth

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<BluetoothServices>().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<object> 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<object> BluetoothDeviceList;

        public void InitializeBluetooth()  // Initialises bluetooth adapter settings
        {
            adapter = BluetoothAdapter.DefaultAdapter;
            BluetoothDeviceList = new ObservableCollection<object>();
        }

...

        public ObservableCollection<object> 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.

Use MessagingCenter to send the data as BroadcastReceiver.OnReceive works independently and you might not have the device list populated from the BTScan function you have . Instead use MessagingCenter to pass any new bluetooth device found from BroadcastReceiver

I know it's a little bit late now, but I am currently facing a similar problem. I don't know whether it is still relevant, but it looks like you forgot a '!' in your if-clause in the BluetoothDeviceReceiver. With your Code

if (BluetoothServicesAndroid.BluetoothDeviceList.Contains(intent.GetParcelableExtra(BluetoothDevice.ExtraDevice)))
{

    BluetoothServicesAndroid.BluetoothDeviceList.Add((BluetoothDevice)intent
        .GetParcelableExtra(BluetoothDevice.ExtraDevice));

}

you check whether your list contains the device and if it does, you want to add it. But because the list starts out empty, no device will ever be added to your list.

if (!BluetoothServicesAndroid.BluetoothDeviceList.Contains(intent.GetParcelableExtra(BluetoothDevice.ExtraDevice)))
{

    BluetoothServicesAndroid.BluetoothDeviceList.Add((BluetoothDevice)intent
        .GetParcelableExtra(BluetoothDevice.ExtraDevice));

}

This should fix it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM