Bluetooth SPP server in C/C++ for Android based device

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
AsAl_4421831
Level 2
Level 2
10 replies posted 10 sign-ins 5 replies posted

 

Hello Everyone,

I am running android on imx7d from NXP running Bluetooth+Wifi 1MW module.

I want to run a Bluetooth SPP server on the device. I have implemented the software in C++, Basically an RFCOMM socket. I read Android used Bluedroid instead of Bluez like Linux. But I understand the RFCOMM socket creation method is the same(not 100% sure).

I run the C++ based server software on the imx7d device, it doesn't give any issue creating the RFCOMM SPP port. It creates the port and goes into listening mode. However, when the Android Bluetooth client app running on my phone tries to connect it, It gives the following error in the android bt stack of my imx7d based device.

> [INFO:port_utils.cc(329)] port_find_mcb_dlci_port: Cannot find
> allocated RFCOMM app port for DLCI 40 on 3c:28:6d:2a:b0:28,
> p_mcb=0x90367c24 PORT_ParNegInd dlci:40 mtu:990 2021-11-26
> 09:04:36.046 583-827/com.android.bluetooth E/bt_stack:
> [ERROR:port_rfc.cc(285)] PORT_ParNegInd: Disconnect RFCOMM, port not
> found, dlci=40, p_mcb=0x90367c24, bd_addr=3c:28:6d:2a:b0:28

I have created the server port on channel 20, but it's not able to find it. I am not sure what exactly is going wrong, I am stuck and not able to find the way to move ahead. Here is the code snippet.

#define BTPROTO_RFCOMM 3

/* BD Address */ typedef struct {
uint8_t b[6]; } __attribute__((packed)) bdaddr_t;

/* ---- RFCOMM sockets ---- */ struct sockaddr_rc {
sa_family_t rc_family;
bdaddr_t rc_bdaddr;
uint8_t rc_channel; };

#define BDADDR_ANY (&(bdaddr_t){{0, 0, 0, 0, 0, 0}})

/* server channel */
#define RFCOMM_SERVER_PORT_NUM 20

void BluetoothSerialPort::openConnection()
{
struct sockaddr_rc loc_addr;
bdaddr_t bdaddr_any = {0, 0, 0, 0, 0, 0};
ALOGE("Start Bluetooth SPP server...\n");

/* allocate socket */
sockFd_ = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
if (sockFd_ < 0)
{
ALOGE("Cannot start Bluetooth SPP server\n");
}
memset(&loc_addr, 0, sizeof(loc_addr));
loc_addr.rc_family = AF_BLUETOOTH;
loc_addr.rc_bdaddr = bdaddr_any;
loc_addr.rc_channel = RFCOMM_SERVER_PORT_NUM;

if (bind(sockFd_, (struct sockaddr *)&loc_addr, sizeof(loc_addr)) < 0)
{
ALOGE("socket bind error %s", strerror(errno));
exit(EXIT_FAILURE);
}

ALOGE("Listen Bluetooth SPP server on %d...\n", sockFd_);
if (listen(sockFd_, 2) < 0)
{
ALOGE("socket listen error %s", strerror(errno));
exit(EXIT_FAILURE);
}
}

// Runs the server.
void BluetoothSerialPort::run()
{
struct sockaddr_rc client_addr;
socklen_t opt = sizeof(client_addr);
openConnection();
ALOGE("Listening on Bluetooth SPP server = %d...\n", sockFd_);
/* return new socket for connection with a client */
clientFd_ = accept(sockFd_, reinterpret_cast<struct sockaddr *>(&client_addr), &opt);
ALOGE("connected from %s\n", (char *)&client_addr.rc_bdaddr);
if (clientFd_ == -1)
{
ALOGE("error accepting incoming connection: %s", strerror(errno));
}
else
{

ALOGI("got new connection");
}

// Create a thread and transfer the new stream to it.
std::thread thr(std::bind(&BluetoothSerialPort::readHandler, this));
thr.detach();
}

Can somebody please help? Is there any mistake in the code, it prints the following line "Listening on Bluetooth SPP server" and goes into listening mode.


Thanks much,
Asma

0 Likes
1 Solution

Hello @AsAl_4421831 

In your case, 

  • SPP runs in the Android BT stack, Android is open source, we don’t have any BT expertise for Android stack
  • From a FW point of view, seems to be working correctly.
  • From your comment "Listening on Bluetooth SPP server" and goes into listening mode:  Looks like the Android is waiting for a Client SPP to connect

Regards,
Anjana 

View solution in original post

0 Likes
3 Replies
AnjanaM_61
Moderator
Moderator
Moderator
5 comments on KBA First comment on KBA 5 questions asked

Hello @AsAl_4421831 

Can you please check with murata partner and confirm if you are using latest firmware files? 

What is the peer device which you are trying to connect? Did you try with any other device?

Regards,

Anjana

0 Likes
AsAl_4421831
Level 2
Level 2
10 replies posted 10 sign-ins 5 replies posted

Hi @AnjanaM_61 

Yes, we are using firmware with LMP version 5.0 from March 2021.

The peer device is an android device, our app runs on android device. 

Yes, I tried with other devices it's same. Normal pairing and connection works.

But SPP/rfcomm communication does not.

 

Thanks,

Asma

0 Likes

Hello @AsAl_4421831 

In your case, 

  • SPP runs in the Android BT stack, Android is open source, we don’t have any BT expertise for Android stack
  • From a FW point of view, seems to be working correctly.
  • From your comment "Listening on Bluetooth SPP server" and goes into listening mode:  Looks like the Android is waiting for a Client SPP to connect

Regards,
Anjana 

0 Likes