java - How restart bluetooth service in bluecove? -
i have desktop , android applications, connected bluetooth(in desktop side use bluecove 2.1.1 library). desktop application create bluetooth service android application connects it. want add logout functionality both desktop , android sides. example in desktop app user click disconnect, both desktop , android apps reset connections , should able connect again. here bluetoothservice code desktop side:
public class bluetoothservice { private static final string servicename = "btspp://localhost:" // + new uuid("0000110100001000800000805f9b34f7", false).tostring() // + new uuid("0000110100001000800000805f9b34f8", false).tostring() + new uuid("0000110100001000800000805f9b34f9", false).tostring() + ";name=servicename"; private streamconnectionnotifier m_service = null; private listenerthread m_listenerthread; private dataoutputstream m_outstream; public bluetoothservice() { open(); } public void open() { try { assert (m_service == null); m_service = (streamconnectionnotifier) connector.open(servicename); } catch (ioexception e) { e.printstacktrace(); } } public void start() { try { streamconnection connection = (streamconnection) m_service .acceptandopen(); system.out.println("connected"); m_listenerthread = new listenerthread(connection); thread listener = new thread(m_listenerthread); listener.start(); m_outstream = new dataoutputstream(connection.openoutputstream()); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } public void send(string message) { assert (m_listenerthread != null); try { m_outstream.writeutf(message); m_outstream.flush(); system.out.println("sent: " + message); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } public void close() { try { m_service.close(); m_listenerthread.stop(); m_listenerthread = null; m_outstream.close(); m_outstream = null; m_service = null; } catch (ioexception e) { e.printstacktrace(); } } } class listenerthread implements runnable { private datainputstream m_instream; private boolean m_isrunning; public listenerthread(streamconnection connection) { try { this.m_instream = new datainputstream(connection.openinputstream()); m_isrunning = true; } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } ; } public void run() { while (m_isrunning) { try { assert (m_instream != null); if (m_instream.available() > 0) { string message = m_instream.readutf(); system.out.println("received command: " + message); commandmanager.getinstance().parse(message); } } catch (ioexception e) { system.err.println(e.tostring()); } } } public void stop() { m_isrunning = false; try { m_instream.close(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } }
for restarting service do:
bluetoothservice::close(); bluetoothservice::open(); bluetoothservice::start();
but seems cannot reconnect. maybe should create service different name?
Comments
Post a Comment