我无法在Android中获得蓝牙设备

时间:2017-07-29 19:35:04

标签: c# android xamarin

我正在使用xamarin android我希望得到所有附近的蓝牙设备我试过这个但它给我一个错误说:对象引用没有设置为对象的实例。

          IntentFilter filter = new IntentFilter(BluetoothDevice.ActionFound);
          BluetoothDeviceReceiver mReceiver = new BluetoothDeviceReceiver();
          RegisterReceiver(mReceiver, filter);
          mBluetoothAdapter.StartDiscovery();

    class BluetoothDeviceReceiver : BroadcastReceiver
    {
    public override void OnReceive(Context context, Intent intent)
    {
        String action = intent.Action;
        MainActivity ma = new MainActivity();            
        if (action == BluetoothDevice.ActionFound)
        {
            // Get device
            BluetoothDevice newDevice = (BluetoothDevice)intent.GetParcelableExtra(BluetoothDevice.ExtraDevice);
            // now you could do your job with newDevice
            ma.mArrayAdapter.Add(newDevice.Name + " -> " + newDevice.Address);
            ma.lsv1.Adapter = ma.mArrayAdapter;

        }

        else
        {

        }
    }
}

错误:

MainActivity ma = new MainActivity();            
ma.mArrayAdapter.Add(newDevice.Name + " -> " + newDevice.Address);
ma.lsv1.Adapter = ma.mArrayAdapter;

0 个答案:

没有答案