PDA

View Full Version : complications with connection to local Bluetooth



Blitzor DDD
20th August 2016, 07:16
Hi!

I am trying to connect to local Bluetooth address:


void Widget::connect_to_local_Bluetooth( )
{

qDebug()<<"trying to connect"<<endl;
QBluetoothAddress address("00:1A:7D:DA:71:15");
qDebug()<<address.toString()<<endl;
if(socket.state() != QBluetoothSocket::UnconnectedState)
{
qDebug()<<"socket.state"<<endl;
if(address != socket.peerAddress())//???
{
qDebug()<<"socket.peeradress"<<endl;
socket.disconnectFromService();
socket.connectToService(address, QBluetoothUuid(QBluetoothUuid::SerialPort));
}
//else
// emit connected();
}
else{
qDebug()<<"else case"<<endl;
socket.connectToService(address, QBluetoothUuid(QBluetoothUuid::SerialPort));
}
}
When I compile and run this, it all is working, but when this SLOT is called program writes:

trying to connect

address

else

and then unexpectedly finishes. Why so? What should I correct here? Thank you!

P.S. When I compile and run it on my Android device it does not interrupt, but I have no idea, is he connected to my Desktop or not. local address of Desktop is 00:1A:7D:DA:71:15

here is message from debugger:

///////////////////////////////////////////////////// bluetooth is powered off W/ResourceType(15962): No package identifier when getting name for resource number 0x00000005 I/View (15962): Touch down dispatch to org.qtproject.qt5.android.QtSurface{41e7a900 V.E..... ........ 0,0-1024,695 #5}, event = MotionEvent { action=ACTION_DOWN, id[0]=0, x[0]=175.8283, y[0]=70.87516, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=12082488, downTime=12082488, deviceId=3, source=0x1002 } I/BufferQueue( 136): SurfaceView [queue] fps:0.67, dur:3002.75, max:2887.19, min:115.55 I/BufferQueue( 136): SurfaceView [release] fps:0.66, dur:3009.61, max:2887.47, min:122.15 W/ResourceType(15962): No package identifier when getting name for resource number 0x00000005 I/View (15962): Touch up dispatch to org.qtproject.qt5.android.QtSurface{41e7a900 V.E..... ........ 0,0-1024,695 #5}, event = MotionEvent { action=ACTION_UP, id[0]=0, x[0]=175.8283, y[0]=70.87516, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=12082557, downTime=12082488, deviceId=3, source=0x1002 } D/libA_for_w8.so(15962): ..\A_for_w8\widget.cpp:59 (void Widget::try_connect_to_local_address()): trying to connect D/libA_for_w8.so(15962): D/libA_for_w8.so(15962): ..\A_for_w8\widget.cpp:61 (void Widget::try_connect_to_local_address()): "00:1A:7D:DA:71:15" D/libA_for_w8.so(15962): D/libA_for_w8.so(15962): ..\A_for_w8\widget.cpp:64 (void Widget::try_connect_to_local_address()): socket.state D/libA_for_w8.so(15962): D/BluetoothDevice(15962): mAddress: 00:1A:7D:DA:71:15

// /////////////////////////// bluetooth is on

