我已经创建了下面的代码,它是我的 bluetoothDeviceReciver 类,它是在 Xamarin 表单 Android 项目中创建的。我正在尝试广播设备 ID 或一些常见的东西,以便我知道使用同一应用程序的设备数量。
类蓝牙设备接收器:广播接收器 {
public override void OnReceive(Context context, Intent intent)
{
var action = intent.Action;
if (action != BluetoothDevice.ActionFound)
{
return;
}
var device = (BluetoothDevice)intent.GetParcelableExtra(BluetoothDevice.ExtraDevice);
if (device.BondState != Bond.Bonded)
{
var name = device.Name;
var address = device.Address;
}
}
}
和在主要活动 OnCreate()
protected override void OnCreate(Bundle savedInstanceState)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(savedInstanceState);
_receiver = new BluetoothDeviceReceiver();
RegisterReceiver(_receiver, new IntentFilter(BluetoothDevice.ActionFound));
BluetoothAdapter.DefaultAdapter.StartDiscovery();
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
Xamarin.Forms.Forms.SetFlags("RadioButton_Experimental");
RequestPermissions(permissionArr, RequestId);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
LoadApplication(new App());
}
这个 OnRecieve() 没有被调用,我不明白为什么?获得所有设备地址后,我必须将其存储在 PCL 项目中的文件中,因此我还需要 PCL 项目中的设备地址。