crazydev
crazydev

Reputation: 575

Get Bluetooth BLE status in background service in Android

I have the following method in Bluetooth Gatt callback which responds to any bluetooth connectivity change:

public override void OnConnectionStateChange(BluetoothGatt gatt, [GeneratedEnum] GattStatus status, [GeneratedEnum] ProfileState newState)
{
    base.OnConnectionStateChange(gatt, status, newState);

    if(newState == ProfileState.Connected)
    {
        gatt.DiscoverServices();
    }

    else if(newState == ProfileState.Disconnected)
    {
        gatt.Close();
        Log.Info("BLE", "Status: Disconnected");
    }

}

In my background service, I want to listen for any connectivity for disconnect when a BLE device is connected or disconnected.

My service:

 [return: GeneratedEnum]
        public override StartCommandResult OnStartCommand(Intent intent, [GeneratedEnum] StartCommandFlags flags, int startId)
        {
            try
            {
                Toast.MakeText(this, "Background service started", ToastLength.Long);

// I want to listen for any connectivity changes here

            base.OnStartCommand(intent, flags, startId);
            return StartCommandResult.Sticky;

        }

Can anyone please help me on how to achieve this ?

Upvotes: 0

Views: 949

Answers (1)

Junior Jiang
Junior Jiang

Reputation: 12723

First , can create a BroadcastReceiver to listen the status of bluetooth,like this:

public class BluetoothStateBroadcastReceive : BroadcastReceiver
{
    public override void OnReceive(Context context, Intent intent)
    {
        Toast.MakeText(context, "Received intent!", ToastLength.Short).Show();
        string action = intent.Action;
        BluetoothDevice device = (BluetoothDevice)intent.GetParcelableExtra(BluetoothDevice.ExtraDevice);
        switch (action)
        {
            case BluetoothDevice.ActionAclConnected:
                Toast.MakeText(context, "BluetoothDevice:" + device.Name + "Connected", ToastLength.Long).Show();
                break;
            case BluetoothDevice.ActionAclDisconnected:
                Toast.MakeText(context, "BluetoothDevice:" + device.Name + "Disconnected", ToastLength.Short).Show();
                break;
            case BluetoothAdapter.ActionStateChanged:
                Toast.MakeText(context, "BluetoothDevice:" + device.Name + "Changed", ToastLength.Short).Show();
                //int blueState = intent.GetIntExtra(BluetoothAdapter.ExtraState, 0);
                //switch (blueState)
                //{
                //    case BluetoothAdapter.STATE_OFF:
                //        Toast.MakeText(context, "off", ToastLength.Short).Show();
                //        break;
                //    case BluetoothAdapter.STATE_ON:
                //        Toast.MakeText(context, "on", ToastLength.Short).Show();
                //        break;
                //}
                break;
        }
    }
}

And in service , you can register it:

public class BluetoothService : Service
{
    private BluetoothStateBroadcastReceive mReceive;

    public BluetoothService()
    {
    }

    public BluetoothService(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
    {
    }

    [return: GeneratedEnum]
    public override StartCommandResult OnStartCommand(Intent intent, [GeneratedEnum] StartCommandFlags flags, int startId)
    {
        registerBluetoothReceiver();
        return base.OnStartCommand(intent, flags, startId);
    }

    public override IBinder OnBind(Intent intent)
    {
        throw new NotImplementedException();
    }

    private void registerBluetoothReceiver()
    {
        if (mReceive == null)
        {
            mReceive = new BluetoothStateBroadcastReceive();
        }
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.AddAction(BluetoothAdapter.ActionStateChanged);
        intentFilter.AddAction(BluetoothDevice.ActionAclConnected);
        intentFilter.AddAction(BluetoothDevice.ActionAclDisconnected);
        //intentFilter.AddAction("android.bluetooth.BluetoothAdapter.STATE_OFF");
        //intentFilter.AddAction("android.bluetooth.BluetoothAdapter.STATE_ON");
        RegisterReceiver(mReceive, intentFilter);
    }

    public override void OnDestroy()
    {
        unregisterBluetoothReceiver();
        base.OnDestroy();
    }

    private void unregisterBluetoothReceiver()
    {
        if (mReceive != null)
        {
            UnregisterReceiver(mReceive);
            mReceive = null;
        }
    }
}

Then you can try this in where you want.When using service, you can refer to this document.

Upvotes: 2

Related Questions