W/ResourceType(15962): No package identifier when getting name for resource number 0x00000006 I/View (15962): Touch down dispatch to org.qtproject.qt5.android.QtSurface{41dce2d8 V.E..... ........ 0,0-1024,695 #6}, event = MotionEvent { action=ACTION_DOWN, id[0]=0, x[0]=178.82536, y[0]=70.87516, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=12161746, downTime=12161746, deviceId=3, source=0x1002 } I/BufferQueue( 136): SurfaceView new GraphicBuffer needed D/GraphicBuffer(15962): create handle(0x6aa44f78) (w:1024, h:695, f:1) I/BufferQueue( 136): SurfaceView [queue] fps:0.40, dur:5054.98, max:4978.11, min:76.87 I/BufferQueue( 136): SurfaceView [release] fps:0.20, dur:4970.67, max:4970.67, min:4970.67 W/ResourceType(15962): No package identifier when getting name for resource number 0x00000006 I/View (15962): Touch up dispatch to org.qtproject.qt5.android.QtSurface{41dce2d8 V.E..... ........ 0,0-1024,695 #6}, event = MotionEvent { action=ACTION_UP, id[0]=0, x[0]=178.82536, y[0]=70.87516, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=12161842, downTime=12161746, deviceId=3, source=0x1002 } D/libA_for_w8.so(15962): ..\A_for_w8\widget.cpp:59 (void Widget::try_connect_to_local_address()): trying to connect D/libA_for_w8.so(15962): D/libA_for_w8.so(15962): ..\A_for_w8\widget.cpp:61 (void Widget::try_connect_to_local_address()): "00:1A:7D:DA:71:15" D/libA_for_w8.so(15962): D/libA_for_w8.so(15962): ..\A_for_w8\widget.cpp:64 (void Widget::try_connect_to_local_address()): socket.state D/libA_for_w8.so(15962): D/BluetoothDevice(15962): mAddress: 00:1A:7D:DA:71:15

///////////////////////////////////// bluetooth is on too

W/ResourceType(18110): No package identifier when getting name for resource number 0x00000004 I/View (18110): Touch down dispatch to org.qtproject.qt5.android.QtSurface{4202d638 V.E..... ........ 0,0-1024,695 #4}, event = MotionEvent { action=ACTION_DOWN, id[0]=0, x[0]=187.81659, y[0]=65.88166, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=12572050, downTime=12572050, deviceId=3, source=0x1002 } I/BufferQueue( 136): SurfaceView new GraphicBuffer needed D/GraphicBuffer(18110): create handle(0x6c277498) (w:1024, h:695, f:1) I/BufferQueue( 136): SurfaceView [queue] fps:0.39, dur:5184.50, max:5110.70, min:73.81 I/BufferQueue( 136): SurfaceView [release] fps:0.20, dur:5127.71, max:5127.71, min:5127.71 W/ResourceType(18110): No package identifier when getting name for resource number 0x00000004 I/View (18110): Touch up dispatch to org.qtproject.qt5.android.QtSurface{4202d638 V.E..... ........ 0,0-1024,695 #4}, event = MotionEvent { action=ACTION_UP, id[0]=0, x[0]=187.81659, y[0]=65.88166, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=12572146, downTime=12572050, deviceId=3, source=0x1002 } D/libA_for_w8.so(18110): ..\A_for_w8\widget.cpp:59 (void Widget::try_connect_to_local_address()): trying to connect D/libA_for_w8.so(18110): D/libA_for_w8.so(18110): ..\A_for_w8\widget.cpp:61 (void Widget::try_connect_to_local_address()): "00:1A:7D:DA:71:15" D/libA_for_w8.so(18110): D/libA_for_w8.so(18110): ..\A_for_w8\widget.cpp:75 (void Widget::try_connect_to_local_address()): else D/libA_for_w8.so(18110): D/BluetoothAdapter(18110): 1105859416: getState(). Returning 12 D/BluetoothDevice(18110): mAddress: 00:1A:7D:DA:71:15 D/libA_for_w8.so(18110): ..\A_for_w8\widget.cpp:80 (void Widget::try_connect_to_local_address()): D/libA_for_w8.so(18110): D/dalvikvm(18110): threadid=13: interp stack at 0x6c857000 W/BluetoothAdapter(18110): getBluetoothService() called with no BluetoothManagerCallback D/BluetoothSocket(18110): connect(), SocketState: INIT, mPfd: {ParcelFileDescriptor: FileDescriptor[71]} D/BluetoothDevice(18110): mAddress: 00:1A:7D:DA:71:15 W/System.err(18110): java.io.IOException: read failed, socket might closed or timeout, read ret: -1 W/System.err(18110): at android.bluetooth.BluetoothSocket.readAll(Bluetoot hSocket.java:505) W/System.err(18110): at android.bluetooth.BluetoothSocket.readInt(Bluetoot hSocket.java:516) W/System.err(18110): at android.bluetooth.BluetoothSocket.connect(Bluetoot hSocket.java:320) W/System.err(18110): at dalvik.system.NativeStart.run(Native Method) W/libA_for_w8.so(18110): (null):0 ((null)): qt.bluetooth.android: Falling back to workaround. W/libA_for_w8.so(18110): (null):0 ((null)): qt.bluetooth.android: Cannot determine RFCOMM service channel. D/BluetoothDevice(18110): mAddress: 00:1A:7D:DA:71:15 W/libA_for_w8.so(18110): (null):0 ((null)): qt.bluetooth.android: Workaround thread invoked. D/dalvikvm(18110): threadid=13: bye! D/dalvikvm(18110): threadid=13: interp stack at 0x6c857000 W/BluetoothAdapter(18110): getBluetoothService() called with no BluetoothManagerCallback D/BluetoothSocket(18110): connect(), SocketState: INIT, mPfd: {ParcelFileDescriptor: FileDescriptor[70]} D/BluetoothDevice(18110): mAddress: 00:1A:7D:DA:71:15

To sum up:
1) Why it does not interrupt on Android, but does on Desktop ?
2) How can I understand, is there connection between two my devices ?
3) if doesn't what am I doing incorrectly?

