gpt4 book ai didi

bluetooth - 蓝湾 : restart bluetooth stack programmatically

转载 作者:行者123 更新时间:2023-12-02 22:30:19 26 4
gpt4 key购买 nike

我正在尝试关闭蓝牙服务,但 Bluecove 在连接关闭方法 ( https://code.google.com/p/bluecove/issues/detail?id=90 ) 上有错误,我正在尝试采取一些解决方法来重新启动服务。我认为重新启动蓝牙堆栈将解决我的问题。我可以通过编程来完成吗?我正在使用微软蓝牙堆栈。

最佳答案

问题就这样解决了。

我重新启动应用程序,但首先手动关闭bluecove。

BlueCoveImpl.shutdown();

如果我只重新启动应用程序,bluecove 会关闭,但无法在启动期间初始化蓝牙堆栈。重启方法如下:

public static void restartApplication(Runnable runBeforeRestart)
throws IOException
{
try
{
// java binary
String java = System.getProperty("java.home") + "/bin/java";
// vm arguments
List<String> vmArguments = ManagementFactory.getRuntimeMXBean()
.getInputArguments();
StringBuffer vmArgsOneLine = new StringBuffer();
for (String arg : vmArguments)
{
// if it's the agent argument : we ignore it otherwise the
// address of the old application and the new one will be in
// conflict
if (!arg.contains("-agentlib"))
{
vmArgsOneLine.append(arg);
vmArgsOneLine.append(" ");
}
}
// init the command to execute, add the vm args
final StringBuffer cmd = new StringBuffer("\"" + java + "\" "
+ vmArgsOneLine);

// program main and program arguments
String[] mainCommand = System.getProperty(SUN_JAVA_COMMAND).split(
" ");
// program main is a jar
if (mainCommand[0].endsWith(".jar"))
{
// if it's a jar, add -jar mainJar
cmd.append("-jar " + new File(mainCommand[0]).getPath());
}
else
{
// else it's a .class, add the classpath and mainClass
cmd.append("-cp \"" + System.getProperty("java.class.path")
+ "\" " + mainCommand[0]);
}
// finally add program arguments
for (int i = 1; i < mainCommand.length; i++)
{
cmd.append(" ");
cmd.append(mainCommand[i]);
}

// execute the command in a shutdown hook, to be sure that all the
// resources have been disposed before restarting the application
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run()
{
try
{
Runtime.getRuntime().exec(cmd.toString());
}
catch (IOException e)
{
e.printStackTrace();
}
}
});
// execute some custom code before restarting
if (runBeforeRestart != null)
{
runBeforeRestart.run();
}
// at first shut down BlueCove manually
BlueCoveImpl.shutdown();

System.exit(0);
}
catch (Exception e)
{
// something went wrong
throw new IOException(
"Error while trying to restart the application", e);
}
}

关于bluetooth - 蓝湾 : restart bluetooth stack programmatically,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16372206/

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