简体   繁体   English

Xamarin 表单和 Android 蓝牙

[英]Xamarin forms and Android Bluetooth

I am developing a cross platform app using Xamarin forms.我正在使用 Xamarin 表单开发跨平台应用程序。

I have an ObserveableCollection and I want to populate it with bluetooth devices that have been found during a search.我有一个 ObserveableCollection,我想用搜索过程中找到的蓝牙设备填充它。 The search is/has to be platform specific and is performed through a DependencyService.搜索是/必须是特定于平台的,并通过 DependencyService 执行。 I then want to display this ObserveableCollection in a Xamarin.Forms ListView, so the user can see all the found bluetooth devices.然后我想在 Xamarin.Forms ListView 中显示这个 ObserveableCollection,以便用户可以看到所有找到的蓝牙设备。 In this case, I'm interested in the Android implementation.在这种情况下,我对 Android 实现感兴趣。

I have a basic goal: Discover Bluetooth devices and display them in a Xamarin forms ListView.我有一个基本目标:发现蓝牙设备并将它们显示在 Xamarin 表单 ListView 中。

Here is what I have:这是我所拥有的:

In BluetoothPage.xaml.cs在 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.上面的代码意味着当 BT_Switch 被翻转时,应用程序应该开始扫描蓝牙设备并用结果填充 Bluetooth_Device_ListView。

In BluetoothServices.cs在 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这用作 Android 特定代码的 DependencyService 接口

In BluetoothServices.Android.cs在 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来自 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. PS:您可以假设适配器已启用并可以使用。

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 .使用 MessagingCenter 发送数据,因为 BroadcastReceiver.OnReceive 独立工作,您可能没有从您拥有的 BTScan 功能填充设备列表。 Instead use MessagingCenter to pass any new bluetooth device found from BroadcastReceiver而是使用 MessagingCenter 传递从 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.在 BluetoothDeviceReceiver 的 if 子句中。 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这应该解决它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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