Esempio n. 1
0
JNIEXPORT void JNICALL
Java_org_iotivity_ca_CaLeClientInterface_caLeStateChangedCallback(JNIEnv *env, jobject obj,
                                                                   jint status)
{
    VERIFY_NON_NULL_VOID(env, TAG, "env is null");
    VERIFY_NON_NULL_VOID(obj, TAG, "obj is null");

    OIC_LOG(DEBUG, TAG, "CaLeClientInterface - Network State Changed");

    if (!gCALEDeviceStateChangedCallback)
    {
        OIC_LOG(ERROR, TAG, "gNetworkChangeCb is null");
        return;
    }

    if (BT_STATE_ON == status) // STATE_ON:12
    {
        CANetworkStatus_t newStatus = CA_INTERFACE_UP;
        CALEClientCreateDeviceList();
        CALEServerCreateCachedDeviceList();

        gCALEDeviceStateChangedCallback(newStatus);
    }
    else if (BT_STATE_OFF == status) // STATE_OFF:10
    {
        CALEClientStopMulticastServer();

        // remove obj for client
        CAResult_t res = CALEClientRemoveAllGattObjs(env);
        if (CA_STATUS_OK != res)
        {
            OIC_LOG(ERROR, TAG, "CALEClientRemoveAllGattObjs has failed");
        }

        res = CALEClientRemoveAllScanDevices(env);
        if (CA_STATUS_OK != res)
        {
            OIC_LOG(ERROR, TAG, "CALEClientRemoveAllScanDevices has failed");
        }

        res = CALEClientRemoveAllDeviceState();
        if (CA_STATUS_OK != res)
        {
            OIC_LOG(ERROR, TAG, "CALEClientRemoveAllDeviceState has failed");
        }

        // remove obej for server
        res = CALEServerRemoveAllDevices(env);
        if (CA_STATUS_OK != res)
        {
            OIC_LOG(ERROR, TAG, "CALEServerRemoveAllDevices has failed");
        }

        CALEClientSetScanFlag(false);

        CANetworkStatus_t newStatus = CA_INTERFACE_DOWN;
        gCALEDeviceStateChangedCallback(newStatus);
    }
}
JNIEXPORT void JNICALL
Java_org_iotivity_ca_CaLeClientInterface_caLeStateChangedCallback(JNIEnv *env, jobject obj,
                                                                   jint status)
{
    VERIFY_NON_NULL_VOID(env, TAG, "env is null");
    VERIFY_NON_NULL_VOID(obj, TAG, "obj is null");

    OIC_LOG_V(DEBUG, TAG, "CaLeClientInterface - Network State Changed : status(%d)", status);

    if (!g_bleDeviceStateChangedCallback)
    {
        OIC_LOG(ERROR, TAG, "gNetworkChangeCb is null");
        return;
    }

    jint state_on = CALEGetConstantsValue(env, CLASSPATH_BT_ADAPTER, "STATE_ON");
    jint state_off = CALEGetConstantsValue(env, CLASSPATH_BT_ADAPTER, "STATE_OFF");
    jint state_turning_off = CALEGetConstantsValue(env, CLASSPATH_BT_ADAPTER, "STATE_TURNING_OFF");

    if (state_on == status) // STATE_ON:12
    {
        CANetworkStatus_t newStatus = CA_INTERFACE_UP;
        CALEClientCreateDeviceList();
        CALEServerCreateCachedDeviceList();

        g_bleDeviceStateChangedCallback(newStatus);
    }
    else if (state_turning_off == status) // BT_STATE_TURNING_OFF:13
    {
        // gatt Device list will be removed.
        // so it is need to create list again when adapter is enabled.
        CAStopLEGattClient();
    }
    else if (state_off == status) // STATE_OFF:10
    {
        CALEClientStopMulticastServer();

        // remove obj for client
        CAResult_t res = CALEClientRemoveAllGattObjs(env);
        if (CA_STATUS_OK != res)
        {
            OIC_LOG(ERROR, TAG, "CALEClientRemoveAllGattObjs has failed");
        }

        res = CALEClientResetDeviceStateForAll();
        if (CA_STATUS_OK != res)
        {
            OIC_LOG(ERROR, TAG, "CALEClientResetDeviceStateForAll has failed");
        }

        // remove obj for server
        res = CALEServerRemoveAllDevices(env);
        if (CA_STATUS_OK != res)
        {
            OIC_LOG(ERROR, TAG, "CALEServerRemoveAllDevices has failed");
        }

        CALEClientSetScanFlag(false);

        CANetworkStatus_t newStatus = CA_INTERFACE_DOWN;
        g_bleDeviceStateChangedCallback(newStatus);
    }
}