phyphox / phyphox-android

Physical Phone Experiments
GNU General Public License v3.0
303 stars 39 forks source link

Cant connect to BLE Peripheral if experiment is loaded via QR Code #35

Open vChavezB opened 1 year ago

vChavezB commented 1 year ago

As I mentioned in this post

https://phyphox.org/forums/showthread.php?tid=1634

I wasnt able to connect a BLE peripheral if the experiment was loaded via QR code (generated with the phyphox editor).

However, if the BLE peripheral includes the custom BLE Phyphox UUIDs (service + experiment char) the app downloads the experiment and then connects without a problem.

How to reproduce

Create an experiment with phyphox editor that includes a BLE characteristic as input. Load the experiment via QR code in the app and then wait for the app to connect to the BLE peripheral that includes the characteristic described (which wont). However if you embed the xml in the char. descibed in the BLE wiki for phyphox it downloads the experiment and the connects without a problem.

As mentioned in the forum I am developing a BLE peripheral with Zephyr OS and another low resource mcu. As I wanted to reduce the load of adding an xml file in ram or flash I was trying to load the experiment via QR. Unfortunately I dont have a simple example so the problem cant be reproduced on your end. If you have either an NRF52 SoC or ESP32 I could build an example this week so you can test it on your end.

BR Victor

Staacks commented 1 year ago

This error would be thrown extremely early when phyphox tries to connect before phyphox even had the chance to look for something specific. This error is shown if phyphox was not even able to connect to the GATT server on your device. Hard to tell what the reason is and I am not sure if an example on an ESP32 will reproduce this error as I suspect that if the GATT server is working properly, it is an exotic (because we connect to plenty of devices with a QR code combination) combination of connection paramters that is either wrong or misinterpreted by phyphox.

Can you connect from nrF Connect? (If this app is not known to you, check it out - one of the most helpful tools when developing BLE stuff.)

If you can reproduce it on an ESP32, we should be able to fix it - we have plenty of those. Alternatively, as you are from the FH Aachen, it might be a good idea to meet and just try it directly. As @Dorsel89 mentioned in the forum, he has been working on a Zephyr-based peripheral for a while now, so you two might be able to learn a few things from each other and probably figure this issue out in no time.

vChavezB commented 1 year ago

I suspect that if the GATT server is working properly, it is an exotic (because we connect to plenty of devices with a QR code combination) combination of connection paramters that is either wrong or misinterpreted by phyphox.

Indeed, I think it is a strange error.

Just to clarify it, the BLE peripheral connects correctly to the phyphox app if you use the option "add experiment for bluetooth device". Then it downloads the xml from char (0x002) , disconnects, loads the experiment the UI and then connects again without a problem.

However, (the same BLE peripheral without changes) instead of using the "add experiment for bluetooth device" option I use the load from QR Code. The QR Code is generated by importing the phyphox experiment xml (same which was embedded to the MCU) to the web editor and then clicking the generate online QR code. The experiment is loaded again in the UI but this time when it tries to connect it cant.

Once Im back at the office I will use an ESP32 to reproduce the error as it seems this might be an edge case.

Staacks commented 1 year ago

The only difference that comes to mind is the scanning/advertising. If you transfer the xml via Bluetooth, you scan from the main menu which lists any device it can see and highlights the ones advertising the phyphox service. If you then connect, it does not scan again, but hand over the hardware address of the device to the experiment activity, which then connects to the device immediately. If you open it from the QR code, there is no specific device attached to it, but instead phyphox will scan after opening the experiment and filter according to the XML (advertised name and/or advertised services). But the only effect I can think of here is that you can create an XML that works in the first case but cannot find the device in the seconds as the filters do not match the advertised data. However, in that case you would not get an error message but the device would simply not show up during the scan.

Dorsel89 commented 1 year ago

Could compare the working xml with the one that is generated by the editor (in the "xml" tab)? Probably its just an issue with parsing/regenerating the xml by the editor

vChavezB commented 1 year ago

I tried the example Create Experiment for the ESP32 and it worked as expected.

I checked the xml by printing it in the function void PhyphoxBLE::when_subscription_received() by adding

for(size_t i=0;i<exp_len;i++)
{
    Serial.print(static_cast<char>(exp[i]));
}

The XML created by the Arduino library is

<phyphox version="1.10">
<title>Random Number Plotter</title>
<category>Arduino Experiments</category>
<description>Random numbers are generated on Arduino and visualized with phyphox afterwards</description>
<data-containers>
    <container size="0" static="false">CH0</container>
    <container size="0" static="false">CB1</container>
    <container size="0" static="false">CH1</container>
    <container size="0" static="false">CH2</container>
    <container size="0" static="false">CH3</container>
    <container size="0" static="false">CH4</container>
    <container size="0" static="false">CH5</container>
