I'm trying to implement a custom Bluetooth service on a NCO C.H.I.P. with Debian 4.3 (in C++) and connect to it via a custom app on my 6.0.1 Android phone (in Java). Using latest bluez tools and libraries, and latest AndroidStudio SDK.
'sdptool browse --raw local' shows my service, with its UUID. My phone pairs with CHIP without problems. But all connection attempts fail.
Sequence
Attribute 0x0000 - ServiceRecordHandle
UINT32 0x00010005
Attribute 0x0001 - ServiceClassIDList
Sequence
UUID16 0x1400 - HDP
Attribute 0x0003 - ServiceID
UUID128 dc0f9df0-bdda-11e6-9598-0800200c9a66
Attribute 0x0004 - ProtocolDescriptorList
Sequence
Sequence
UUID16 0x0100 - L2CAP
Sequence
UUID16 0x0003 - RFCOMM
UINT8 0x1b
Attribute 0x0005 - BrowseGroupList
Sequence
UUID16 0x1002 - PublicBrowseGroup
Attribute 0x0009 - BluetoothProfileDescriptorList
Sequence
Sequence
UUID16 0x1400 - HDP
UINT16 0x0100
The UUIDs match on both sides.
Requesting all UUIDs (http://digitalhacksblog.blogspot.com/2012/05/android-example-bluetooth-discover-and.html) only shows a Headset service UUID, not mine. My phone can't be restricted from connecting to some classes of service: I see several "weird" service UUIDs from my Honda HandsFreeLink.
Tried re-pairing while the server had the socket open and was listening, no difference.
I changed the class of service and profile from Health Device to Serial Port and a new UUID now appears -- but it is a "standard" 16bits Bluetooth UUID, not the one I specified for the service. Tried using the that Serial Port UUID in my app instead of my customer UUID, still no connections.
The server code is pretty standard: I get as far as the "Waiting for client to connect..." message, then no further.
struct sockaddr_rc loc_addr = { 0 }, rem_addr = { 0 };
char buf[1024] = { 0 };
int bytes_read;
socklen_t opt = sizeof(rem_addr);
// allocate socket
g_socket = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
if (g_socket < 0) {
fprintf(stderr, "ERROR: socket(2) failed: %s\n", strerror(errno));
goto abort;
}
// bind socket to port 27 of the first available
// local bluetooth adapter
loc_addr.rc_family = AF_BLUETOOTH;
loc_addr.rc_bdaddr = {0, 0, 0, 0, 0, 0};
loc_addr.rc_channel = (uint8_t) 27;
bind(g_socket, (struct sockaddr *)&loc_addr, sizeof(loc_addr));
// put socket into listening mode
listen(g_socket, 1);
printf("Waiting for client to connect...\n");
// accept one connection
client = accept(g_socket, (struct sockaddr *)&rem_addr, &opt);
if (client < 0) {
fprintf(stderr, "ERROR: accept(2) failed: %s\n", strerror(errno));
goto abort;
}
ba2str( &rem_addr.rc_bdaddr, buf );
fprintf(stderr, "accepted connection from %s\n", buf);
So is the client code: it fails in the connect() call with:
D/BluetoothSocket: connect(): myUserId = 0
W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback
E/MyAPP: No connect: read failed, socket might closed or timeout, read ret: -1
try {
mSocket = myDevice.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
Log.e(TAG, "No RFCOMM: " + e.getMessage());
return false;
}
try {
mSocket.connect();
} catch (IOException e) {
Log.e(TAG, "No connect: " + e.getMessage());
return false;
}