dependency service BroadcastReceiver send data back

MarkusFMarkusF USMember ✭✭

hi,
what is the best way to send data back from the dependency service Broadcastreceiver (android) within the OnReceive method?
i used this project https://github.com/angelcalvasp/Blacksun.XamForms to build an dependency service for scanning BT devices. however, this project is not finished yet.

_bluetoothClient = DependencyService.Get();

public interface IBluetoothScanner
{
void StartDiscovery();

    void EndDiscovery();
}

public async Task SearchDevices()
{
_bluetoothClient.StartDiscovery();
await Task.Delay(TimeSpan.FromMilliseconds(10000));
_bluetoothClient.EndDiscovery();
}

public void StartDiscovery()
{
btAdapter.StartDiscovery();
}

    public void EndDiscovery()
    {
        btAdapter.CancelDiscovery();
    }

     broadcastReceiver = new CustomBroadcastReceiver();
            CurrentActivity = (Activity)Forms.Context;
            IntentFilter filter = new IntentFilter();
            filter.AddAction(BluetoothDevice.ActionFound);
            filter.AddAction(BluetoothAdapter.ActionDiscoveryStarted);
            filter.AddAction(BluetoothAdapter.ActionDiscoveryFinished);
            CurrentActivity.RegisterReceiver(broadcastReceiver, filter);

public class CustomBroadcastReceiver : BroadcastReceiver
{
public event EventHandler DeviceDiscoveryStarted;

    public event EventHandler DeviceDiscoverEnded;

    public event EventHandler<DeviceFoundEventArgs> DeviceDiscovered;

    public CustomBroadcastReceiver()
    {

    }

    public override void OnReceive(Context context, Intent intent)
    {

        String action = intent.Action;

        if (BluetoothAdapter.ActionDiscoveryStarted.Equals(action))
        {
            DoDeviceDiscoveryStarted();
        }
        else if (BluetoothAdapter.ActionDiscoveryFinished.Equals(action))
        {
            DoDeviceDiscoverEnded();
        }
        else if (BluetoothDevice.ActionFound.Equals(action))
        {

        //SEND DATA BACK TO FORMS
            System.Diagnostics.Debug.WriteLine("Found a device");
            BluetoothDevice device = (BluetoothDevice)intent.GetParcelableExtra(BluetoothDevice.ExtraDevice);
            //DoOnDeviceDiscovered(device.GetPairableBluetoothDevice());

        }
    }
}

thanks

Best Answers

Answers

Sign In or Register to comment.