Bluetooth application works on Android 5 api22 but not on Android 6 api 23
-
Hi,
I developed a Bluetooth application on Qt5.5.1 C++ and QML, Android 5 api 22 and everything worked fine.
Now I'm trying to test it on Android 6 api 23 but I'm not able to connect to external Bluetooth device.
This is what I made:- Upgrade to Qt5.7
- Download api 23 SDK
- In project build settings, Android build APK set to Android-23
- In AndroidManifest.xml, Minimum required SDK API 16
- In AndroidManifest.xml, Target SDK API 23
- In AndroidManifest.xml, android.permission.ACCESS_COARSE_LOCATION
- In AndroidManifest.xml, android.permission.ACCESS_FINE_LOCATION
- In AndroidManifest.xml, android.permission.BLUETOOTH
- In AndroidManifest.xml, android.permission.BLUETOOTH_ADMIN
The code used to connect to Bluetooth is:
#if isScan
void blue::startClient(const QBluetoothServiceInfo &remoteService)
#else
void blue::startClient()
#endif
{
if (socket)
return;socket = new QBluetoothSocket(QBluetoothServiceInfo::RfcommProtocol); socket->setPreferredSecurityFlags(QBluetooth::NoSecurity);
#if isScan
socket->connectToService(remoteService,QIODevice::ReadWrite);
#else
QBluetoothAddress indirizzo("00:12:6F:90:6A:DA");#if isAndroid
btUuid = new QBluetoothUuid;
btUuid->fromRfc4122("00000000-0000-0000-0000-000000000000");
socket->connectToService(indirizzo,*btUuid,QIODevice::ReadWrite);
#else
socket->connectToService(indirizzo,1,QIODevice::ReadWrite);
#endif#endif
connect(socket, SIGNAL(readyRead()), this, SLOT(leggiSocket()));
connect(socket, SIGNAL(connected()), this, SLOT(connesso()));
connect(socket, SIGNAL(disconnected()), this, SLOT(disconnesso()));
}When I deploy on an Android 5 device with these settings everything works fine and I get this:
I/InputDispatcher( 906): Delivering touch to (11222): action: 0x0, toolType: 1
D/ViewRootImpl(11222): ViewPostImeInputStage ACTION_DOWN
I/InputDispatcher( 906): Delivering touch to (11222): action: 0x1, toolType: 1
D/libMGplus.so(11222): (null):0 ((null)): Connesso bluetooth...
D/BluetoothUtils(11222): isSocketAllowedBySecurityPolicy start : device null
W/BluetoothAdapter(11222): getBluetoothService() called with no BluetoothManagerCallback
D/BluetoothSocket(11222): connect(), SocketState: INIT, mPfd: {ParcelFileDescriptor: FileDescriptor[51]}
W/System.err(11222): java.io.IOException: bt socket connect failed
W/System.err(11222): at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:402)
W/libMGplus.so(11222): (null):0 ((null)): qt.bluetooth.android: Falling back to workaround.
W/libMGplus.so(11222): (null):0 ((null)): qt.bluetooth.android: Cannot determine RFCOMM service channel.
W/libMGplus.so(11222): (null):0 ((null)): qt.bluetooth.android: Workaround thread invoked.
W/BluetoothAdapter(11222): getBluetoothService() called with no BluetoothManagerCallback
D/BluetoothSocket(11222): connect(), SocketState: INIT, mPfd: {ParcelFileDescriptor: FileDescriptor[52]}When I deploy on an Android 6 device this is the result:
I ViewRootImpl: ViewRoot's Touch Event : ACTION_DOWN
I ViewRootImpl: ViewRoot's Touch Event : ACTION_UP
D libMGplus.so: (null):0 ((null)): Connesso bluetooth...
W BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback
W System.err: java.io.IOException: bt socket connect failed
W System.err: at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:390)
W libMGplus.so: (null):0 ((null)): qt.bluetooth.android: Falling back to workaround.
W libMGplus.so: (null):0 ((null)): qt.bluetooth.android: Using found rfcomm channel 0
W libMGplus.so: (null):0 ((null)): qt.bluetooth.android: Invoke Resulted with error.
W System.err: java.lang.reflect.InvocationTargetException
W System.err: at java.lang.reflect.Method.invoke(Native Method)
W System.err: Caused by: java.io.IOException: Invalid RFCOMM channel: 0
W System.err: at android.bluetooth.BluetoothSocket.<init>(BluetoothSocket.java:189)
W System.err: at android.bluetooth.BluetoothSocket.<init>(BluetoothSocket.java:165)
W System.err: at android.bluetooth.BluetoothDevice.createInsecureRfcommSocket(BluetoothDevice.java:1518)
W System.err: ... 1 more
W libMGplus.so: (null):0 ((null)): qt.bluetooth.android: Workaround failedI find the same result even after the app permissions are manually forced to "Allow" on the phone.
Is a permissions problem? Where do I'm doing wrong?
Many thanks in advance -
QBluetoothAddress indirizzo("00:12:6F:90:6A:DA");
The problem is solved replacing
btUuid = new QBluetoothUuid; btUuid->fromRfc4122("00000000-0000-0000-0000-000000000000"); socket->connectToService(indirizzo,*btUuid,QIODevice::ReadWrite);
with
socket->connectToService(indirizzo,QBluetoothUuid(QBluetoothUuid::SerialPort));