xamarin.android蓝牙,c# - Xamarin forms and Android Bluetooth - Stack Overflow

I am developing a cross platform app using Xamarin forms.

I have an ObserveableCollection and I want to populate it with bluetooth devices that have been found during a search.

The search is/has to be platform specific and is performed through a DependencyService. I then want to display this ObserveableCollection in a Xamarin.Forms ListView, so the user can see all the found bluetooth devices. In this case, I'm interested in the Android implementation.

I have a basic goal: Discover Bluetooth devices and display them in a Xamarin forms ListView.

Here is what I have:

In 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().BTDeviceScan(); //populate the Bluetooth device ListView with the found devices

}

The code above means that when BT_Switch is flipped, the app should start scanning for Bluetooth Devices and populate the Bluetooth_Device_ListView with the result.

In 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 BTDeviceScan(); // Scans for available bluetooth devices

}

}

This serves as the DependencyService Interface to the Android specific code

In 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 BluetoothDeviceList;

public void InitializeBluetooth() // Initialises bluetooth adapter settings

{

adapter = BluetoothAdapter.DefaultAdapter;

BluetoothDeviceList = new ObservableCollection();

}

...

public ObservableCollection BTDeviceScan() // Scans for available bluetooth devices

{

adapter.StartDiscovery();

return BluetoothDeviceList;

}

}

}

From 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));

}

}

}

}

This code seems (mostly) correct to me, but the list view is not being populated with anything. Ideally I'd like it to be populated in real time, as each device is found. Can anyone offer help or tell me what I need to change? Thanks.

PS: you can assume the adapter is enabled and ready to use.

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
©️2021 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值