我如何从EventHandler到Xamarin.Android中的后台服务获取值

时间:2019-01-09 11:01:21

标签: c# xamarin.android

我正在使用以下代码连接到BLE设备。我的问题是,当设备连接或断开连接时,我希望我的服务监听GattCallBack类中发生的任何更改,并将其显示为本地通知。

service

如何在下面的 [Service] public class DemoService : Service { public override StartCommandResult OnStartCommand (Android.Content.Intent intent, StartCommandFlags flags, int startId) { var t = new Thread (() => { Log.Debug ("DemoService", "Doing work"); StopSelf (); } ); t.Start (); return StartCommandResult.Sticky; } 中侦听任何连接更改?

Java

1 个答案:

答案 0 :(得分:3)

您可以在服务中声明一个蓝牙连接方法,在“活动”中实例化该服务并调用该连接方法,然后在该服务中监视设备,然后在“活动”中注册广播以侦听由其发出的连接状态服务

在服务中:

public bool Initialize() 
    {
        // For API level 18 and above, get a reference to BluetoothAdapter through
        // BluetoothManager.
        if (mBluetoothManager == null) {
            mBluetoothManager = (BluetoothManager) GetSystemService (Context.BluetoothService);
            if (mBluetoothManager == null) {
                return false;
            }
        }

        mBluetoothAdapter = mBluetoothManager.Adapter;
        if (mBluetoothAdapter == null) {
            return false;
        }

        return true;
    }

public bool Connect (String address)
    {
        if (mBluetoothAdapter == null || address == null) {
            return false;
        }

        if (mBluetoothDeviceAddress != null && address == mBluetoothDeviceAddress && mBluetoothGatt != null) {
            if (mBluetoothGatt.Connect ()) {
                mConnectionState = State.Connecting;
                return true;
            } else {
                return false;
            }
        }

        BluetoothDevice device = mBluetoothAdapter.GetRemoteDevice (address);
        if (device == null) {
            Log.Warn (TAG, "Device not found.  Unable to connect.");
            return false;
        }
        mBluetoothGatt = device.ConnectGatt (this, false, new BGattCallback (this));
        mBluetoothDeviceAddress = address;
        mConnectionState = State.Connecting;
    }

class BGattCallback : BluetoothGattCallback
{
    YourService service;

    public  BGattCallback (YourService s)
    {
        service = s;
    }

    public override void OnConnectionStateChange (BluetoothGatt gatt, GattStatus status, ProfileState newState)
    {
        String intentAction;
        if (newState == ProfileState.Connected) {
            intentAction = BluetoothLeService.ACTION_GATT_CONNECTED;
            Intent intent = new Intent (intentAction);
            service.SendBroadcast (intent);
        } else if (newState == ProfileState.Disconnected) {
            intentAction = BluetoothLeService.ACTION_GATT_DISCONNECTED;
            Intent intent = new Intent (intentAction);
            service.SendBroadcast (intent);
        }
    }

在活动中:

protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);
        SetContentView (Resource.Layout.gatt_services_characteristics);
        mServiceManager = new ServiceManager (this);
        Intent gattServiceIntent = new Intent(this, typeof (YourService));
        BindService (gattServiceIntent, mServiceManager, Bind.AutoCreate);
   }

protected override void OnResume ()
    {
        base.OnResume ();

        //RegisterReceiver
    }

protected override void OnPause ()
    {
        base.OnPause ();
        //UnregisterReceiver;
    }

protected override void OnDestroy ()
    {
        base.OnDestroy ();
        //UnbindService;

    }
class ServiceManager : BroadcastReceiver
{   
    Activity _activity;

    public ServiceManager (Activity dca)
    {
        _activity= dca;
    }

    public override void OnReceive (Context context, Intent intent)
    {
        String action = intent.Action;

        if (BluetoothLeService.ACTION_GATT_CONNECTED == action) {
            //do Something
        } else if (BluetoothLeService.ACTION_GATT_DISCONNECTED == action) {
            //do Something
        }
    }
}

这是总的方向,更准确,更具体的内容可以参考:https://github.com/xamarin/monodroid-samples/tree/master/BluetoothLeGatt