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
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值