Is it possible to program Bluetooth-serial port in C++?

Discussion in 'Mac Programming' started by awary, Oct 29, 2009.

  1. macrumors newbie

    Joined:
    Oct 29, 2009
    Location:
    Switzerland
    #1
    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!
     
  2. macrumors 603

    Joined:
    Aug 9, 2009
    #2
    Compile as Objective-C++?
     
  3. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #3
  4. thread starter macrumors newbie

    Joined:
    Oct 29, 2009
    Location:
    Switzerland
    #4
    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!
     
  5. macrumors 6502

    Joined:
    Apr 24, 2008
    #5
    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.
     
  6. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #6
    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.
     
  7. thread starter macrumors newbie

    Joined:
    Oct 29, 2009
    Location:
    Switzerland
    #7
    OK I'll try and comment it here, if it works!
    Thanks a lot!
     
  8. thread starter macrumors newbie

    Joined:
    Oct 29, 2009
    Location:
    Switzerland
    #8
    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++.
     
  9. macrumors 65816

    Muncher

    Joined:
    Apr 19, 2007
    Location:
    California
    #9
    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.
     
  10. thread starter macrumors newbie

    Joined:
    Oct 29, 2009
    Location:
    Switzerland
    #10
    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.
     
  11. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #11
    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!
     
  12. macrumors 603

    Joined:
    Aug 9, 2009
    #12
    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:

    Code:
    ls -l /dev
    
     
  13. thread starter macrumors newbie

    Joined:
    Oct 29, 2009
    Location:
    Switzerland
    #13

    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?
     
  14. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #14
    Really? I didn't know that.

    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.
     
  15. thread starter macrumors newbie

    Joined:
    Oct 29, 2009
    Location:
    Switzerland
    #15
    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!
     
  16. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #16
    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!
     
  17. macrumors 68000

    Joined:
    Sep 10, 2008
    Location:
    Asheville, NC
    #17
    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.
     
  18. macrumors newbie

    Joined:
    Jan 15, 2015
    #18

    How did you accessed functionalities in the framework from cpp. They are saying everywhere that one can use IOBluetooth framework in cpp but no documentation. How will you scan n connect to the bluetooth device?
    Please help in this. struggling a lot..:(

    Thanku...
     

Share This Page