serialport / bluetooth issues

There seems to be a lot of bluetooth issues with Matlab and MacOS. I connect to various ESP32's with bluetooth and I have to use "serialport" in order to communicate with them. blelist, bluetoothlist, etc. will crash matlab but I have this in a different post ( https://www.mathworks.com/matlabcentral/answers/1870442-macos-bluetoothlist-command-hangs-up-busy ). There was a suggestion that this would be fixed for 2023a but doesn't appear that it was.
At any rate, the problem I'm having with serialport() is that I have to quit Matlab completely and forget all my bluetooth pairing's every time before I open my .mlapp . On Mac this is requires a lot of mouse clicks and is tedious when you have to "forget" and reconnect a lot of bluetooth pairs. I tried to delete the ports before I close out my .mlapp but this doesn't seem to help. Looking at this post ( https://www.mathworks.com/matlabcentral/answers/886269-how-do-i-close-serialport-objects ) it suggests the ability to drop the handles was intentionally removed. Does anyone know if there is a way to drop the handles?
Does anyone out there have a reliable way to connect a Mac to a bluetooth device via matlab to send and receive serial data?

Answers (1)

Walter Roberson
Walter Roberson on 4 Jun 2023
What that second post is telling you is that serialport() has an internal onCleanup that is invoked when serialport() detects the device is no longer reachable (because it is unplugged), so when the device gets unplugged, all of the internal MATLAB records of the port are removed -- there are no more handle objects left to drop when this situation occurs.
At the moment, I do not have any ideas why you need to re-pair the devices, so I cannot address that -- but it is not because of left-over handles.

3 Comments

So with MacOS / Unix when a device is paired you'll get the "cu.*" and the "tty.*" in the device "dev" folder ( * = name of device). These devices stay in that folder whether there is a radio link to the device or not (connected or disconnected). The only way to remove the device from the "dev" folder is by forgetting the device (on mac this is going into bluetooth settings, clicking on the (i), and then clicking on "Forget This Device..."). Forgetting the device, which is tedious with lots of devices, is the only way to 'unplug' the device. After this action the "cu.*" and the "tty.*" in the device "dev" folder will go away.
Bluetooth as I understanding has two layers; Pairing and Connection. Must be paired in order to have a connection but don't need to be connected (for instance if you go to work and left your headphones at home the computer has no connection to the headphones but when you come back home, the computer knows to reconnect with them because the device was paired). Matlab seems to treat pairing as the 'cable connection' (plug / unplug).
I am running Catalina on the system I am using at the moment. I have three paired bluetooth devices. The only cu.* and tty.* in my /dev are /dev/cu.Bluetooth-Incoming-Port and /dev/tty.Bluetooth-Incoming-Port and there is nothing in /dev that looks like a device file for the paired bluetooth entities.
You'll want to use a bluetooth device that has Serial Port Profile (SPP). If you have an ESP32 or something similar you can setup SPP on that device. Basically it allows a bluetooth device to act like a serial device.

Sign in to comment.

Products

Release

R2023a

Asked:

on 3 Jun 2023

Commented:

on 18 Jun 2023

Community Treasure Hunt

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

Start Hunting!