NordicSemiconductor / Android-nRF-Mesh-Library

The Bluetooth Mesh Provisioner and Configurator library.
https://www.nordicsemi.com/
BSD 3-Clause "New" or "Revised" License
410 stars 178 forks source link

Get BluetoothLeScannerCompat from different Android Version #103

Closed WangManAndroid closed 5 years ago

WangManAndroid commented 5 years ago

public static BluetoothLeScannerCompat getScanner() { if (mInstance != null) return mInstance; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) return mInstance = new BluetoothLeScannerImplOreo(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) return mInstance = new BluetoothLeScannerImplMarshmallow(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) return mInstance = new BluetoothLeScannerImplLollipop(); return mInstance = new BluetoothLeScannerImplJB(); } in my xiaomi 4>>will get BluetoothLeScannerImplMarshmallow (android 6.0) xiaomi mix 2>> will get BluetoothLeScannerImplOreo( android 8.0)

could you tell me the diffrence of this?
i found xiaomi mix2 can scan provinsioned device easier than xiaomi 4, You can't even scan it on xiammi 4, i dont know the reaseon of this condition , whiling provinsioning , onProvisioningComplete, public void onProvisioningComplete(final ProvisionedMeshNode provisionedMeshNode) { provisionedMeshNode.setIsProvisioned(true); Debuger.d("onProvisioningComplete............"); mMeshNode = provisionedMeshNode; mIsProvisioningComplete = true;

    final Intent intent = new Intent(ACTION_PROVISIONING_STATE);
    intent.putExtra(EXTRA_PROVISIONING_STATE, MeshNodeStates.MeshNodeStatus.PROVISIONING_COMPLETE.getState());
    LocalBroadcastManager.getInstance(this).sendBroadcast(intent);

    mIsProvisioningComplete = true;
    mIsReconnecting = true;
    final Intent intent1 = new Intent(ACTION_IS_RECONNECTING);
    intent1.putExtra(EXTRA_DATA, mIsReconnecting);
    LocalBroadcastManager.getInstance(this).sendBroadcast(intent1);

    //mIsReconnecting.postValue(true);
    mBleMeshManager.setProvisioningComplete(true);
    mBleMeshManager.disconnect();
    mBleMeshManager.refreshDeviceCache();
    mHandler.postDelayed(mReconnectRunnable, 1500); //Added a slight delay to disconnect and refresh the cache
}`

in this place the xiaomi 4 rescan so hard and you can't even scan it on xiammi 4, Is this a compatibility issue?

WangManAndroid commented 5 years ago

When the interface was stuck on connecting , I found that the log was print as follows: onClientRegistered() - status=0 clientIf=6 onClientConnectionState() - status=133 clientIf=6 device=98:12:88:77:00:0B

roshanrajaratnam commented 5 years ago

Hi @WangManAndroid different versions of Android have different Ble scanners. The Bluetooth scanner compat library is a backward compatible library that will give you a scanner instance based on the Android version.

I recommend you try the latest dev branch. Error 133 is a connection timeout. Also newer phones have always been more robust when it comes to reconnecting after provisioning, specially in Android Oreo onwards because it's very rarely you can see error 133. Still there was a bug that was stopping the UI navigating back to the scanning fragment. So it looks like the app was stuck but ideally it found the device and failed to connect and it should have gone back to the scanner fragment. Could you try the latest version in the Dev branch and let me know if you face this?

WangManAndroid commented 5 years ago

ok i`ll try it , Thanks for your reply

WangManAndroid commented 5 years ago

the Dev branch while provisioning cannot see status=133 but while scaning unprovisioned node then connecting it ,maby apare status=133 not every times

WangManAndroid commented 5 years ago

what causes status=133? i found the lower android version easier apare status=133, how to avoid?

roshanrajaratnam commented 5 years ago

It's usually a connection timeout. Sometimes you may find a device during a Bluetooth scan but might fail to connect. This case also happen of a device is turned off during the connection process. Usually it's thrown after about 30 seconds which is the default connection supervision timeout on Android. What you can also do is, when this error occurs, you can check if the status is 133 in the on connection status changed and try to reconnect.

WangManAndroid commented 5 years ago

thank your reply !

WangManAndroid commented 5 years ago

today I found new problems while Discovering services gatt.discoverServices(); @Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { // It's possible here callback> status=133 } in some android phone will heppen For example, some of xiaomi's mobile phones

roshanrajaratnam commented 5 years ago

Yeah this is possible, you just have to work around it in my opinion. Sometimes this depends on the Android version on certain devices. You can also try turning on/off the bluetooth on the device to see if it helps?

WangManAndroid commented 5 years ago

thanks your reply! yes I did like you said, it work! but Is there any other way?

roshanrajaratnam commented 5 years ago

Well the only way around this is to make sure all the bluetooth related communication is done in the UI Thread and add some delays before connecting etc.