我正在用 Xamarin 实现一个 APP。
我想执行蓝牙扫描。并找到设备。
这是我的代码。
一旦触发“Button_Scan()”,我该如何实现它来开始扫描并收集结果?
谢谢你。
[BroadcastReceiver(Enabled = true)]
public class BluetoothReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
var action = intent.Action;
if (action != BluetoothDevice.ActionFound)
{
return;
}
if (BluetoothDevice.ActionFound.Equals(action))
{
var device = (BluetoothDevice)intent.GetParcelableExtra(BluetoothDevice.ExtraDevice);
var ConnectState = device.BondState;
/*
switch (ConnectState)
{
case Bond.None:
break;
case Bond.Bonded:
break;
case Bond.Bonding:
break;
}
*/
if (device.Name != null)
{
Xamarin.Forms.Application.Current.MainPage.DisplayAlert("Found device", device.Name, "ok");
}
}
}
}
public async void Button_Scan(object sender, EventArgs e)
{
try
{
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.DefaultAdapter;
mBluetoothAdapter.Enable();
mBluetoothAdapter.StartDiscovery();
}
catch(Exception ex)
{
DisplayAlert("ex", ex.ToString(), "ok");
}
}