</data-containers>
<input>
    <bluetooth name="My Device" id="phyphoxBLE" mode="notification" rate="1" subscribeOnStart="false">
<output char="cddf1002-30f7-4671-8b43-5e40ba53514a" conversion="float32LittleEndian" offset="0" >CH1</output>
        <output char="cddf1002-30f7-4671-8b43-5e40ba53514a" conversion="float32LittleEndian" offset="4" >CH2</output>
        <output char="cddf1002-30f7-4671-8b43-5e40ba53514a" conversion="float32LittleEndian" offset="8" >CH3</output>
        <output char="cddf1002-30f7-4671-8b43-5e40ba53514a" conversion="float32LittleEndian" offset="12" >CH4</output>
        <output char="cddf1002-30f7-4671-8b43-5e40ba53514a" conversion="float32LittleEndian" offset="16" >CH5</output>
        <output char="cddf1002-30f7-4671-8b43-5e40ba53514a" extra="time">CH0</output>
    </bluetooth>
</input>
<output>
    <bluetooth id="phyphoxBLE" name="My Device">
        <input char="cddf1003-30f7-4671-8b43-5e40ba53514a" conversion="float32LittleEndian">CB1</input>
    </bluetooth>
</output>
<analysis sleep="0.1"  onUserInput="false"></analysis>
<views>
    <view label="FirstView">
        <graph label="Random number over time" labelX="time" labelY="random number" unitX="s" unitY="" xPrecision="1" yPrecision="1">
            <input axis="x">CH0</input>
            <input axis="y">CH1</input>
        </graph>
        <graph label="Random number squared over random number" labelX="random number" labelY="squared" unitX="" unitY="" style="dots" color="2E728E">
            <input axis="x">CH1</input>
            <input axis="y">CH2</input>
        </graph>
    </view>
    <view label="SecondView">
        <info label="In this view you can set a value between 1 and 10. The squared random value will be multiplied by this value and can be seen here." size="1.2">
        </info>
        <separator height="0.30" color="404040">
        </separator>
        <value label="Number" precision="2" unit="u" facor="1" color="FFFFFF" size="2">
            <input>CH3</input>
        </value>
        <edit label="Editfield" decimal="false" unit="u" max="10">
        <output>CB1</output>
        </edit>
    </view>
</views>
<export>
    <set name="mySet">
        <data name="myData1">CH1</data>
        <data name="myData2">CH2</data>
    </set>
</export>
</phyphox>

I noticed that the experiment does not include a service filter. I thought this was the error but after testing different settings I noticed that if my advertising name is "CARL_" (similar to what my BLE peripherals with the problem were named) then phyphox doesnt find the device.

Heres the xml that I generated based on the previous one with the only change to the name to "CARL_".

