Unity2017在安卓下获取GPS位置时闪退的解决办法

在Unity使用低功耗蓝牙通信(BLE)需要用到设备的位置信息。但是调用Input.location.Start()程序会闪退。

解决办法:调用原生安卓接口。

参见《Unity2021通过aar调用Android方法》编写一个aar插件gpsplugin,在插件中提供获取GPS位置的接口StartGPSLocation,插件代码如下

复制代码
package com.example.gpsplugin;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;

public class GPSPluginInstance {
    private LocationManager locationManager;
    private LocationListener locationListener;
    private static final int REQUEST_LOCATION_PERMISSION = 1;

    double latitude = 0.0;
    double longitude = 0.0;

    public int Add(int i, int j) {
        return i + j;
    }

    private static Activity unityActivity;

    public static void receiveUnityActivity(Activity tActivity) {
        unityActivity = tActivity;
    }

    public void Toast(String msg) {
        Toast.makeText(unityActivity, msg, Toast.LENGTH_SHORT).show();
    }

    public void StartGPSLocation() {
        // 初始化 LocationManager
        locationManager = (LocationManager) unityActivity.getSystemService(Context.LOCATION_SERVICE);

        // 创建 LocationListener
        if(locationListener == null)
        {
            locationListener = new LocationListener() {
                @Override
                public void onLocationChanged(Location location) {
                    if (location != null) {
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                    }
                }

                @Override
                public void onStatusChanged(String provider, int status, Bundle extras) {
                    // 状态改变时调用
                }

                @Override
                public void onProviderEnabled(String provider) {
                    // 提供者启用时调用
                }

                @Override
                public void onProviderDisabled(String provider) {
                    // 提供者禁用时调用
                }
            };
        }

        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 0, locationListener);
    }
}

AndroidManifest.xml中添加蓝牙和位置权限(注意Android12以上和之前的版本不一样)

复制代码
  <uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
  <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />  
	<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
    <uses-permission android:name="android.permission.BLUETOOTH"/>
    <uses-feature android:name="android.hardware.bluetooth_le" android:required="false"/>
  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
  <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

Unity代码,在使用Input.location.Start()的地方替换成StartGPSLocation就行了

cs 复制代码
public class BLEBluetoothGunMgr : MonoBehaviour
{
    void Start()
    {
        InitializePlugin("com.example.gpsplugin.GPSPluginInstance");
    }

    public void GetPos()
    {   
        //Input.location.Start()
        StartGPSLocation();
    }

    void InitializePlugin(string pluginName)
    {
        unityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        unityActivity = unityClass.GetStatic<AndroidJavaObject>("currentActivity");
        _pluginInstance = new AndroidJavaObject(pluginName);
        if (_pluginInstance == null)
        {
            Debug.Log("Plugin Instance Error");
        }
        _pluginInstance.CallStatic("receiveUnityActivity", unityActivity);
    }
    
    public void Toast()
    {
        if (_pluginInstance != null)
        {
            _pluginInstance.Call("Toast", "Hi,from Unity");
        }
    }

    public void StartGPSLocation()
    {
        if (_pluginInstance != null)
        {
            _pluginInstance.Call("StartGPSLocation");
        }
    }
}
相关推荐
没有了遇见2 小时前
Android 原生定位(替代高德 / 百度等三方定位)<终极版本>
android
2501_916008893 小时前
iOS 抓包工具有哪些?全面盘点主流工具与功能对比分析
android·ios·小程序·https·uni-app·iphone·webview
2501_915921433 小时前
iOS混淆工具实战 视频流媒体类 App 的版权与播放安全保护
android·ios·小程序·https·uni-app·iphone·webview
CYRUS_STUDIO3 小时前
LLVM 全面解析:NDK 为什么离不开它?如何亲手编译调试 clang
android·编译器·llvm
CYRUS_STUDIO3 小时前
静态分析神器 + 动态调试利器:IDA Pro × Frida 混合调试实战
android·逆向
g_i_a_o_giao6 小时前
Android8 binder源码学习分析笔记(一)
android·java·笔记·学习·binder·安卓源码分析
翻滚丷大头鱼6 小时前
android 四大组件—BroadcastReceiver
android
人生游戏牛马NPC1号6 小时前
学习 Android (二十) 学习 OpenCV (五)
android·opencv·学习
2501_916008896 小时前
uni-app iOS 日志与崩溃分析全流程 多工具协作的实战指南
android·ios·小程序·https·uni-app·iphone·webview
文 丰7 小时前
【AndroidStudio】官网下载免安装版,AndroidStudio压缩版的配置和使用
android