gpt4 book ai didi

c# - Xamarin 表单和 Android 蓝牙

转载 作者:太空宇宙 更新时间:2023-11-03 14:50:49 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com