App not responding after bluetooth connection

Hi, I'm trying to build an app to control my old rc car which now uses Arduino instead of old analog circuit
car uses arduino nano + hc-05 (bluetooth) + l298n I tested it with "analog stick (with x/y sent to arduino)" and was working as expected
now I want to controll it with phone tilt(x steering) + touch(p forward/reverse power)
I wasnt sure which one to use accelerometer or oriention sensor.... I went with oriention
I'm using labels to see tilt/touch drag results(for develop purpose)
I'm really bad at this and there is high chance I did several things really wrong
as soon as bt connects app wont show results on labels and will stop responding

I tested on several phones:
xperia arc s: android 4
xperia mini pro: android 4
honor 8x:android 10
(tablet)tab s6 lite: android 12
note 20 ultra: android 13

here is blocks:







Two problems:

  • sending before connected
  • sending faster than BT can handle

For the first, add an IF/THEN test to guard each send attempt, like you do for your connection labels.

For the second, make three or more global variables to hold what you want to send. Update those global variables in the sensor event as often as you want, but only send them in the Clock Timer event, in the connected section.

1 Like

Dear @saeed_badali
may I add some other comments:
in your Screen1Initialize block you try to connect the BT before having granted it the permission and in the PermissionGranted event you ask again for the permission.
Maybe I'm missing something ?
image
I guess that you shall remove the Connection block from the Initialization, and move it in the Permission granted in place of the second "asking":
Someting like:

Then, in the "if then else" sequence of Orientation Changed block, there is an else that (if I have correctly understood) is never reached:
image
In my understanding the 1st "if" checks for a value outside the limits -30 and + 30
then if the value is outside, you check wheter it is greater than 30: if yes you send something,
if not you check if less than - 30; and if less than -30 you send something.
If not you send 140. But this last case should never happen since the first "if" checks for outside the limits -30 and + 30.
A the end if the value is, on the contrary, within the limits (i.e. between - 30 and + 30) you send 140.
Then to make it simpler you should consider something like:


This does not solve your BT problem, but it helps in doing things easier.

Another hint is to verify whether the app is really sending out data on the BT.
To this purpose I suggest you to try with a terminal program like TeraTerm on your PC. This very simple tool (free) allows you to find the BT sender source (your phone running the app) and to show whatever arrives to the PC on that line (the BT are typically seen in the Serial Lines list, as one of the COM's).
So, to make one step at a time, reduce your app to a very few rows, i.e. just open the BT client, connect it and send a "Hello World" to Tera Term. When you are done with it, then you can enrich your app by sending the effective data you want.

Best wishes !

PS last but not least : please be aware that for newer Android, in order to allow the BT transmission, it is necessary to set at Android level, the permissions to your app and for some mysterious reason, also the geo-localization. Give it a look, there is plenty of other topics on this matter in the Community.

1 Like

This

should be BLUETOOTH_SCAN
see also Runtime error when using (Lỗi runtime khi sử dụng) BluetoothClient - #4 by Anke

Taifun

1 Like

thanks I will try to do what u said

Actually I don't want to scan.... hard coded address will do and I'm ok with it as long as it works and keeps things simple... it is fine, right?

thanks for explanation and pictures ....will help a lot .... you are right about that else is not working

You can read more about rhe permissions here Bluetooth permissions  |  Connectivity  |  Android Developers

Asking for connect permission only once is sufficient rather than twice....

Taifun

1 Like

I changed my block app doesn't go to not responding status anymore
problem is it connects sometimes and sometimes just gives 908 error for fine location right after I open app and sometimes 507
I removed rest of the blocks and now I'm just trying to keep connection and send data over bt and it was successful (when it connects) I used teraterm as mentioned suggestion to check if I'm actually sending data and I'm sending it but not expected values I tried decimal / number but I receive different things on TeraTerm for example for 250 value I get 4 of this character= ْ or for 10 it goes next line...
I just want to send a value 1-255 as 1byte
I tried on two device: android10 and 12
also I made list just in case if I need to add any other permissions I want to make this app work with older androids as well but don't know how developer.android says:
<For your legacy Bluetooth-related permission declarations, set android:maxSdkVersion to 30 . This app compatibility step helps the system grant your app only the Bluetooth permissions that it needs when installed on devices that run Android 12 or higher>
not sure how I can do this on mit app inventor (is it even possible?)


ooooh, this happened to my teacher! the problem for us was that there were too many different people using bluetooth, and somehow that made the app crash. maybe try a different location?

I don't think its relevant ....

should I ask this as new question?

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.