How to make a re-connection for Bluetooth communication?

10 次查看(过去 30 天)
Hello! I am currently developing a MATLAB APP Designer application that connects to a Bluetooth device and plots the data received from the device's analog inputs. The code for the application is mostly functional, but I am facing an issue where the application does not attempt to reconnect to the Bluetooth device if the connection is lost between the application and the device.
To address this issue, I have implemented a code to check the connection status and attempt to reconnect to the device in case of a disconnection. The code snippet is provided below.
Is there something that I made a mistake about?
It still does not make a reconnection once after losing its connection.
It would be really appreciated if there is someone who can give me a clue about this.
I will attach an app also for just in case.
Thankyou very much.
methods (Access = private)
function onDataRecieve(app, src)
app.timer = timer('ExecutionMode', 'fixedRate', 'Period', 1, 'TimerFcn', @(~,~)checkDataReceived(app));
app.ble = ble(app.BLEAddressEditField.Value);
start(app.timer);
function checkDataReceived(app)
% Get the timestamp of the last received packet
lastTimestamp = datetime(app.bleData.timestamp(app.bleData.count, :));
% Check if the last packet was received more than 1 second ago
if (datetime('now') - lastTimestamp) > seconds(1)
% Stop the timer
stop(app.timer);
% Disconnect from the BLE device
disconnect(app.ble);
% Reconnect to the BLE device
app.ble = ble(app.BLEAddressEditField.Value);
start(app.timer);
end
end
%
[data, timestamp] = read(src,'oldest');
timestamp.Format = 'yyyy-MM-DD hh:mm:ss.SSS';
count = app.bleData.count+1;
if count >= size(app.bleData.rx,1)
app.bleData.rx(count + 100,app.bleData.MTUlength) = 0;
aa = strings([100,1]);
app.bleData.timestamp = [app.bleData.timestamp; aa];
end
if app.bleData.leadigZeroCut==true %
app.bleData.rx(count,:) = data(2:end);
else
app.bleData.rx(count,:) = data;
end
app.bleData.count = count;
app.RecievedPacketsEditField.Value = count;
app.bleData.timestamp(count,1) = timestamp;
data_1 = app.bleData.rx(count,:);
app.ChannelPktSizeEditField.Value = app.bleData.MTUlength/app.ChannelCountSpinner.Value;
switch app.ChannelCountSpinner.Value
case 1
app.bleData.channel0(count,:) = data_1;
case 2
app.bleData.channel0(count,:) = data_1(2:2:end);
app.bleData.channel1(count,:) = data_1(1:2:end);
case 3
app.bleData.channel0(count,:) = data_1(3:3:end);
app.bleData.channel1(count,:) = data_1(2:3:end);
app.bleData.channel2(count,:) = data_1(1:3:end);
case 4
app.bleData.channel0(count,:) = data_1(4:4:end);
app.bleData.channel1(count,:) = data_1(3:4:end);
app.bleData.channel2(count,:) = data_1(2:4:end);
app.bleData.channel3(count,:) = data_1(1:4:end);
end
if mod(count,app.axes.updateSpeed) == 0
app.updateFigure;
if count > app.XCountSpinner.Value %originally 2
if strcmp(app.AutoUpdatePlotSwitch.Value, 'On')
xlim(app.UIAxes, [(count-app.XCountSpinner.Value)*app.bleData.MTUlength, count*app.bleData.MTUlength]/app.ChannelCountSpinner.Value);%originally 2
xlim(app.UIAxes_2, [(count-app.XCountSpinner.Value)*app.bleData.MTUlength, count*app.bleData.MTUlength]/app.ChannelCountSpinner.Value); %originally 2
xlim(app.UIAxes_3, [(count-app.XCountSpinner.Value)*app.bleData.MTUlength, count*app.bleData.MTUlength]/app.ChannelCountSpinner.Value);%originally 2
xlim(app.UIAxes_4, [(count-app.XCountSpinner.Value)*app.bleData.MTUlength, count*app.bleData.MTUlength]/app.ChannelCountSpinner.Value); %originally 2
end
end
end
end
%Rest of the code for plotting~~~
end

回答(1 个)

Dinesh
Dinesh 2023-4-5
Hello.
The issue with your code may be related to the fact that you are starting the timer inside the "onDataRecieve" function. This function will be called every time a new data packet is received. So, this means you will be creating multiple timers, which might interfere with each other.
I suggest moving the timer creation and start outside of the "onDataRecieve" function, so that you only create and start the timer once. You can do this in the "startupFcn" function or in any other function where you initialize your app.

Community Treasure Hunt

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

Start Hunting!

Translated by