【问题标题】:Xamarin forms and Android BluetoothXamarin 表单和 Android 蓝牙
【发布时间】:2019-01-23 10:19:30
【问题描述】:

我正在使用 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 接口

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

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

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

【问题讨论】:

  • 这是用于经典蓝牙还是蓝牙低功耗 (4.X)?
  • 是的,它适用于经典蓝牙。我最终想实现一个 SPP 连接。
  • 该死,对不起,我帮不上什么忙。

标签: c# android xamarin xamarin.forms bluetooth


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    我知道现在有点晚了,但我目前面临着类似的问题。 我不知道它是否仍然相关,但看起来你忘记了一个“!”在 BluetoothDeviceReceiver 的 if 子句中。使用您的代码

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

    您检查您的列表是否包含该设备,如果包含,您要添加它。但由于列表一开始是空的,因此永远不会将任何设备添加到您的列表中。

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

    这应该可以解决它

    【讨论】:

      猜你喜欢
      • 2019-05-02
      • 1970-01-01
      • 2023-03-24
      • 2018-12-31
      • 1970-01-01
      • 2013-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多