Creating a manual timeout for bluetooth connection in MATLAB App Designer

조회 수: 9 (최근 30일)
epzolo
epzolo 2020년 7월 4일
답변: Maty 2024년 3월 8일
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
Motaz Kaoud
Motaz Kaoud 2021년 5월 26일
Hi, Has it work? i'm working in a simmilar project to receive data from a Bluetooth device using App designer tool.

댓글을 달려면 로그인하십시오.

답변 (2개)

epzolo
epzolo 2021년 5월 26일
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
Motaz Kaoud
Motaz Kaoud 2021년 5월 26일
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 2021년 5월 26일
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.

댓글을 달려면 로그인하십시오.


Maty
Maty 2024년 3월 8일
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.

카테고리

Help CenterFile Exchange에서 MATLAB Support Package for Arduino Hardware에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by