anda_skoa
20th August 2016, 11:32
Have you checked the stack trace of the program at the time of the crash?

Cheers,
_

Blitzor DDD
20th August 2016, 13:34
It says:

The inferior stopped because it received a signal from OS.
Signal name: SIGSEGV.
Signal meaning: Segmentation fault.

it crashes on the line socket.connectToService(address, QBluetoothUuid(QBluetoothUuid::SerialPort));

anda_skoa
20th August 2016, 14:04
When a program crashes on a Unix system (assuming that is one due to SIGSEV), then it usually generates a core dump file, which can be analyized with a debugger.

Or the program can be run in the debugger and it will then know the last trace of function calls at the time of the crash.

That line itself doesn't look like it could crash (no pointer or uninitialized data present), so it could be inside the connectToService() method.

But make sure that "this" is a valid object.

Cheers,
_

Blitzor DDD
20th August 2016, 14:20
Why it does not crash, when I run it on Android device?

anda_skoa
20th August 2016, 15:14
There can be many reasons.

For example if "this" is no longer a valid object then the memory "socket" points to could still be accessible depending on the operating system's memory managment.

Which is why the stack trace is important.
If it crashes in some internal function inside connectToService() it could be a bug in the platform's implementation for that function that is not in the implementation of the other platform.
But if the crash is in application code, then it might just be hidden on one of the platforms.

Very often, when crashes seemingly happen in library code it is actually the application code that is wrong.
When access to non-heap members fails, like in this case, it is often that the "this" object has already been destroyed.

Cheers,
_

Blitzor DDD
20th August 2016, 16:00
Look, ich hab noch nicht verstanded..
Why "this" can not be a valid object? If I firslty just push the button, which calls slot try_to_connect_to_local_address after I run my program?
And "this" must be valid!
oder ist das falsch?

Grüße,

anda_skoa
20th August 2016, 16:37
It is likely that "this" is valid, but it can happen that a method is called on an object that has already been deleted.

Then the method will often execute nicely but can crash if access to certain members or in a certain way can no longer be done properly.

For example, the following works nicely for me, despite the object having been deleted


#include <iostream>
#include <string>

using namespace std;

class Test
{
public:
Test() : m_int(0) {}

void noMemberAccess()
{
cout << "noMemberAccess called" << endl;
}

void intReadAccess()
{
cout << "intReadAccess called: value=" << m_int << endl;
}

void stringWriteAccess()
{
m_string = "foo";
cout << "stringWriteAccess called:" << m_string << endl;
}

int m_int;
string m_string;
};

int main()
{
Test *t = new Test;

t->noMemberAccess();
t->intReadAccess();
t->stringWriteAccess();

cout << "deleting object" << endl;

delete t;

t->noMemberAccess();
t->intReadAccess();
t->stringWriteAccess();

return 0;
}

Obvioulsy the memory pointed to by "t" is still OK enough, but that is not in any way guaranteed.
So different systems or different loads on the same system, etc. can lead to different outcomes. It might work nicely but it could crash.

The result is often weird stack traces, like QString::length() appearing to crash, while actually QString::length() was called on a QString member variable of a destroyed object.

Hence it is important to see how deep the stack trace goes into the method you are experiencing a crash with, but also important to verify that the object the call is coming from is still valid,

Cheers,
_

Blitzor DDD
22nd August 2016, 13:02
wow!
it is very unexpectedly for me!)
Thank you for full explanation.