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");
        }
    }
}
相关推荐
安东尼肉店6 小时前
Android compose屏幕适配终极解决方案
android
2501_916007476 小时前
HTTPS 抓包乱码怎么办?原因剖析、排查步骤与实战工具对策(HTTPS 抓包乱码、gzipbrotli、TLS 解密、iOS 抓包)
android·ios·小程序·https·uni-app·iphone·webview
feiyangqingyun7 小时前
基于Qt和FFmpeg的安卓监控模拟器/手机摄像头模拟成onvif和28181设备
android·qt·ffmpeg
用户20187928316712 小时前
ANR之RenderThread不可中断睡眠state=D
android
煤球王子12 小时前
简单学:Android14中的Bluetooth—PBAP下载
android
小趴菜822712 小时前
安卓接入Max广告源
android
齊家治國平天下12 小时前
Android 14 系统 ANR (Application Not Responding) 深度分析与解决指南
android·anr
ZHANG13HAO12 小时前
Android 13.0 Framework 实现应用通知使用权默认开启的技术指南
android
【ql君】qlexcel12 小时前
Android 安卓RIL介绍
android·安卓·ril
写点啥呢12 小时前
android12解决非CarProperty接口深色模式设置后开机无法保持
android·车机·aosp·深色模式·座舱