Creating a manual timeout for bluetooth connection in MATLAB App Designer

10 visualizzazioni (ultimi 30 giorni)
I have created a wireless signal analyzing application using MATLAB App Designer, a HC-06 Bluetooth module, and an Arduino. The application is basically complete and I am now coding in as much error detection as possible so the app does not crash if used incorrectly.
Given the device is connected using bluetooth, there are certain instances where the application is unable to esablish a connection. If this occurs, an error is shown in the code view of the App Designer, and the app crashes. Is there any way to create a sort of timeout in the code to keep the app from crashing while in use?
Attached is a picture of the UI I've created as well as the code used to create the bluetooth connection. When the user clicks the 'Connect' button, the shown code runs to pair the bluetooth device. This is where I'd like to be able to catch a failed connection before the app crashes. Normally the error occurs at the "fopen(app.bt);" line indicating the device cannot connect.
Note: The "Message Board" area of the UI is used to display error messages to the user
% Button pushed function: ConnectButton
function ConnectButtonPushed(app, event)
% Initialization and setup of BT device
if(app.connectFlag == 0 && app.inputSelectFlag == 1 && app.wirelessSelectFlag == 1)
% Clear message board
app.MessageBoardLabelText.Text = '';
instrreset; % clear and reset any existing instrument object
app.bt = Bluetooth(app.btName,1); % create bluetooth object using user selected device
fopen(app.bt); % Open BT connection
% Lamps indicating successful connection
app.ConnectLamp.Color = [0,1,0]; % Green
app.ConnectLabel.Text = 'Connection Successful!';
app.DisconnectLamp.Color = [0.9,0.9,0.9]; % Grey
app.DisconnectLabel.Text = '';
% Update Flags
app.connectFlag = 1;
app.disconnectFlag = 0;
app.wirelessSelectFlag = 0;
% app.inputSelectFlag = 0;
% app.transmissionSelectFlag = 0;
app.signalAnalysisFlag = 0;
app.resetFlag = 0;
app.setupAxesFlag = 0;
elseif(app.connectFlag == 0 && app.inputSelectFlag == 0 && app.wirelessSelectFlag == 1)
app.MessageBoardLabelText.Text = 'Please select input device!';
app.MessageBoardLabelText.FontColor = [1.0,0.0,0.0]; % red text
elseif(app.connectFlag == 0 && app.inputSelectFlag == 1 && app.wirelessSelectFlag == 0)
app.MessageBoardLabelText.Text = 'Please select wireless device!';
app.MessageBoardLabelText.FontColor = [1.0,0.0,0.0]; % red text
elseif(app.connectFlag == 0 && app.inputSelectFlag == 0 && app.wirelessSelectFlag == 0)
app.MessageBoardLabelText.Text = 'Please select input and wireless device before connecting!';
app.MessageBoardLabelText.FontColor = [1.0,0.0,0.0]; % red text
else
app.MessageBoardLabelText.Text = 'Device is already connected!';
app.MessageBoardLabelText.FontColor = [1.0,0.0,0.0]; % red text
end
end
  1 Commento
Motaz Kaoud
Motaz Kaoud il 26 Mag 2021
Hi, Has it work? i'm working in a simmilar project to receive data from a Bluetooth device using App designer tool.

Accedi per commentare.

Risposte (2)

epzolo
epzolo il 26 Mag 2021
Yes it does work. I finished it a year ago so my recollection isn’t great. I don’t think I figured out a way to set a timeout unfortunately. But the application and other error catching code works great.
  2 Commenti
Motaz Kaoud
Motaz Kaoud il 26 Mag 2021
Great, Can you guide me in using App Designer to receive data from a such Bluetooth device? what's the libraries or functions should i use to read/write a stream of data with a Bluetooth on microcontroller using app designer thanks for advance
epzolo
epzolo il 26 Mag 2021
I used hardware including an arduino and an Hc-05 Bluetooth module to transfer the data from the sensor circuit into the matlab environment. I used basic arduino commands and the Bluetooth read commands shown in the code above to establish the connection. Then I used read commands in matlab to extract the data after the transfer.

Accedi per commentare.


Maty
Maty il 8 Mar 2024
Bonjour, je travail sur un projet de robot et j'aimerais faire la connexion de matlab app designer avec un module bluetooth,alors j'aimerais que vous m'expliquiez la procedure a suivre.

Categorie

Scopri di più su MATLAB Support Package for Arduino Hardware in Help Center e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by