PDA

View Full Version : Is it possible to program Bluetooth-serial port in C++?




awary
Oct 29, 2009, 02:32 PM
Dear all!
I need to integrate a bluetooth-serial port communication in an application that I'm already developing.
Does anyone know any easy example of a bluetooth serial port communication in C++?
What I've seen so far in the examples and in the documentation is always written in objective-C and I cannot integrate Objective-C in my program since I'm using C++.

I'm looking for it everywhere and I've no cue if it's possible to do it. :(
I would really appreciate if someone can give me any advice.:)

Thank you in advance!



chown33
Oct 29, 2009, 02:56 PM
Compile as Objective-C++?

robbieduncan
Oct 29, 2009, 03:08 PM
IOBluetooth has a pure-C interface. Documentation (http://developer.apple.com/mac/library/documentation/DeviceDrivers/Reference/IOBluetooth/index.html)

awary
Nov 2, 2009, 04:35 AM
Thanks for your answer but I think I do not really understand how I can do it: Chown33: Is there any possibility of compiling objective-c and C++ at the same time and mixed? Is it really optimal to do this?

RobbieDuncan: I'm glad you answer since all I've seen in internet about bluetooth and apple is done by you! Does it mean I can really use it calling the functions in C way or should I do kind of a wraper for that (I've read this somewhere but I've no idea how to do it...)

Thanks anyway for your fast answer!

Sander
Nov 2, 2009, 05:06 AM
Is there any possibility of compiling objective-c and C++ at the same time and mixed? Is it really optimal to do this?


There are some limitations, but in general you can mix Objective-C and C++ just fine. You can pass Objective-C objects to C++ functions and use them, and vice versa. One severe limitation is that you cannot have C++ objects as member variables ("ivars") in Objective-C classes and you have to do that by pionters.

robbieduncan
Nov 2, 2009, 05:08 AM
RobbieDuncan: I'm glad you answer since all I've seen in internet about bluetooth and apple is done by you! Does it mean I can really use it calling the functions in C way or should I do kind of a wraper for that (I've read this somewhere but I've no idea how to do it...)

Well that's kind-of worrying as I only wrote a couple of very small apps way back! Anyways the API should be callable just using C functions: you don't need a wrapper.

awary
Nov 2, 2009, 05:38 AM
OK I'll try and comment it here, if it works!
Thanks a lot!

awary
Nov 10, 2009, 10:13 AM
Thanks a lot!
It works really well!
All the necessary functions are explained in:
#include <IOBluetooth/IOBluetoothUserLib.h>
#include <IOBluetooth/IOBluetoothUtilities.h>

You do not even have to mix object-c and C++ it's already available for C++.

Muncher
Nov 10, 2009, 08:23 PM
I think you might also be able to access them like a general unix serial port (i.e. open() the file in /dev/tty.port_name). When I was fiddling around with that a little while ago I noticed bluetooth serial ports also appear there.

awary
Dec 1, 2009, 03:36 PM
In fact now I've problems with the problems with IOBluetoothDeviceOpenRFCOMMChannelSync function.
It just works randomly and even if I see that the bluetooth is connected to the device it does not work. So I've to run the program several times until it connects, it's really annoying. I've tried to set up a loop that call this function several times until it works, but it does not work neither. I think it usually takes some time until the connection is ready to set up a channel so I've tried to add sleeps or flags in the program to all channelSync once the connection is open.
But it does not work neither :/

I've been looking for \dev and I do not even find the folder. In fact if it could be set up as a linux serial port it would be perfect.

robbieduncan
Dec 1, 2009, 04:14 PM
Hmm, is your program threaded? I found that Bluetooth did not work at all with threaded Cocoa apps: needed to move the Bluetooth stuff out to a separate non-threaded non-GUI process!

chown33
Dec 1, 2009, 05:17 PM
I've been looking for \dev and I do not even find the folder.

A. How are you looking for it?
B. The correct pathname is "/dev", not "\dev".

If you enter this command, it should list a lot of things. If it doesn't, post the exact error message:

ls -l /dev

awary
Dec 2, 2009, 06:10 AM
Hmm, is your program threaded? I found that Bluetooth did not work at all with threaded Cocoa apps: needed to move the Bluetooth stuff out to a separate non-threaded non-GUI process!


Hi!
Exactly, I wanted to use several threads to do different CFRunloop in each one since I wanted to have two devices connected at the same time by bluetooth and sending different type of commands. However, I did a previous version with just one thread and it wasn't connecting at the first time anyway...
I was already creating the device with a given address so it should not have problems to find it..

Now, I've seen that in the next versions of the Bluetooth (upper than 2.2) they indicate that the C-API is already deprecated and is going to be removed soon, thus they suggest to use the Objective-C equivalents. So once again, I've to deal with the mix of Objective-C and C++, or try to do it using Linux commands.
Do you know any example of bluetooth connection doing it with Linux commands?

robbieduncan
Dec 2, 2009, 06:42 AM
Now, I've seen that in the next versions of the Bluetooth (upper than 2.2) they indicate that the C-API is already deprecated and is going to be removed soon, thus they suggest to use the Objective-C equivalents.

Really? I didn't know that.

So once again, I've to deal with the mix of Objective-C and C++, or try to do it using Linux commands.
Do you know any example of bluetooth connection doing it with Linux commands?

I've no idea where this Linux thing came from: Mac OSX has nothing to do with Linux. It's Unix underpinnings are real Unix (BSD), not Unix-like (Linux). Anyways I'd not recommend going that way. If I were you I'd package all the Bluetooth comms bit up into a stand-alone, non-GUI Objective-C app that you can start as a sub-process of your main app and communicate with over STDIN/OUT. That way you can keep your C++ code, multi-thread etc in the main app.

awary
Dec 2, 2009, 09:03 AM
If I were you I'd package all the Bluetooth comms bit up into a stand-alone, non-GUI Objective-C app that you can start as a sub-process of your main app and communicate with over STDIN/OUT. That way you can keep your C++ code, multi-thread etc in the main app.

It sounds good!, however I do not have any experience with Objective -C. I had a look to you application for blue tooh but I do not understand well the use of the dictionaries, the (NSNotification *) aNotification, userinfo...
The type of notifications and how to treat is already define or did you do it by your self?
I Imagine that it's the equivalent of the callbacks that I was using in C++ but I cant find where they are defined.

If you know any document where I could find usuefull information about how to use this bluetooth library it would be very helpfull.
In the description of the functions there is nothing...

Thanks a lot in advance!

robbieduncan
Dec 2, 2009, 09:09 AM
Notifications are not really the same as callbacks: they are posted to a notification centre which distributes them to any object that registers for them. So they can go to any number of objects.

All the documentation is in the link I first posted.

If there is specific code you don't understand then copy/paste it here and I'll try and explain what I was doing, although it's a very long time since I looked at that code!

Detrius
Dec 3, 2009, 02:35 PM
I think you might also be able to access them like a general unix serial port (i.e. open() the file in /dev/tty.port_name). When I was fiddling around with that a little while ago I noticed bluetooth serial ports also appear there.

YES, you can do it this way. I had a program I was working on a while ago that would search for a certain set of USB devices, but they appeared as serial ports. I had to tweak it a bit because every time one of my customer's did this search, his phone would beep on and off, since it was connected via bluetooth. I had gone this route of using the native Unix interface to the serial ports.

If you're already using boost, take a look at Boost.Asio, as they've added support for serial ports recently. I can't vouch for how well it works with serial ports, but it's wonderful for asynchronous network communications.