tuya / tuya-home-android-sdk-sample-kotlin

This sample is designed to help developers learn more about how to use the features of Tuya Smart Life App SDK for Android.
https://developer.tuya.com/en/docs/app-development/android-app-sdk/featureoverview?id=Ka69nt97vtsfu
MIT License
32 stars 23 forks source link

Sig Mesh connection #37

Closed LiliaRud closed 6 months ago

LiliaRud commented 6 months ago

Hi! Are there plans to implement mesh search and connection? It's presented on the App screenshot but there no implementation.

I have issues implementing Mesh scanning for Android, although no problems with iOS. So it would be good to have some example or help.

taojingGino commented 6 months ago

https://developer.tuya.com/en/docs/app-development/meshsig?id=Ka6km4aeuygxr

@LiliaRud do you need this? if not, Could you please provide specific issues encountered so that we can support them

LiliaRud commented 6 months ago

@taojingGino

I'm using ThingHomeSdk.getThingBlueMeshConfig().newThingBlueMeshSearch(searchBuilder) for search according to documentation. It seems work almost as expected, as there no errors and callback onSearchFinish works properly. However, I never get callback onSearched but my device is in pairing mode and can be discovered by Smart Life app and by my custom iOS app with Tuya SDK.

All permissions (even more) are set:

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
    <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />

    <uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>

On Kotlin my code looks this way

 val iThingBlueMeshSearchListener: IThingBlueMeshSearchListener = object : IThingBlueMeshSearchListener {
      override fun onSearched(deviceBean: SearchDeviceBean) {
        Log.i("MYLOGS", "--------------deviceBean: $deviceBean");
      }

      override fun onSearchFinish() {
        Log.i("MYLOGS", "--------------SEARCH FINISHED");
      }
    }

    val searchBuilder: SearchBuilder = SearchBuilder()
       .setMeshName("out_of_mesh")
      .setTimeOut(300)
      .setThingBlueMeshSearchListener(iThingBlueMeshSearchListener)
      .build()

     mMeshSearch = ThingHomeSdk.getThingBlueMeshConfig().newThingBlueMeshSearch(searchBuilder)
     mMeshSearch?.startSearch()

I also tried to use .setServiceUUIDs(MESH_PROVISIONING_UUID) instead .setMeshName("out_of_mesh") but it change nothing.

taojingGino commented 6 months ago

@LiliaRud pls use this method

https://developer.tuya.com/en/docs/app-development/meshsig?id=Ka6km4aeuygxr#title-12-Connect%20to%20or%20disconnect%20from%20a%20Bluetooth%20mesh%20sub-device

and we have a demo that you can refer to

https://github.com/tuya/tuya-home-android-sdk-sample-kotlin

JiyeHoo commented 6 months ago

// Starts a connection. ThingHomeSdk.getThingSigMeshClient().startClient(mSigMeshBean);

// Starts a connection with a specified scanning period. ThingHomeSdk.getThingSigMeshClient().startClient(mSigMeshBean,searchTime);

startSearch() can only be used to discover sub devices ready for pairing.

LiliaRud commented 6 months ago

@taojingGino @JiyeHoo Sorry, but I don't really get it. I need to discover device which is not connected yet and is in pairing mode. So which method should I use then? As I understand it's startSearch. But as I can see ThingHomeSdk.getThingSigMeshClient also has a startSearch method. Is there some callback to get discovered devices? I can't find it.

And as for this demo, there no SigMesh search implemented (or I can't find it somehow)

JiyeHoo commented 6 months ago

1. Scans for a Bluetooth mesh device ready for pairing.

This startSearch() is only used to discover devices without cloud activation.

mMeshSearch = ThingHomeSdk.getThingBlueMeshConfig().newThingBlueMeshSearch(searchBuilder);
mMeshSearch.startSearch();

2. Reconnect the paired device

Devices that have already been paired do not need to be scanned. The scan function has been implemented in startClient()

// Starts a connection.
ThingHomeSdk.getThingSigMeshClient().startClient(mSigMeshBean);
// Starts a connection with a specified scanning period.
ThingHomeSdk.getThingSigMeshClient().startClient(mSigMeshBean,searchTime);

ThingHomeSdk.getThingSigMeshClient().startSearch() is a scan function used for connections, and I think you may not need it

In addition, our Java demo function is quite comprehensive. You can take a look at it: https://github.com/tuya/tuya-home-android-sdk-sample-java