c# - Xamarin 表单和 Android 蓝牙

标签 c# android xamarin xamarin.forms bluetooth

我正在使用 Xamarin 表单开发跨平台应用程序。

我有一个 ObserveableCollection,我想用在搜索过程中找到的蓝牙设备填充它。 搜索是/必须是特定于平台的,并且通过 DependencyService 执行。然后我想在 Xamarin.Forms ListView 中显示此 ObserveableCollection,以便用户可以看到所有找到的蓝牙设备。在这种情况下,我对 Android 实现很感兴趣。

我有一个基本目标:发现蓝牙设备并在 Xamarin 表单 ListView 中显示它们。

这是我的:

在 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
}

上面的代码意味着当 BT_Switch 被翻转时,应用程序应该开始扫描蓝牙设备并用结果填充 Bluetooth_Device_ListView。

在 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

    }
}

这用作 Android 特定代码的 DependencyService 接口(interface)

在 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;
        }
    }
}

来自 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));
            }
        }
    }
}

这段代码对我来说似乎(大部分)是正确的,但是 ListView 没有填充任何东西。理想情况下,我希望在找到每个设备时实时填充它。谁能提供帮助或告诉我需要更改的内容?谢谢。

PS:您可以假设适配器已启用并准备好使用。

最佳答案

使用 MessagingCenter 发送数据,因为 BroadcastReceiver.OnReceive 独立工作,您可能没有从您拥有的 BTScan 函数填充设备列表。而是使用 MessagingCenter 传递从 BroadcastReceiver 找到的任何新蓝牙设备

关于c# - Xamarin 表单和 Android 蓝牙,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51877326/

相关文章:

c# - 拖放后将 div 保存到图像

c# - 将 <T>(T obj) 与 Linq 查询结合使用

java - 从点数组中搜索 GeoPoints

android - 是否可以在 Xamarin 中创建几个屏幕,然后将它们导入到 iOS 和 Android 项目中?

android - 如何在 xamarin 移动应用程序中未调用 Looper.prepare() 的线程内创建处理程序

c# - 文件上传时 HttpPostedFileBase 始终为 null,模型绑定(bind)错误

android - Google Play 警告 : WebViewClient. onReceivedSslError 处理程序

android - BroadcastReceiver 中的 ApplicationContext

xaml - Xamarin - 错误 IDE1100 读取源文件内容时出错

c# - 你会在哪里使用 C# 运行时编译?