繁体   English   中英

Xamarin 表单和 Android 蓝牙

[英]Xamarin forms and Android 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 接口

在 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:您可以假设适配器已启用并可以使用。

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

我知道现在有点晚了,但我目前面临着类似的问题。 我不知道它是否仍然相关,但您似乎忘记了“!” 在 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));

}

这应该解决它

暂无
暂无

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

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