Bluetooth in non-blocking application with Qt user interface
Bluetooth in non-blocking application with Qt user interface
- Subject: Bluetooth in non-blocking application with Qt user interface
- From: Lukas Hetzenecker <email@hidden>
- Date: Sat, 20 Feb 2010 17:05:09 +0100
Hello,
I need your help porting my application Series60-Remote [1] to Mac OS X.
It is an open source software suite for S60 mobile phones, which is written in
Python. Its GUI is based on PyQt4 [2] and it uses Bluetooth to connect to a
mobile phone.
I tried to port this application to Mac OS X using lightblue as bluetooth
wrapper for IOBluetooth. I tried to handle the bluetooth connection in a
seperate thread (like in Linux and Windows) [3], but this seems to be not
possible with IOBluetooth.
My next try was to use an asynchronous connection instead and use IOBluetooth
directly. It works great with the attached Objective-C application [4].
But how can I use this wrapper in my Python application?
When I import this extension using objc I already have the Qt event loop
running. But I also have to start the Cocoa event loop (CFRunLoopRun()) if I
want to receive rfcommChannelData and rfcommChannelOpenComplete events?
Is it possible to integrate this event loop in Qt, like it is possible to
integrate the Qt event loop in GTK+?
Did anybody try something like this? Are there any examples?
Thank you for every help,
Lukas Hetzenecker
[1] http://series60-remote.sorceforge.net
[2] http://www.riverbankcomputing.co.uk/software/pyqt/intro
[3] thread.py (attachment)
[4] async.m (attachment)
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import lightblue
import threading
import Foundation
#class Thread(QThread):
#or:
class Thread(threading.Thread):
def run(self):
pool = Foundation.NSAutoreleasePool.alloc().init()
sock = lightblue.socket()
print "connecting"
sock.connect(('00:1B:AF:84:FF:E2', 18))
print "connected"
print "received", sock.recv(1024)
print "finish..."
class Button(QPushButton):
def __init__(self):
QPushButton.__init__(self, "Start thread")
self.connect(self, SIGNAL("clicked()"), self.start)
def start(self):
self.thread = Thread()
self.thread.start()
app = QApplication([])
button = Button()
button.show()
app.exec_()
#import <Foundation/NSObject.h>
#import <IOBluetooth/objc/IOBluetoothDevice.h>
#import <IOBluetooth/objc/IOBluetoothDeviceInquiry.h>
#import <IOBluetooth/objc/IOBluetoothRFCOMMChannel.h>
#import <stdio.h>
@interface ConnectionHandler : NSObject {}
- (void)rfcommChannelOpenComplete:(IOBluetoothRFCOMMChannel*)channel
status:(IOReturn)status;
- (void)rfcommChannelData:(IOBluetoothRFCOMMChannel*)channel
data:(void *)dataPointer
length:(size_t)dataLength;
@end
@implementation ConnectionHandler
- (void)rfcommChannelOpenComplete:(IOBluetoothRFCOMMChannel*)channel
status:(IOReturn)status
{
if( kIOReturnSuccess == status ) {
printf("connection established\n");
[channel writeSync: "110\x02\n" length: 5];
} else {
printf("Connection error!\n");
CFRunLoopStop( CFRunLoopGetCurrent() );
}
}
- (void)rfcommChannelData:(IOBluetoothRFCOMMChannel*)channel
data:(void *)dataPointer
length:(size_t)dataLength
{
printf("received: %s\n", dataPointer);
[channel writeSync: "110\x02\n" length: 5];
CFRunLoopStop( CFRunLoopGetCurrent() );
}
@end
int main( int argc, const char *argv[] )
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *addr_str = @"00:1B:AF:84:FF:E2";
BluetoothDeviceAddress addr;
IOBluetoothNSStringToDeviceAddress( addr_str, &addr );
IOBluetoothDevice *remote_device =
[IOBluetoothDevice withAddress:&addr];
IOBluetoothRFCOMMChannel *chan;
ConnectionHandler *handler = [[ConnectionHandler alloc] init];
[remote_device openRFCOMMChannelAsync:&chan withChannelID:18
delegate: handler];
CFRunLoopRun();
printf("exited!");
[handler release];
[pool release];
return 0;
}
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Bluetooth-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden