سؤال

عند إنشاء تطبيق Bluetooth الخاص بي، أحتاج إلى الوصول إلى بعض إجراءات البث على جانب Android من الكود الخاص بي.

يتم تشغيل كل هذا في Core الخاص بي، لذلك لدي ViewModel الذي سيتم الاتصال به من خلال الواجهة الخاصة بي

public interface IConnectionService
{
    //Properties
    string IntentName { get; }

    //Events
    event EventHandler<SearchConnectionItemEventArgs> SearchItemFoundEvent;

    //Methods
    void RunIntent();
    void SearchConnection();
    void Connect(string macAddress);
}

يطالب RunIntent المستخدم بتشغيل Bluetooth (قد تكون تقنية أخرى) وأرغب بعد ذلك في تشغيل حدث عند تغيير حالة Bluetooth

Android.Bluetooth.BluetoothAdapter.ActionStateChanged

وأيضاً عندما أبحث عن أجهزة جديدة أحتاجها

Android.Bluetooth.BluetoothDevice.ActionFound

لكن لا يمكنني وضع [Android.Runtime.Register("ACTION_FOUND")] و[Android.Runtime.Register("ACTION_STATE_CHANGED")] في صفي، فهذا لا يعمل إلا إذا حاولت وضعه في طريقة العرض الخاصة بي، ولكن إذا هل أحتاج إلى المنطق في وجهة نظري؟

هل من الممكن وضعه في القلب بطريقة أو بأخرى؟

صفي ينفذ واجهتي

using System;
using Android.Bluetooth;
using Android.Content;
using Cirrious.MvvmCross.Android.Platform.Tasks;
using Test.Core.Interfaces;
using Test.Core.Models;

namespace Test.Core.Android
{
    public class AndroidBluetooth : MvxAndroidTask, IConnectionService
    {
    #region Private Fields

    private readonly BluetoothAdapter _bluetoothAdapter;

    #endregion
    #region Public Fields

    #endregion
    #region Properties

    public string IntentName { get { return "Turn on Bluetooth"; }}

    #endregion
    #region Constructor

    public AndroidBluetooth()
    {
        _bluetoothAdapter = BluetoothAdapter.DefaultAdapter;
    }

    #endregion
    #region Events

    public event EventHandler<SearchConnectionItemEventArgs> SearchItemFoundEvent;
    private void ItemFound(SearchConnectionItemEventArgs e)
    {
        if(SearchItemFoundEvent != null)
        {
            SearchItemFoundEvent(this, e);
        }
    }

    #endregion

    #region Methods
    public void RunIntent()
    {
        if (_bluetoothAdapter == null)
        {
            //No bluetooth support on phone
        }
        else if(!_bluetoothAdapter.IsEnabled)
        {
            var intent = new Intent(BluetoothAdapter.ActionRequestEnable);
            StartActivity(intent);
        }
    }

    public void SearchConnection()
    {

        if (_bluetoothAdapter == null)
        {
            //No bluetooth support on phone
        }
        else if (!_bluetoothAdapter.IsEnabled)
        {
            //Bluetooth not turned on
            RunIntent();
        }
        else
        {
            FindBondedDevices();
        }
    }

    private void FindBondedDevices()
    {
        var pairedDevices = _bluetoothAdapter.BondedDevices;

        if (pairedDevices.Count <= 0) return;

        foreach (var item in pairedDevices)
        {
            ItemFound(new SearchConnectionItemEventArgs {Name = item.Name, MacAddress = item.Address});
        }
    }

    private void FindNewDevices()
    {
        if (_bluetoothAdapter == null)
        {

        }
        else if (!_bluetoothAdapter.IsEnabled)
        {

        }
        else
        {
            _bluetoothAdapter.StartDiscovery();
            //Bind event for new devices
        }
    }

    public void Connect(string macAddress)
    {

    }
    #endregion
}
}
هل كانت مفيدة؟

المحلول

تنصل:لست على دراية بهذا الجزء من Android - ولم أستخدم حزمة Bluetooth مطلقًا.

من الوصف الخاص بك، يبدو أنك تعرف الإجابة بالفعل - يجب أن تنتقل هذه السمات إلى الأساليب الموجودة داخل النشاط/العرض.

بالطبع، بمجرد إضافتها إلى النشاط/العرض، فمن السهل توجيه استدعاءات الطريقة هذه إلى ViewModel - فقط استخدم ViewModel الملكية داخل العرض.


ربما يكون من الأسهل محاولة الحصول على هذا الجزء من عملك كعينة مستقلة أولاً - ثم العمل على كيفية جعله مشتركًا بين الأنظمة الأساسية و/أو mvvm.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top