c# - BroadcastReceiver 中的 Xamarin Android 访问变量
问题描述
我有一个broadcastReceiver
检查传入意图的方法。传入intent
包含一个BluetoothDevice
. 在BroadcastReceiver
我检查天气中,它是否正确BluetoothDevice
。如果是,我想将设备的名称和 MAC 地址存储在一个变量中。
我的问题:我只能访问在我的 BroadcastReceivervariable
中声明的一个。const
如何访问内部的变量BroadcastReceiver
?
namespace firstTry3
{
[Activity(Label = "@string/app_name")]
public class bluetoothConnectionActivity : AppCompatActivity
{
BluetoothAdapter mBluetoothAdapter;
Button buttonBluetoothOn;
Button buttonConnect;
Button buttonDissconnect;
const string deviceName = "HC-05"; //must be changed when bluetooth Device gets new name
string deviceMAC = "";
const string tag = "blueApp";
btdeviceFoundBroadcastReceiver mBtBroadcastReciever;
...
[BroadcastReceiver(Enabled = true, Exported = true)]
public class btdeviceFoundBroadcastReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
// Do stuff here.
string action = intent.Action;
if (BluetoothDevice.ActionFound.Equals(action) && deviceMAC == "")
{
BluetoothDevice device = (BluetoothDevice)intent.GetParcelableExtra(BluetoothDevice.ExtraDevice);
string dName = device.Name;
if (dName == deviceName)
{
deviceMAC = device.Address;
Log.Info(tag, MethodBase.GetCurrentMethod().Name + ": bluetooth module found name: " + deviceName + " MAC: " + deviceMAC);
}
}
}
}
我无法访问 deviceMAC,出现以下错误:
“非静态字段、方法或属性 bluetoothConnectionActivity.deviceMethod 需要对象引用”
解决方案
推荐阅读
- c# - 比较 2 个大型 DATA 表(70k 记录)
- c# - 如何将具有某些 xml 类型列的行批量插入数据库中?
- javascript - 从 chrome.storage.local 中检索对象并使用它来更改当前页面的 CSS
- java - 子字符串大文本的更好方法是什么?
- jquery - 添加带有字段名称的 ParsleyJS 自定义消息
- tableau-api - 在 Tableau 中为筛选的分组数据创建计算字段
- javascript - 由于引用错误,未找到函数名称
- scala - 与 Scala 类型不匹配
- sql - 如何从单列中获取值的组合?
- javascript - 如何在rails link_to中的form-data中传递查询参数而不是将其附加到url中?