Bluez application running on PC for listening an Android Device

2.3k Views Asked by At

I want to develop an application that use the Linux Bluetooth API Bluez by mean allow an Android device to send and receive data.

For the PC side i have looked at this code: http://people.csail.mit.edu/albert/bluez-intro/x502.html because Android use RFCOMM protocol, or i guess that since is impossible to set manually the protocol.

Since in Android is only possible to use the Java API (Bluez API locked) and the SDP in not working very well i have to use this call:

InsecureBluetooth.createRfcommSocket(BlueDevice , Channel , false);

When i set the channel for example to 0xC (12) that is one of the channel returned by the command:

sdptool browse local | grep Channel

the android application can connect correctly to the PC.

The problem is that i can't figure out what are the parameter to allow the two applications to communicate. To the PC side i have only a port number with value 1. To the Android side i have a channel that is assigned to a service available by SDP. But SDP is not working in Android so how i can do that ?

Any help is appreciated. Thanks

2

There are 2 best solutions below

1
On

I know this doesn't answer your question, I think bluecove (http://bluecove.org/) would be better choice for this. It supports most operating systems, and provides decent library for Java.

I don't think bluez will work for Windows, you need a WIDCOMM or BlueSoleil stack for it, and bluecove will use appropriate stack for you.

0
On

I have solved in this way:

For the PC side using the SDP record. We have to record the service to a well know channel that must be FREE, i mean not used by other service. To chek it do the command:

sdptool record local

The code to do that is at http://people.csail.mit.edu/albert/bluez-intro/x604.html

Section Example 4-9. Describing a service

This two line must be added after // set the general service ID

// set the service class ID
sdp_list_t service_class = {NULL, &svc_uuid};
sdp_set_service_classes( record, &service_class);

and we have to change the channel to one not used by other service. For example the channel 1 :

uint8_t rfcomm_channel = 1;

Now that we have something listening to the PC side we have to connect with the android device directly to the channel one by the well know reflection method because unfortunately the SDP now work at all in android.

Method bluetoothConnect;

bluetoothConnect = BlueDevice.getClass().getMethod("createRfcommSocket", new Class[]{int.class} );
CommunicationChannel = (BluetoothSocket) bluetoothConnect.invoke(BlueDevice, 1);

CommunicationChannel.connect();

Of course BlueDevice is a device discored by the startDiscovery() method and the "1" stand for channel 1.