<phyphox xmlns="http://phyphox.org/xml" xmlns:editor="http://phyphox.org/editor/xml" version="1.7" editor:version="1.0" locale="">
    <title>Random Number Plotter</title>
    <category>Arduino Experiments</category>
    <description>Random numbers are generated on Arduino and visualized with phyphox afterwards</description>
    <data-containers>
        <container size="0" static="false">CH0</container>
        <container size="0" static="false">CB1</container>
        <container size="0" static="false">CH1</container>
        <container size="0" static="false">CH2</container>
        <container size="0" static="false">CH3</container>
        <container size="0" static="false">CH4</container>
        <container size="0" static="false">CH5</container>
    </data-containers>
    <input>
        <bluetooth editor:uuid="385" id="phyphoxBLE" name="CARL_"  mode="notification" rate="1" subscribeOnStart="false">
            <output char="cddf1002-30f7-4671-8b43-5e40ba53514a" conversion="float32LittleEndian" offset="0">CH1</output>
            <output char="cddf1002-30f7-4671-8b43-5e40ba53514a" conversion="float32LittleEndian" offset="4">CH2</output>
            <output char="cddf1002-30f7-4671-8b43-5e40ba53514a" conversion="float32LittleEndian" offset="8">CH3</output>
            <output char="cddf1002-30f7-4671-8b43-5e40ba53514a" conversion="float32LittleEndian" offset="12">CH4</output>
            <output char="cddf1002-30f7-4671-8b43-5e40ba53514a" conversion="float32LittleEndian" offset="16">CH5</output>
            <output char="cddf1002-30f7-4671-8b43-5e40ba53514a" conversion="string" extra="time">CH0</output>
        </bluetooth>
    </input>
    <output>
        <bluetooth editor:uuid="388" id="phyphoxBLE" name="CARL_" >
            <input char="cddf1003-30f7-4671-8b43-5e40ba53514a" conversion="float32LittleEndian">CB1</input>
        </bluetooth>
    </output>
    <analysis sleep="0.1"  onUserInput="false">
    </analysis>
    <views>
        <view label="FirstView">
            <graph editor:uuid="440" label="Random number over time" aspectRatio="2.5" style="lines" lineWidth="1" color="ff7e22" partialUpdate="false" history="1" labelX="time" labelY="random number" labelZ="" unitX="s" unitY="" unitZ="" logX="false" logY="false" logZ="false" xPrecision="1" yPrecision="1" zPrecision="3" scaleMinX="auto" scaleMaxX="auto" scaleMinY="auto" scaleMaxY="auto" scaleMinZ="auto" scaleMaxZ="auto" minX="0" maxX="0" minY="0" maxY="0" minZ="0" maxZ="0"  mapWidth="0"       >
                <input axis="x">CH0</input>
                <input axis="y">CH1</input>
            </graph>
            <graph editor:uuid="441" label="Random number squared over random number" aspectRatio="2.5" style="dots" lineWidth="1" color="2E728E" partialUpdate="false" history="1" labelX="random number" labelY="squared" labelZ="" unitX="" unitY="" unitZ="" logX="false" logY="false" logZ="false" xPrecision="3" yPrecision="3" zPrecision="3" scaleMinX="auto" scaleMaxX="auto" scaleMinY="auto" scaleMaxY="auto" scaleMinZ="auto" scaleMaxZ="auto" minX="0" maxX="0" minY="0" maxY="0" minZ="0" maxZ="0"  mapWidth="0"       >
                <input axis="x">CH1</input>
                <input axis="y">CH2</input>
            </graph>
        </view>
        <view label="SecondView">
            <info editor:uuid="442" label="In this view you can set a value between 1 and 10. The squared random value will be multiplied by this value and can be seen here." >
            </info>
            <separator editor:uuid="443" height="0.3" color="404040">
            </separator>
            <value editor:uuid="444" label="Number" size="2" precision="2" scientific="false" unit="u" factor="1" color="FFFFFF">
                <input>CH3</input>
            </value>
            <edit editor:uuid="445" label="Editfield" signed="true" decimal="false"  max="10" unit="u" factor="1" default="0">
                <output>CB1</output>
            </edit>
        </view>
    </views>
    <export>
        <set name="mySet">
            <data name="myData1">CH1</data>
            <data name="myData2">CH2</data>
        </set>
    </export>
</phyphox>

When I generate this experiment with QR code and change the name in the arduino sketch it doesnt work. Could it be the underscore is generating a problem?

PhyphoxBLE::start("CARL_");
Dorsel89 commented 1 year ago

Thats strange. I just uploaded the "Create Experiment" example with "Carl_" as server name and it works for me on iOS and Android (via QR-code from the editor and received via bluetooth).

You could test the following :

Cheers Dominik

vChavezB commented 1 year ago

Hi thanks for the feedback. I just tried the advertising name "Carl" and it worked. However, my issue is when the advertising name uses capital letters ... i.e., "CARL" and not "carl_" (via QR Code)

grafik

And yes I used the nrf connect app and the BLE Peripheral is displayed as it should "CARL_" and adv. service uuid is the one mentioned for the phyphox ble service.

Dorsel89 commented 1 year ago

hm, it still works for me with "CARL_" as server-name and your QR-code (iOS and Android).

Staacks commented 1 year ago

I don't see why capitalization should play a role. I just checked the code and it just compares the strings. It is case-sensitive, so advertising CARL does not match a Carl XML and vice versa, but if they match there should be no difference.

Just to be sure we are talking about the right thing here: You are still getting the error message from the forum thread or does it just not show up in the scan? Because a wrong device name should lead to the latter even before you can connect.

One other thought in this context: Could the device be paired to your phone, so it is treated as a paired device with one name but not with the other name? That could make a huge difference. In particular, the system might even offer the device during a scan while blocking it due to connecting in the background.

Besides all this: The offer still stands, that we can just meet in person to figure it out with the devices on the table. After all you are just around the corner.

vChavezB commented 1 year ago

You are still getting the error message from the forum thread or does it just not show up in the scan? Because a wrong device name should lead to the latter even before you can connect.

After loading the QR Code I cannot connect to the device as mentioned in the forum.

I checked with the phone of a colleague and it works. So I think the problem is on my phone, also the BLE Peripheral is not paired.

Thanks for the offer, at the moment this issue is low priority as I already implemented the phyphox service embedded on the device and the issue is just on my phone.

Staacks commented 1 year ago

Still, would be good to know what causes this. So, please let us know if you figure out more. Also: Are both Android (as this issue is in the Andoid section)? Because as phyphox is implemented separately on both platforms, we cannot entirely rule out minor differences. And finally, if you tell us the problematic device or its Android version, we could test a bit more.

vChavezB commented 1 year ago

Also: Are both Android (as this issue is in the Andoid section)?

Yes both phones are Android .

if you tell us the problematic device or its Android version,

Its version Android 12