Establishing bluetooth-connection with native code

3,015 views
Skip to first unread message

Christian Linne

unread,
May 9, 2010, 8:31:57 AM5/9/10
to andro...@googlegroups.com

Hi,

I'm actually working on a project that has to inherit an enormous part of native code to interact with a special kind of remote device (an inverter) using a bluetooth-connection with a RFCOMM-Socket. The data that is exchanged between both devices is preprocessed through a library (in C) that I may not port for compatibility issues. As android uses the BlueZ-library, I thought it would be a good idea to directly access this on native level (even because the program should be compatible for Android 1.6 +), and that's where the problem appears.
I have used this tutorial to start this up, and the device-inquiry with reading the remote devices' names works fine (this shows how that works), but trying to connect to a remote devices causes an illogical error. Using " status = connect(s, (struct sockaddr *)&addr, sizeof(addr)); " (with "addr" being a sockaddr_rc) always sets "errno" to 113, also known as "EHOSTUNREACH" - tested on an Archos 5 (Android 1.6); Trying the same on a Nexus One (Android 2.1) causes a segmentation fault.
In the first case, it rather seems that the remote devices (I have checked out more than a dozen) do not event recognize that my devices tries to connect with them.

Has anyone got an idea why these errors might happen? At the moment, I am running out of reasons that may cause it...
I have already checked out the following:
- Hardware : Works fine.
- Remote address: correct (including the byte-order)
- Remote port: correct & accessable with other devices using other versions of my program
- Local port: also
- Connection family: settled correct

Another option may be to use the backport-library, but that would require that I may exchange data from native code ( C ) to Java and send it without changing any bit - the remote device only works on native code and is extremly exakt on this. Is there any kind of type that may match this (e.g. byte) ? If the first option shown above would not work anyway, this might me an alternative, even if my principal does not like having to much Java-Code in programs ;)

Regards,
christian.l

--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To post to this group, send email to andro...@googlegroups.com.
To unsubscribe from this group, send email to android-ndk...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.

David Turner

unread,
May 9, 2010, 2:49:16 PM5/9/10
to andro...@googlegroups.com
The fact that Android uses the Bluez library is an implementation detail. This is not guaranteed on all Android devices; even which exact version of Bluez is being used may vary (and has varied in the past, which is why we had to delay exposing some of the BT APIs from Java).

I thus really recommend that you only use the exposed Java APIs to use Bluetooth in your program.

Christian Linne

unread,
May 10, 2010, 1:55:39 AM5/10/10
to andro...@googlegroups.com
If the project I'm actually working on would only be mine, I would do that  (not only because I'm quite better in programming Java-Code ;) ), but my principal wants bluetooth-support for the entire program and for Android 1.6+ , not only for Android 2.0+ .
The problem with the data-part as described before also exists, for that was the main reason to try this with native code. Every bit sended counts, because it inherits a custom header and custom redundancy that is constructed with native code ( C ) - changing only one bit would make the receiving inverter not to understand the packet and throw it away, and that is not what I want (understandable, I think).  
But thanks for your answer.
 
Regards,
christian.l
2010/5/9 David Turner <di...@android.com>



--
_________
Mit freundlichen Grüßen
Christian Linne

Mail (Privat): linn...@aol.com
Mail (Offiziell): Christi...@googlemail.com, Christi...@student.uni-kassel.de
ICQ: 293253013

Nikolay ILYIN

unread,
Jan 26, 2017, 10:32:26 AM1/26/17
to android-ndk
Dave, your post was a while ago, but I want to get more details on how we can use your advice to access system BT Java API from native C-code.

To be more specific let me focus on how to scan / search BT devices via JNI interface.

I have attached the file with Java implementation, where OnReceive method is highlighted in red color, how we can implement the same in C code (the place, where ???? signs are shown)?

Thank you in advance,

Nikolay
AndroidBluetoothJavaToC.pdf

Nikolay ILYIN

unread,
Feb 26, 2017, 6:49:44 PM2/26/17
to android-ndk
hi, is there any advice on that from Google NDK team?
Reply all
Reply to author
Forward
0 new messages