获取JNIEnv的方法


static JavaVM *g_JavaVM;


g_JavaVM = android::AndroidRuntime::getJavaVM();


static JNIEnv *GetEnv()

{

int status;

    JNIEnv *envnow = NULL;

    status = g_JavaVM->GetEnv((void **)&envnow, JNI_VERSION_1_4);

    if(status < 0)

    {

        status = g_JavaVM->AttachCurrentThread(&envnow, NULL);

        if(status < 0)

        {

            return NULL;

        }

        g_bAttatedT = true;

    }

    return envnow;

}


static void DetachCurrent()

{

    if(g_bAttatedT)

    {

        g_JavaVM->DetachCurrentThread();

    }

}

从native世界进入java世界.熟悉jni编程的都知道,java到native的桥梁是JNIEnv,我们只要获得一个JNIEnv就能进入到java世界了.突破点就在AndroidRuntime,android::AndroidRuntime::getJavaVM();这个静态方法能够获取一个JavaVM, JavaVM->GetEnv方法能够获得一个JNIEnv了.JNIEnv是和线程相关的,使用前一定记得将其附加到当前进程,也要在适当的时候将其销毁.


  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
获取蓝牙设备的RSSI(Received Signal Strength Indication)可以使用BluetoothGatt的readRemoteRssi()方法。以下是一个简单的C++代码示例,演示如何在Android平台上使用BluetoothGatt读取远程设备的RSSI。 ```c++ #include <jni.h> #include <android/bluetooth/BluetoothGatt.h> #include <android/bluetooth/BluetoothGattCallback.h> #include <android/bluetooth/BluetoothGattCharacteristic.h> #include <android/bluetooth/BluetoothGattDescriptor.h> #include <android/bluetooth/BluetoothGattService.h> #include <android/bluetooth/BluetoothDevice.h> #include <android/bluetooth/IBluetoothGatt.h> #include <android_runtime/AndroidRuntime.h> using namespace android; class MyGattCallback : public BluetoothGattCallback { public: virtual void onReadRemoteRssi(BluetoothGatt* gatt, int rssi, int status) { if (status == BluetoothGatt::GATT_SUCCESS) { // RSSI读取成功,可以在这里处理读取的数值 } else { // RSSI读取失败,可以在这里处理错误 } } }; JNIEXPORT void JNICALL Java_com_example_bluetooth_BluetoothHelper_readRemoteRssi(JNIEnv* env, jobject thiz, jobject device) { sp<BluetoothDevice> btDevice = android_bluez::BluetoothDevice::fromJavaObject(env, device); sp<MyGattCallback> callback = new MyGattCallback(); btDevice->connectGatt(env, callback.get(), true); btDevice->readRemoteRssi(); } ``` 这个示例代码中,我们实现了一个MyGattCallback类,它继承了BluetoothGattCallback。在onReadRemoteRssi()方法中,我们可以处理从远程设备读取的RSSI数值。然后,在Java_com_example_bluetooth_BluetoothHelper_readRemoteRssi()方法中,我们通过BluetoothDevice::connectGatt()方法连接到设备,并调用BluetoothDevice::readRemoteRssi()方法读取远程设备的RSSI。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

asmcvc

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值