本文介绍了Bluecove:重启蓝牙堆栈programmically的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图关闭蓝牙服务,但Bluecove对连接close方法错误(的),我试图做一些变通方法来重新启动服务?我认为,重新启动bluetoot堆栈将解决我的问题。我能做到programmically?我'使用Microsoft蓝牙堆栈。

I'm trying to close bluetooth service, but Bluecove has bug on Connection close method (https://code.google.com/p/bluecove/issues/detail?id=90) and I am trying to do some workaround to restart service. I think restarting bluetoot stack will solve my problem. Can I do it programmically? I'am using microsoft bluetooth stack.

推荐答案

问题解决了这个样子。

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

I restart the application, but firstly shut down bluecove manually.

BlueCoveImpl.shutdown();

如果我只是重新启动应用程序,bluecove关闭,但启动过程中无法初始化蓝牙堆栈。下面是重启方法:

If I only restart application, bluecove shut down, but cannot initialize bluetooth stack during start. Here is restart method:

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);
    }
}

这篇关于Bluecove:重启蓝牙堆栈programmically的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-29 19:00