Переменная Xamarin для Android-доступа в BroadcastReceiver - PullRequest
0 голосов
/ 15 декабря 2018

У меня есть broadcastReceiver, который проверяет входящее намерение.Входящий intent содержит BluetoothDeviceBroadcastReceiver проверяю погоду это правильно BluetoothDevice или нет.Если да, я хочу сохранить имя и MAC-адрес устройства в переменной.

Моя проблема: я могу получить доступ только к variable, который объявлен как const в моем BroadcastReceiver.Как я могу получить доступ к переменной внутри 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"

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...