Using BLE async Scans in a Background service ?

214 views
Skip to first unread message

Sana Rajput

unread,
Sep 14, 2018, 9:18:55 AM9/14/18
to PlayN

Hi,

I am working on a Bluetooth Low Energy scanner for Android, which has to scan every X minutes all nearly devices, getting all services and characteristics, and sending them to a HTTP server. This task has to be executed in background, so I figured I should use Services, but I dont know if that can work correctly with BLE scans.

BLE scans mainly use async methods and callbacks, is it possible to set all this process in a task or something I can control with a background service ?

By the way, I found many things about background services, and I don't get what I should use ... Can someone take a few minutes to explain me ? :dizzy:

Thanks !

akshita...@gmail.com

unread,
Sep 18, 2018, 11:35:40 AM9/18/18
to PlayN
Hi Sana,

Do you want to scan Bluetooth in Background service?
You could try to use Bluetooth LE plugin for Xamarin.
It requires ACCESS_COARSE_LOCATION ACCESS_FINE_LOCATION BLUETOOTH BLUETOOTH_ADMIN permissions.

    List<IDevice> deviceList;

    [return: GeneratedEnum]
    public override StartCommandResult OnStartCommand(Intent intent, [GeneratedEnum] StartCommandFlags flags, int startId)
    {

        var ble = CrossBluetoothLE.Current;
        var adapter = CrossBluetoothLE.Current.Adapter;
        deviceList = new List<IDevice>();
        adapter.DeviceDiscovered += (s, a) => {
            deviceList.Add(a.Device);

        } ;

        Task task = new Task(() => adapter.StartScanningForDevicesAsync());
        task.Start();

        return StartCommandResult.NotSticky;
    }
If you still need help regarding Xamarin Development feel free to contact me.

Andrei Kleshchanka

unread,
Sep 18, 2018, 7:32:56 PM9/18/18
to PlayN
Reply all
Reply to author
Forward
0 new messages