簡體   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