gpt4 book ai didi

c# - 如何打开蓝牙

转载 作者:搜寻专家 更新时间:2023-11-01 09:26:59 25 4
gpt4 key购买 nike

我正在尝试通过 android 插件在 unity 项目中进行蓝牙通信,一开始我想打开蓝牙。

java代码是这样的

package com.example.unityplugin;

import android.bluetooth.BluetoothAdapter;

public class PluginClass {
public static String testMessage(){
return "I AM WORKING";
}

public static String TurnOnBluetooth(){
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (bluetoothAdapter != null) {
if (!bluetoothAdapter.isEnabled()) {
bluetoothAdapter.enable();
return "BLUETOOTH ON";
} else {
return "WAS ON";
}
}
return "no bluetooth adapter";
}

在 Unity 中非常简单

void Start () {
textMEsh = GetComponent<TextMesh>();
var plugin = new AndroidJavaClass("com.example.unityplugin.PluginClass");
textMEsh.text = plugin.CallStatic<string>("testMessage");
textMEsh.text = plugin.CallStatic<string>("TurnOnBluetooth");
}

因此,应用程序中显示的文本在第一个方法“testMessage”之后更改为“我正在工作”,但随后什么也没有发生,我真的不明白为什么。蓝牙未打开,我从日志中看到以下错误:

I/Unity: AndroidJavaException: java.lang.SecurityException: Need BLUETOOTH ADMIN permission: Neither user 10069 nor current process has android.permission.BLUETOOTH_ADMIN.

我应该如何在 Unity 中正确设置该权限?

最佳答案

这是一个权限错误。您需要在Unity中添加蓝牙权限。

1。转到 <UnityInstallationDirecory>\Editor\Data\PlaybackEngines\AndroidPlayer\Apk , 复制 AndroidManifest.xml文件到您的<ProjectName>Assets\Plugins\Android .

如果<ProjectName>Assets\Plugins\Android还不存在,创建它。 文件夹的拼写区分大小写,必须正确拼写。

2.打开从<ProjectName>Assets\Plugins\Android复制的Manifest文件并添加您的 list 。

为其添加以下权限:

<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

3.保存AndroidManifest修改,Build and Run。

Unity 现在将在最终构建中包含蓝牙权限。

关于c# - 如何打开蓝牙,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49843366/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com