How to create an Android RFCOMM socket without any input from the user?
Asked Answered
S

3

30

Here's the scenario:

I need to write an application for Android to create a RFCOMM socket to a PC with a Bluetooth dongle (I'm going to write the server too).

My requirement is that the user doesn't have to pair device manually.

Actually, with a big hack, I'm using the createInsecureRfcommSocket.

A little scenario: I've an Android application which exchanges information with a Linux box with a RFCOMM socket opened. I can manually set the PIN on the devices (hard coding it IS an option)

I'm looking for different roads:

Write a JNI wrapper

As has been done here by Max Kellermann, I can write a JNI layer to make all the pairing phase. This should be a good option, but there's a problem:

The NDK 4b does not provide libbluetooth libraries, so -lbluetooth fails, as well as the NDK v.3.

Options:

  1. Find the NDK 1.5 (which includes lib bluetooth). After days of web search I've been not able to find out. Anyone has or knows where I can find it?
  2. Compile libbluetooth for Android by myself and use them for -lbluetooth. No lucky there, I'm not able to build them. Any hint?

Use something exposed by the APIs

Any one know how can I use createRfcommSocketToServiceRecord and have the user not to manually pair the device? Is it possible? How should I write the server?

Something I do not know

Maybe (sure!) there is something I do not know. Maybe I can use something else? Not RFCOMM? SDP?

Maybe I can manually pair with the Android API?

I hope to have been clear enough, if not just ask. And again, as it's not the first time, I'm in your hands :)

Thanks for all the support guys!

Samuella answered 17/11, 2010 at 14:37 Comment(9)
Speculating, but I would not be surprised if it's intentionally designed so that user involvement (approval) is required for pairing. At least that would be consistent with other places where the android design requires user interaction for security reasons.Instead
Yes, but the "official" api are not the only way. For that I'm asking about ndk 1.5, build the libbluetooth or the other @hide functions.Samuella
If libbluetooth has been removed, most likely the older version wouldn't work on a newer android version even if you found it. That's the constant warning against using non-public API's - future changes may break them.Instead
I'm not sure I understand why you want this to work without the need for pairing manually (which is a fairly trivial thing for the user to do and is a one-time procedure).Letterperfect
I understand your point, and I agree. But it's a requirement for a project I'm involved. And the explanation is quite trivial, let's think about some kind of work which need to check a lot of different "things" which expose some bluetooth services. Pairing every time with the device is simply a waste of time.Samuella
Is this what you need code.google.com/p/androidobex/downloads/… ?Pinnate
Enrico, I'm sure this doesn't answer your question, but maybe it's worth asking -- are there any other communication mechanisms you can use to get this job done? BT is annoying, a bit. One thing you might do is encode your data into the BT device name that your phone broadcasts, and then a scan for devices will get that name (which is a few bytes of data).Expect
What's your use-case here? If you're querying things to find out if a service is available on a device, wouldn't SDP be a better option? I'm pretty sure you don't have to pair for service discovery.V
Thanks all for the hints. With order: @sugarynugs thanks, I have been able to compile the by myself but it's not enough. Android itself has a very strange way to use bluez... @Expect We are currently using both BT and wifi connection, but in some scenario BT is mandatory. @Gaz yes, but whit sdp I can only find services, but I need to have a channel over which communicate... Once I've fount the rfcomm socket broadcasted by sdp I must connect to it, thus pairing is needed...Samuella
S
2

At the end, I can say that you can't.

Even if you find a way, using wrappers, writing C modules and so on, android evolution will probably change the thing you're using.

And so, there's no option. Sadly.

Let's see how Android Bluetooth API will change in the future.

Samuella answered 20/2, 2011 at 13:22 Comment(0)
C
0

Yes we can create it Rfcomsocket and listen the socket without user concerns and also without pairing devices. https://code.tutsplus.com/tutorials/create-a-bluetooth-scanner-with-androids-bluetooth-api--cms-24084

Follow this !!

Cirrhosis answered 6/11, 2019 at 9:56 Comment(1)
Thanks for the reply :D I need to point out the question was from November 2010 though :DSamuella
H
-2

The Android Bluetooth API is not finalized, and will change. Use at your own risk. This class implements an API to the Bluetooth RFCOMM layer. An RFCOMM socket is similar to a normal socket in that it takes an address and a port number. The difference is of course that the address is a Bluetooth-device address, and the port number is an RFCOMM channel. The API allows for the establishment of listening sockets via methods bind, listen, and accept, as well as for the making of outgoing connections with connect, connectAsync, and waitForAsyncConnect. After constructing a socket, you need to create it and then destroy it when you are done using it. Both create and accept return a FileDescriptor for the actual data. Alternatively, you may call getInputStream and getOutputStream to retrieve the respective streams without going through the FileDescriptor.

Hypotenuse answered 21/1, 2011 at 5:52 Comment(2)
Have you read the question? Because I cannot understand the answer... It does not fit at all.Samuella
This has been copied and pasted from androidjavadoc.com/1.0_r1_src/android/bluetooth/…. Please add your own input.Baxy

© 2022 - 2024 — McMap. All rights reserved.