Qt5.2 beta bluetooth exams good working on android ?
-
I dont want to wait for Qt 5.3, therefore I found a way to access the Java Bluetooth API via JNI.
http://stefanfrings.de/android_qt/index-en.html
It seems that Qt 5.3 contains a very similar approach. The main difference seems to be that Qt 5.3 recieves data in a separate thread and signals reception with a signal. My class works with polling, the application has to check repeatedly, if something has been received.
-
[quote author="s.frings74" date="1398153323"]I dont want to wait for Qt 5.3, therefore I found a way to access the Java Bluetooth API via JNI.
http://stefanfrings.de/android_bluetooth/index-en.html
It seems that Qt 5.3 contains a very similar approach. The main difference seems to be that Qt 5.3 recieves data in a separate thread and signals reception with a signal. My class works with polling, the application has to check repeatedly, if something has been received.
[/quote]Hi Steve, thank you for sharing your solution!Note that you don't have to wait. The Qt 5.3 beta is already available: http://download.qt-project.org/development_releases/qt/5.3/5.3.0-beta/ (The Release Candidate will be out this coming Thursday)
-
Oops, so I wasted a weekend :-)
At least I learnt a bit about JNI. -
This is what im looking for but cant get it connect to arduino.
Im using QT 5.3 Beta@W/System.err( 9490): java.io.IOException: read failed, socket might closed or timeout, read ret: -1
W/System.err( 9490): at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:569)
W/System.err( 9490): at android.bluetooth.BluetoothSocket.readInt(BluetoothSocket.java:580)
W/System.err( 9490): at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:321)
W/System.err( 9490): at dalvik.system.NativeStart.run(Native Method)
E/Qt ( 9490): ../Mittari/androidrfcomm.cpp:33 (void AndroidRfComm::check(const char*)): Exception in BluetoothSocket.connect()
E/Qt ( 9490): ../Mittari/androidrfcomm.cpp:161 (void AndroidRfComm::connect(const QString&)): Cannot connect to the bluetooth device
D/Qt ( 9490): fontdatabases/basic/qbasicfontdatabase.cpp:238 (static QStringList QBasicFontDatabase::addTTFile(const QByteArray&, const QByteArray&)): FT_New_Face failed with index 0 : 90@ -
I would try to test the connection with a plain Java program. Then you know if there is an issue with the C++ part or a problem in the underlying Java interface.
I would also try to use another program first, for example "Bluetooth SPP" (the older 1.x version, version 2 runs unstable).
By the way: Why do you use my class with Qt 5.3? You dont need it!
-
Because i dont know how to code but i need to make simple android <-> hc-06 bluetooth program that reads numbers from arduino and displays them and can send terminal commands to it.
I made one version with eclipse but it doesnt work on other phones than samsung, dont know why.
And i am thinking that qt is best language fir coding because its multiplatform.
I tried to study bluetooth coding from chat example but its just too complex
-
Yes, the bluetooth classes of Qt are a bit more difficult. The use asynchronous callbacks to indicate received data. Also openng a connection is a bit more difficult. You have to search the arduino first and then you have the object needed to open a connection.
Anyway,check first if you have the same connection problems in Java. Create a new project with an empty activity. then insert the following lines into the end of MainActivity.onCreate() method:
@
try {
BluetoothAdapter adapter=BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> pairedDevices=adapter.getBondedDevices();
Iterator<BluetoothDevice> iterator=pairedDevices.iterator();while (iterator.hasNext()) { BluetoothDevice device=iterator.next(); Log.w(this.getClass().getSimpleName(),"Found "+device.getAddress()+" = "+device.getName()); // Check for my Ardunio either by address or name // if device.getAddress().equals("20:13:11:15:16:08")) { if (device.getName().equals("HC-06")) { BluetoothSocket socket=device.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB")); socket.connect(); InputStream istream=socket.getInputStream(); OutputStream ostream=socket.getOutputStream(); ostream.write("whatever you want to send\n".getBytes()); ostream.flush(); // seems to be not neccessary Thread.sleep(1000); int avail=istream.available(); Log.w(this.getClass().getSimpleName(),"Received "+avail+" bytes"); BufferedReader br = new BufferedReader(new InputStreamReader(istream)); String s=br.readLine(); Log.w(this.getClass().getSimpleName(),"Received: "+s); } }
}
catch(Exception e) {
e.printStackTrace();
}
@Are you sure that you entered the correct device address? Every Bluetooth module has its own unique address.
-
I found a bug in androidrfcomm.cpp:
@
if (address.compare(adr.toString(),Qt::CaseInsensitive)) {...}
@
Must be changed to
@
if (address.compare(adr.toString(),Qt::CaseInsensitive)==0) {...}
@Otherwise it would attempt to connect to the first bluetooth devcie that is reachable. May that's your problem cause.
-
i do have java version working, but i dont want to make my app with java its just too complicated.
but thats just what im trying to do and when i can do it in qt its easy to customise in my way
i made motorcycle speedometer with eclipse but i still have few issues with keeping bluetooth state when changing orientation
-
I just uploaded an improved version of the class.
The isTimeout() function did not work as planned, and you can now connect by device address or name.
I also added an example program. -
[quote author="s.frings74" date="1398174767"]Oops, so I wasted a weekend :-)
At least I learnt a bit about JNI.[/quote]I don't think that's wasted -- learning more about your target platform is always useful. :)