javaextensions/bluetooth/bluetoothcommons/bluetoothplatformcontrol/src/btplatformcontrolnative.cpp
changeset 21 2a9601315dfc
child 72 1f0034e370aa
equal deleted inserted replaced
18:e8e63152f320 21:2a9601315dfc
       
     1 /*
       
     2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "javajniutils.h"
       
    20 #include "bluetoothconsts.h"
       
    21 #include "bluetoothplatformcontrol.h"
       
    22 #include "com_nokia_mj_impl_bluetooth_BTPlatformControl.h"
       
    23 #include "logger.h"
       
    24 #include "serviceclasshandler.h"
       
    25 #include "exceptionbase.h"
       
    26 
       
    27 using namespace java::bluetooth;
       
    28 using namespace java::util;
       
    29 
       
    30 /*
       
    31  * Class:     com_nokia_mj_impl_bluetooth_BTPlatformControl
       
    32  * Method:    getBluetoothAddress
       
    33  * Signature: ()Ljava/lang/String;
       
    34  */
       
    35 JNIEXPORT jstring JNICALL
       
    36 Java_com_nokia_mj_impl_bluetooth_BTPlatformControl__1getBluetoothAddress
       
    37 (JNIEnv *aJni, jclass)
       
    38 {
       
    39     JELOG2(EJavaBluetooth);
       
    40     BluetoothPlatformControl* control =
       
    41         BluetoothPlatformControl::getBluetoothPlatformControl();
       
    42 
       
    43     if (!control)
       
    44     {
       
    45         return NULL;
       
    46     }
       
    47 
       
    48     std::wstring *devAddress = control->getBluetoothAddress();
       
    49     jstring result;
       
    50     try
       
    51     {
       
    52         result = java::util::JniUtils::wstringToJstring(aJni, *devAddress);
       
    53     }
       
    54     catch (ExceptionBase ex)
       
    55     {
       
    56         result = NULL;
       
    57     }
       
    58 
       
    59 
       
    60     delete devAddress;
       
    61     delete control;
       
    62 
       
    63     return result;
       
    64 }
       
    65 
       
    66 /*
       
    67  * Class:     com_nokia_mj_impl_bluetooth_BTPlatformControl
       
    68  * Method:    getBluetoothName
       
    69  * Signature: ()Ljava/lang/String;
       
    70  */
       
    71 JNIEXPORT jstring JNICALL
       
    72 Java_com_nokia_mj_impl_bluetooth_BTPlatformControl__1getBluetoothName
       
    73 (JNIEnv *aJni, jclass)
       
    74 {
       
    75     JELOG2(EJavaBluetooth);
       
    76     BluetoothPlatformControl* control =
       
    77         BluetoothPlatformControl::getBluetoothPlatformControl();
       
    78 
       
    79     if (!control)
       
    80     {
       
    81         return NULL;
       
    82     }
       
    83 
       
    84     std::wstring *devName = control->getBluetoothName();
       
    85     jstring result;
       
    86     try
       
    87     {
       
    88         result = java::util::JniUtils::wstringToJstring(aJni, *devName);
       
    89     }
       
    90     catch (ExceptionBase ex)
       
    91     {
       
    92         result = NULL;
       
    93     }
       
    94 
       
    95 
       
    96     delete devName;
       
    97     delete control;
       
    98 
       
    99     return result;
       
   100 }
       
   101 
       
   102 
       
   103 /*
       
   104  * Class:     com_nokia_mj_impl_bluetooth_BTPlatformControl
       
   105  * Method:    getBluetoothDiscoverable
       
   106  * Signature: ()I
       
   107  */
       
   108 JNIEXPORT jint JNICALL
       
   109 Java_com_nokia_mj_impl_bluetooth_BTPlatformControl__1getBluetoothDiscoverable
       
   110 (JNIEnv* , jclass)
       
   111 {
       
   112     JELOG2(EJavaBluetooth);
       
   113     BluetoothPlatformControl* control =
       
   114         BluetoothPlatformControl::getBluetoothPlatformControl();
       
   115 
       
   116     if (!control)
       
   117     {
       
   118         ELOG(EJavaBluetooth,
       
   119              "  JNI::getBluetoothDiscoverable Error in creating BluetoothPlatformControl");
       
   120         return NOT_DISCOVERABLE;
       
   121     }
       
   122 
       
   123     int result = control->getVisibilityMode();
       
   124 
       
   125     delete control;
       
   126 
       
   127     return result;
       
   128 }
       
   129 
       
   130 /*
       
   131  * Class:     com_nokia_mj_impl_bluetooth_BTPlatformControl
       
   132  * Method:    setBluetoothDiscoverable
       
   133  * Signature: (I)Z
       
   134  */
       
   135 JNIEXPORT jboolean JNICALL
       
   136 Java_com_nokia_mj_impl_bluetooth_BTPlatformControl__1setBluetoothDiscoverable
       
   137 (JNIEnv* , jclass, jint aMode)
       
   138 {
       
   139     JELOG2(EJavaBluetooth);
       
   140     BluetoothPlatformControl* control =
       
   141         BluetoothPlatformControl::getBluetoothPlatformControl();
       
   142 
       
   143     if (!control)
       
   144     {
       
   145         ELOG(EJavaBluetooth,
       
   146              "  JNI::setBluetoothDiscoverable Error in creating BluetoothPlatformControl");
       
   147         return false;
       
   148     }
       
   149 
       
   150     int result = control->setDiscoverability(aMode);
       
   151 
       
   152     delete control;
       
   153 
       
   154     return (0==result);
       
   155 }
       
   156 
       
   157 /*
       
   158  * Class:     com_nokia_mj_impl_bluetooth_BTPlatformControl
       
   159  * Method:    isPowerOn
       
   160  * Signature: ()Z
       
   161  */
       
   162 JNIEXPORT jboolean JNICALL
       
   163 Java_com_nokia_mj_impl_bluetooth_BTPlatformControl__1isPowerOn
       
   164 (JNIEnv* , jclass)
       
   165 {
       
   166     JELOG2(EJavaBluetooth);
       
   167     BluetoothPlatformControl* control =
       
   168         BluetoothPlatformControl::getBluetoothPlatformControl();
       
   169 
       
   170     if (!control)
       
   171     {
       
   172         ELOG(EJavaBluetooth,
       
   173              "  JNI::isPowerOn Error in creating BluetoothPlatformControl");
       
   174         return false;
       
   175     }
       
   176     int result = control->getPowerState();
       
   177 
       
   178     delete control;
       
   179 
       
   180     return (BluetoothPlatformControl::BT_YES==result);
       
   181 }
       
   182 
       
   183 /*
       
   184  * Class:     com_nokia_mj_impl_bluetooth_BTPlatformControl
       
   185  * Method:    setPowerOn
       
   186  * Signature: ()Z
       
   187  */
       
   188 JNIEXPORT jboolean JNICALL
       
   189 Java_com_nokia_mj_impl_bluetooth_BTPlatformControl__1setPowerOn
       
   190 (JNIEnv* , jclass)
       
   191 {
       
   192     JELOG2(EJavaBluetooth);
       
   193     BluetoothPlatformControl* control =
       
   194         BluetoothPlatformControl::getBluetoothPlatformControl();
       
   195 
       
   196     if (!control)
       
   197     {
       
   198         ELOG(EJavaBluetooth,
       
   199              "  JNI::setPowerOn Error in creating BluetoothPlatformControl");
       
   200         return false;
       
   201     }
       
   202 
       
   203     // Returns 0 if successful.
       
   204     int result = control->setPower(true);
       
   205 
       
   206     delete control;
       
   207 
       
   208     return (0 == result);
       
   209 }
       
   210 
       
   211 /*
       
   212  * Class:     com_nokia_mj_impl_bluetooth_BTPlatformControl
       
   213  * Method:    promptPowerOn
       
   214  * Signature: (Ljava/lang/String;)Z
       
   215  */
       
   216 JNIEXPORT jboolean JNICALL
       
   217 Java_com_nokia_mj_impl_bluetooth_BTPlatformControl__1promptPowerOn
       
   218 (JNIEnv *aJni, jclass, jstring appName, jboolean aIsStatic)
       
   219 {
       
   220     JELOG2(EJavaBluetooth);
       
   221     std::wstring name;
       
   222 
       
   223     try
       
   224     {
       
   225         name = java::util::JniUtils::jstringToWstring(aJni, appName);
       
   226     }
       
   227     catch (ExceptionBase ex)
       
   228     {
       
   229         name=L"Unknown Application";
       
   230     }
       
   231 
       
   232     BluetoothPlatformControl* control =
       
   233         BluetoothPlatformControl::getBluetoothPlatformControl();
       
   234 
       
   235     if (!control)
       
   236     {
       
   237         ELOG(EJavaBluetooth,
       
   238              "  JNI::promptPowerOn Error in creating BluetoothPlatformControl");
       
   239         return false;
       
   240     }
       
   241 
       
   242     int result;
       
   243     if (aIsStatic)
       
   244     {
       
   245         result = control->askPower(name, true);
       
   246     }
       
   247     else
       
   248     {
       
   249         result = control->askPower(name, false);
       
   250     }
       
   251 
       
   252     delete control;
       
   253 
       
   254     return (BluetoothPlatformControl::BT_YES==result);
       
   255 }
       
   256 
       
   257 /*
       
   258  * Class:     com_nokia_mj_impl_bluetooth_BTPlatformControl
       
   259  * Method:    promptVisibilityChange
       
   260  * Signature: (Ljava/lang/String;)Z
       
   261  */
       
   262 JNIEXPORT jboolean JNICALL
       
   263 Java_com_nokia_mj_impl_bluetooth_BTPlatformControl__1promptVisibilityChange
       
   264 (JNIEnv *aJni, jclass, jstring appName,jboolean aIsStatic)
       
   265 {
       
   266     JELOG2(EJavaBluetooth);
       
   267     std::wstring name;
       
   268 
       
   269     try
       
   270     {
       
   271         name = java::util::JniUtils::jstringToWstring(aJni, appName);
       
   272     }
       
   273     catch (ExceptionBase ex)
       
   274     {
       
   275         name=L"Unknown Application";
       
   276     }
       
   277 
       
   278     BluetoothPlatformControl* control =
       
   279         BluetoothPlatformControl::getBluetoothPlatformControl();
       
   280 
       
   281     if (!control)
       
   282     {
       
   283         ELOG(EJavaBluetooth,
       
   284              "  JNI::promptVisibilityChange Error in creating BluetoothPlatformControl");
       
   285         return false;
       
   286     }
       
   287 
       
   288     int result;
       
   289     if (aIsStatic)
       
   290     {
       
   291         result = control->askDiscoverability(name, true);
       
   292     }
       
   293     else
       
   294     {
       
   295         result = control->askDiscoverability(name, false);
       
   296     }
       
   297 
       
   298     delete control;
       
   299 
       
   300     return (BluetoothPlatformControl::BT_YES==result);
       
   301 }
       
   302 
       
   303 /*
       
   304  * Class:     com_nokia_mj_impl_bluetooth_BTPlatformControl
       
   305  * Method:    getProperty
       
   306  * Signature: (I)Ljava/lang/String;
       
   307  */
       
   308 JNIEXPORT jstring JNICALL
       
   309 Java_com_nokia_mj_impl_bluetooth_BTPlatformControl__1getProperty
       
   310 (JNIEnv *aJni, jclass, jint aPropertyId)
       
   311 {
       
   312     JELOG2(EJavaBluetooth);
       
   313     BluetoothPlatformControl* control =
       
   314         BluetoothPlatformControl::getBluetoothPlatformControl();
       
   315 
       
   316     if (!control)
       
   317     {
       
   318         ELOG(EJavaBluetooth,
       
   319              "  JNI::getProperty Error in creating BluetoothPlatformControl");
       
   320         return NULL;
       
   321     }
       
   322 
       
   323     jstring result;
       
   324 
       
   325     try
       
   326     {
       
   327         result =  java::util::JniUtils::wstringToJstring(aJni,
       
   328                   control->getSystemProperty(aPropertyId));
       
   329     }
       
   330     catch (ExceptionBase ex)
       
   331     {
       
   332         result = NULL;
       
   333     }
       
   334 
       
   335     delete control;
       
   336     return result;
       
   337 }
       
   338 
       
   339 
       
   340 /*
       
   341  * Class:     com_nokia_mj_impl_bluetooth_BTPlatformControl
       
   342  * Method:    setServiceClasses
       
   343  * Signature: (I)I
       
   344  */
       
   345 JNIEXPORT jint JNICALL
       
   346 Java_com_nokia_mj_impl_bluetooth_BTPlatformControl__1setServiceClasses
       
   347 (JNIEnv* /*aJni*/, jclass, jint aServiceClassBits)
       
   348 {
       
   349     JELOG2(EJavaBluetooth);
       
   350     return ServiceClassHandler::setDeviceServiceClass(aServiceClassBits);
       
   351 }
       
   352 
       
   353 
       
   354 /*
       
   355  * Class:     com_nokia_mj_impl_bluetooth_BTPlatformControl
       
   356  * Method:    getBluetoothDeviceClass
       
   357  * Signature: ()I
       
   358  */
       
   359 JNIEXPORT jint JNICALL
       
   360 Java_com_nokia_mj_impl_bluetooth_BTPlatformControl__1getBluetoothDeviceClass
       
   361 (JNIEnv* , jclass)
       
   362 {
       
   363     JELOG2(EJavaBluetooth);
       
   364     BluetoothPlatformControl* control =
       
   365         BluetoothPlatformControl::getBluetoothPlatformControl();
       
   366     if (!control)
       
   367     {
       
   368         ELOG(EJavaBluetooth,
       
   369              "  JNI::getBluetoothDeviceClass Error in creating BluetoothPlatformControl");
       
   370         return 0;
       
   371     }
       
   372 
       
   373     int err = 0;
       
   374     int ret = control->getBluetoothDeviceClass(err);
       
   375 
       
   376     delete control;
       
   377 
       
   378     if (err)
       
   379     {
       
   380         ELOG1(EJavaBluetooth, "  JNI::getBluetoothDeviceClass Error:%d", err);
       
   381         return 0;
       
   382     }
       
   383     return ret;
       
   384 }