Jump to content
Sign in to follow this  
Rollo62

[Fmx, iOS, Bluetooth] Background modes bluetooth-central, bluetooth-peripheral : OnDiscoverLEDevice returns empty DeviceName string

Recommended Posts

Hi all,

 

as described here, I found a solution to the crash problem when using bluetooth background modes.
Unfortunately this puts new issues on the table.

Short recap:

I use these background modes

Quote

bluetooth-central
bluetooth-peripheral

( of coaarse all under latest 10.3.2 and current XCode/iOS versions).

 

Actually I would need only bluetooth-central, since I want to receive data from a connected device.
Once the connection is lost, I don't need to re-connect.
Which works, but only for a short time (2- 10 min), then iOS seems to kill this app, even if peramently (1x per sec.) data is received.

So I searched for solutions and found some in the web to add bluetooth-peripheral, so this mode will allow permanent app connection and streaming data,
and even discovering devices and re-connect (untested).

To get this running, I need to add these permission keys

Quote

<key>NSBluetoothPeripheralUsageDescription</key>
<string>The app needs bluetooth access to be able to process data from an IoT device in the background</string>
<key>NSBluetoothAlwaysUsageDescription</key>
<string>The app needs bluetooth access to be able to process data from an IoT device in the background</string>


But the purpose of this bluetooth-peripheral background mode is, that the Phone will act as a peripheral, which I don't really need here.
The drawback of this is, that I receive a new privacy permission request message, strangely when I disconnect a previous connected device, not earlier.

 

(2.) Issue: unintended permission request call

This message seemed to be related to the  BluetoothLE.ClearServices; call, which I was calling to cleanup.
This call seems to be used only when using as peripheral, not cleaning the services in central mode.
Anyway, removing this call disabled the permission request.

So far ,so good, 2 issues fixed.


(3) issue: after disconnection, the device cannot be re-discovered again in the first attempt

When I disconnect an IoT device (shut off), andwait until app has completely disconnected, and switch it on again.
Strangely the device is not found any longer.
It can be discovered again, when I does this power cycle twice (shut off the IoT device and power up again), then its discovered as usual.

Of coarse I search for missing variables in my code, but I found the reasonf for this behaviour is the following:

  • 1. time power up the IoT device
  • 1. time connection: OnDiscoverLEDevice is called and the ADevice.DeviceName contains the name of the IoT device, as it should
  • All OK, the IoT device is connected
  • 1. time power off the IoT device, until fully disconnected in the app
  • 2. time power up the IoT device
  • 2. time connection: OnDiscoverLEDevice is called and the ADevice.DeviceName IS EMPTY STRING "", this causes the problem here
  • 2. time power off the IoT device
  • 3. time power up the IoT device
  • 3. time connection: OnDiscoverLEDevice is called and the ADevice.DeviceName contains the name of the IoT device, al OK again, as in 1. start

 

So the device is only able to re-connect when its two times powered up.

I already tried to reset the BluetoothDeviceLE, the BluetoothAdapter, the BluetoothManager somehow, but without success.

Also waiting a long time (30 min) before power up doesn't reset.
When I connect another IoT device instead, it works, so it looks as if only the last device cannot simply be reconnected.
 

Question:

I'm still searching howto solve issue (3), to at re-connect every power up, maybe you have some clues howto get this running ?
 

Some other open questions:
How can I get rid of the bluetooth-peripheral mode at all ?
- To be able to use bluetooth-central only, but with ability to receive data from a connected device in background mode.
- To be able to use bluetooth-central only, but with the ability to discover and connect devices in background mode.

 

 

Edited by Rollo62

Share this post


Link to post

I've tried the follwing to reset the internal devices data

FBluetoothLE1.CurrentManager.AllDiscoveredDevices.Clear;
FBluetoothLE1.CurrentManager.LastDiscoveredDevices.Clear;
FBluetoothLE1.CurrentManager.ForceRefreshCachedDevices := True;

None of this works to solve issue (3).
It looks as if this is something deep inside the iOS framework, which probably could be reset there somehow with some magic method.
I'm further testing and searching solutions, but I hope for some help from the community.

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×