Bluecove : restart bluetooth stack programmatically

时光怂恿深爱的人放手 提交于 2020-01-14 19:30:50

问题


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 bluetooth stack will solve my problem. Can I do it programmatically? I'am using microsoft bluetooth stack.


回答1:


Problem solved in this way.

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

BlueCoveImpl.shutdown();

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


来源:https://stackoverflow.com/questions/16372206/bluecove-restart-bluetooth-stack-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!