Problem stopping thread (I think)

Reply

Join Date: Aug 2008
Posts: 25
Reputation: Dancealot is an unknown quantity at this point 
Solved Threads: 0
Dancealot Dancealot is offline Offline
Light Poster

Problem stopping thread (I think)

 
0
  #1
Sep 11th, 2008
Hi all.
Hope you can help me out with this problem.
I am trying to get a bluetooth finder to stop, and start again.
What I can see is that the first time it stops and then start again, it work. the next time I do it, nothing happens. When I say it works, I mean it finds the devices and lists them. It doesnt do that the second time. I can see that it doesnt stop the first time as my test output keeps spitting out data. Hope someone can help me here. thx in advance.

P.s. this is a modification of another bluetooth code so not my own work, just trying to rewrite it so it fits my need.

Main midlet:

  1.  
  2. /*
  3.  * To change this template, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6.  
  7. import java.io.DataOutputStream;
  8. import java.io.IOException;
  9. import java.util.Vector;
  10. import javax.bluetooth.LocalDevice;
  11. import javax.microedition.io.Connector;
  12. import javax.microedition.io.StreamConnection;
  13. import javax.microedition.midlet.*;
  14. import javax.microedition.lcdui.*;
  15. import org.netbeans.microedition.lcdui.SplashScreen;
  16. import org.netbeans.microedition.lcdui.TableItem;
  17.  
  18. /**
  19.  * @author MMA
  20.  */
  21. public class MainMidlet extends MIDlet implements CommandListener, Runnable {
  22.  
  23.  
  24. private boolean midletPaused = false;
  25. MyFinder mainFinder = new MyFinder(this);
  26.  
  27.  
  28. //<editor-fold defaultstate="collapsed" desc=" Generated Fields ">
  29. private List Menu;
  30. private Form ClientForm;
  31. private TextField textField;
  32. private Form ServerForm;
  33. private StringItem stringItem;
  34. private TableItem tableItem;
  35. private Form ServerList;
  36. private ChoiceGroup choiceGroup;
  37. private SplashScreen SendSplash;
  38. private Command backCommand1;
  39. private Command backCommand;
  40. private Command okCommand1;
  41. private Command okCommand;
  42. private Command exitCommand;
  43. private Command backCommand2;
  44. private Command okCommand2;
  45. private Command Send;
  46. private Command start_searchCommand;
  47. private Command stop_searchCommand;
  48. private Image image1;
  49. //</editor-fold>
  50.  
  51. /**
  52.   * The MainMidlet constructor.
  53.   */
  54. public MainMidlet() {
  55. }
  56.  
  57. //<editor-fold defaultstate="collapsed" desc=" Generated Methods ">
  58. //</editor-fold>
  59.  
  60. //<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">
  61. /**
  62.  * Initilizes the application.
  63.  * It is called only once when the MIDlet is started. The method is called before the <code>startMIDlet</code> method.
  64.  */
  65. private void initialize () {
  66. // write pre-initialize user code here
  67.  
  68. // write post-initialize user code here
  69. }
  70. //</editor-fold>
  71.  
  72. //<editor-fold defaultstate="collapsed" desc=" Generated Method: startMIDlet ">
  73. /**
  74.  * Performs an action assigned to the Mobile Device - MIDlet Started point.
  75.  */
  76. public void startMIDlet () {
  77. // write pre-action user code here
  78. switchDisplayable (null, getMenu ());
  79. // write post-action user code here
  80. }
  81. //</editor-fold>
  82.  
  83. //<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet ">
  84. /**
  85.  * Performs an action assigned to the Mobile Device - MIDlet Resumed point.
  86.  */
  87. public void resumeMIDlet () {
  88. // write pre-action user code here
  89.  
  90. // write post-action user code here
  91. }
  92. //</editor-fold>
  93.  
  94. //<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">
  95. /**
  96.  * Switches a current displayable in a display. The <code>display</code> instance is taken from <code>getDisplay</code> method. This method is used by all actions in the design for switching displayable.
  97.  * @param alert the Alert which is temporarily set to the display; if <code>null</code>, then <code>nextDisplayable</code> is set immediately
  98.  * @param nextDisplayable the Displayable to be set
  99.  */
  100. public void switchDisplayable (Alert alert, Displayable nextDisplayable) {
  101. // write pre-switch user code here
  102. Display display = getDisplay ();
  103. if (alert == null) {
  104. display.setCurrent (nextDisplayable);
  105. } else {
  106. display.setCurrent (alert, nextDisplayable);
  107. }
  108. // write post-switch user code here
  109. }
  110. //</editor-fold>
  111.  
  112. //<editor-fold defaultstate="collapsed" desc=" Generated Method: commandAction for Displayables ">
  113. /**
  114.  * Called by a system to indicated that a command has been invoked on a particular displayable.
  115.  * @param command the Command that was invoked
  116.  * @param displayable the Displayable where the command was invoked
  117.  */
  118. public void commandAction (Command command, Displayable displayable) {
  119. // write pre-action user code here
  120. if (displayable == ClientForm) {
  121. if (command == backCommand1) {
  122. // write pre-action user code here
  123. switchDisplayable (null, getMenu ());
  124. // write post-action user code here
  125. } else if (command == okCommand1) {
  126. // write pre-action user code here
  127. switchDisplayable (null, getServerList ());
  128. // write post-action user code here
  129. }
  130. } else if (displayable == Menu) {
  131. if (command == List.SELECT_COMMAND) {
  132. // write pre-action user code here
  133. MenuAction ();
  134. // write post-action user code here
  135. } else if (command == exitCommand) {
  136. // write pre-action user code here
  137. exitMIDlet ();
  138. // write post-action user code here
  139. }
  140. } else if (displayable == SendSplash) {
  141. if (command == SplashScreen.DISMISS_COMMAND) {
  142. // write pre-action user code here
  143. switchDisplayable (null, getServerList ());
  144. // write post-action user code here
  145. }
  146. } else if (displayable == ServerForm) {
  147. if (command == backCommand) {
  148. // write pre-action user code here
  149. switchDisplayable (null, getMenu ());
  150. // write post-action user code here
  151. } else if (command == okCommand) {
  152. // write pre-action user code here
  153. switchDisplayable (null, getServerForm ());
  154. // write post-action user code here
  155. }
  156. } else if (displayable == ServerList) {
  157. if (command == backCommand2) {
  158. // write pre-action user code here
  159. switchDisplayable (null, getClientForm ());
  160. // write post-action user code here
  161. } else if (command == okCommand2) {
  162. // write pre-action user code here
  163. switchDisplayable (null, getSendSplash ());
  164. // write post-action user code here
  165. } else if (command == start_searchCommand) {
  166. // write pre-action user code here
  167. start_device_search();
  168. // write post-action user code here
  169. } else if (command == stop_searchCommand) {
  170. // write pre-action user code here
  171. stop_device_search();
  172. // write post-action user code here
  173. }
  174. }
  175. // write post-action user code here
  176. }
  177. //</editor-fold>
  178.  
  179.  
  180.  
  181. //<editor-fold defaultstate="collapsed" desc=" Generated Getter: Menu ">
  182. /**
  183.  * Returns an initiliazed instance of Menu component.
  184.  * @return the initialized component instance
  185.  */
  186. public List getMenu () {
  187. if (Menu == null) {
  188. // write pre-init user code here
  189. Menu = new List ("Menu", Choice.IMPLICIT);
  190. Menu.append ("Client", null);
  191. Menu.append ("Server", null);
  192. Menu.addCommand (getExitCommand ());
  193. Menu.setCommandListener (this);
  194. Menu.setSelectedFlags (new boolean[] { false, false });
  195. // write post-init user code here
  196. }
  197. return Menu;
  198. }
  199. //</editor-fold>
  200.  
  201. //<editor-fold defaultstate="collapsed" desc=" Generated Method: MenuAction ">
  202. /**
  203.  * Performs an action assigned to the selected list element in the Menu component.
  204.  */
  205. public void MenuAction () {
  206. // enter pre-action user code here
  207. String __selectedString = getMenu ().getString (getMenu ().getSelectedIndex ());
  208. if (__selectedString != null) {
  209. if (__selectedString.equals ("Client")) {
  210. // write pre-action user code here
  211. switchDisplayable (null, getClientForm ());
  212. // write post-action user code here
  213. } else if (__selectedString.equals ("Server")) {
  214. // write pre-action user code here
  215. switchDisplayable (null, getServerForm ());
  216. // write post-action user code here
  217. }
  218. }
  219. // enter post-action user code here
  220. }
  221. //</editor-fold>
  222.  
  223. //<editor-fold defaultstate="collapsed" desc=" Generated Getter: ClientForm ">
  224. /**
  225.  * Returns an initiliazed instance of ClientForm component.
  226.  * @return the initialized component instance
  227.  */
  228. public Form getClientForm () {
  229. if (ClientForm == null) {
  230. // write pre-init user code here
  231. ClientForm = new Form ("", new Item[] { getTextField () });
  232. ClientForm.addCommand (getBackCommand1 ());
  233. ClientForm.addCommand (getOkCommand1 ());
  234. ClientForm.setCommandListener (this);
  235. // write post-init user code here
  236. }
  237. return ClientForm;
  238. }
  239. //</editor-fold>
  240.  
  241. //<editor-fold defaultstate="collapsed" desc=" Generated Getter: textField ">
  242. /**
  243.  * Returns an initiliazed instance of textField component.
  244.  * @return the initialized component instance
  245.  */
  246. public TextField getTextField () {
  247. if (textField == null) {
  248. // write pre-init user code here
  249. textField = new TextField ("Message:", null, 32, TextField.ANY);
  250. // write post-init user code here
  251. }
  252. return textField;
  253. }
  254. //</editor-fold>
  255.  
  256. //<editor-fold defaultstate="collapsed" desc=" Generated Getter: ServerForm ">
  257. /**
  258.  * Returns an initiliazed instance of ServerForm component.
  259.  * @return the initialized component instance
  260.  */
  261. public Form getServerForm () {
  262. if (ServerForm == null) {
  263. // write pre-init user code here
  264. ServerForm = new Form ("Server", new Item[] { getTableItem (), getStringItem () });
  265. ServerForm.addCommand (getBackCommand ());
  266. ServerForm.addCommand (getOkCommand ());
  267. ServerForm.setCommandListener (this);
  268. // write post-init user code here
  269. }
  270. return ServerForm;
  271. }
  272. //</editor-fold>
  273.  
  274. //<editor-fold defaultstate="collapsed" desc=" Generated Getter: tableItem ">
  275. /**
  276.  * Returns an initiliazed instance of tableItem component.
  277.  * @return the initialized component instance
  278.  */
  279. public TableItem getTableItem () {
  280. if (tableItem == null) {
  281. // write pre-init user code here
  282. tableItem = new TableItem (getDisplay (), "Device:");
  283. // write post-init user code here
  284. }
  285. return tableItem;
  286. }
  287. //</editor-fold>
  288.  
  289. //<editor-fold defaultstate="collapsed" desc=" Generated Getter: stringItem ">
  290. /**
  291.  * Returns an initiliazed instance of stringItem component.
  292.  * @return the initialized component instance
  293.  */
  294. public StringItem getStringItem () {
  295. if (stringItem == null) {
  296. // write pre-init user code here
  297. stringItem = new StringItem ("Message", null);
  298. // write post-init user code here
  299. }
  300. return stringItem;
  301. }
  302. //</editor-fold>
  303.  
  304. //<editor-fold defaultstate="collapsed" desc=" Generated Getter: backCommand ">
  305. /**
  306.  * Returns an initiliazed instance of backCommand component.
  307.  * @return the initialized component instance
  308.  */
  309. public Command getBackCommand () {
  310. if (backCommand == null) {
  311. // write pre-init user code here
  312. backCommand = new Command ("Back", Command.BACK, 0);
  313. // write post-init user code here
  314. }
  315. return backCommand;
  316. }
  317. //</editor-fold>
  318.  
  319. //<editor-fold defaultstate="collapsed" desc=" Generated Getter: backCommand1 ">
  320. /**
  321.  * Returns an initiliazed instance of backCommand1 component.
  322.  * @return the initialized component instance
  323.  */
  324. public Command getBackCommand1 () {
  325. if (backCommand1 == null) {
  326. // write pre-init user code here
  327. backCommand1 = new Command ("Back", Command.BACK, 0);
  328. // write post-init user code here
  329. }
  330. return backCommand1;
  331. }
  332. //</editor-fold>
  333.  
  334. //<editor-fold defaultstate="collapsed" desc=" Generated Getter: okCommand1 ">
  335. /**
  336.  * Returns an initiliazed instance of okCommand1 component.
  337.  * @return the initialized component instance
  338.  */
  339. public Command getOkCommand1 () {
  340. if (okCommand1 == null) {
  341. // write pre-init user code here
  342. okCommand1 = new Command ("Ok", Command.OK, 0);
  343. // write post-init user code here
  344. }
  345. return okCommand1;
  346. }
  347. //</editor-fold>
  348.  
  349. //<editor-fold defaultstate="collapsed" desc=" Generated Getter: okCommand ">
  350. /**
  351.  * Returns an initiliazed instance of okCommand component.
  352.  * @return the initialized component instance
  353.  */
  354. public Command getOkCommand () {
  355. if (okCommand == null) {
  356. // write pre-init user code here
  357. okCommand = new Command ("Ok", Command.OK, 0);
  358. // write post-init user code here
  359. }
  360. return okCommand;
  361. }
  362. //</editor-fold>
  363.  
  364. //<editor-fold defaultstate="collapsed" desc=" Generated Getter: exitCommand ">
  365. /**
  366.  * Returns an initiliazed instance of exitCommand component.
  367.  * @return the initialized component instance
  368.  */
  369. public Command getExitCommand () {
  370. if (exitCommand == null) {
  371. // write pre-init user code here
  372. exitCommand = new Command ("Exit", Command.EXIT, 0);
  373. // write post-init user code here
  374. }
  375. return exitCommand;
  376. }
  377. //</editor-fold>
  378.  
  379.  
  380.  
  381.  
  382.  
  383. //<editor-fold defaultstate="collapsed" desc=" Generated Getter: ServerList ">
  384. /**
  385.  * Returns an initiliazed instance of ServerList component.
  386.  * @return the initialized component instance
  387.  */
  388. public Form getServerList () {
  389. if (ServerList == null) {
  390. // write pre-init user code here
  391. ServerList = new Form ("", new Item[] { getChoiceGroup () });
  392. ServerList.addCommand (getBackCommand2 ());
  393. ServerList.addCommand (getOkCommand2 ());
  394. ServerList.addCommand (getStart_searchCommand ());
  395. ServerList.addCommand (getStop_searchCommand ());
  396. ServerList.setCommandListener (this);
  397. // write post-init user code here
  398. }
  399. return ServerList;
  400. }
  401. //</editor-fold>
  402.  
  403. //<editor-fold defaultstate="collapsed" desc=" Generated Getter: choiceGroup ">
  404. /**
  405.  * Returns an initiliazed instance of choiceGroup component.
  406.  * @return the initialized component instance
  407.  */
  408. public ChoiceGroup getChoiceGroup () {
  409. if (choiceGroup == null) {
  410. // write pre-init user code here
  411. choiceGroup = new ChoiceGroup ("Servers", Choice.MULTIPLE);
  412. choiceGroup.setPreferredSize (-1, -1);
  413. choiceGroup.setLayout (ImageItem.LAYOUT_CENTER | Item.LAYOUT_TOP | Item.LAYOUT_BOTTOM | Item.LAYOUT_VCENTER);
  414. choiceGroup.setSelectedFlags (new boolean[] { });
  415. // write post-init user code here
  416. }
  417. return choiceGroup;
  418. }
  419. //</editor-fold>
  420.  
  421. //<editor-fold defaultstate="collapsed" desc=" Generated Getter: backCommand2 ">
  422. /**
  423.  * Returns an initiliazed instance of backCommand2 component.
  424.  * @return the initialized component instance
  425.  */
  426. public Command getBackCommand2 () {
  427. if (backCommand2 == null) {
  428. // write pre-init user code here
  429. backCommand2 = new Command ("Back", Command.BACK, 0);
  430. // write post-init user code here
  431. }
  432. return backCommand2;
  433. }
  434. //</editor-fold>
  435.  
  436. //<editor-fold defaultstate="collapsed" desc=" Generated Getter: okCommand2 ">
  437. /**
  438.  * Returns an initiliazed instance of okCommand2 component.
  439.  * @return the initialized component instance
  440.  */
  441. public Command getOkCommand2 () {
  442. if (okCommand2 == null) {
  443. // write pre-init user code here
  444. okCommand2 = new Command ("Ok", Command.OK, 0);
  445. // write post-init user code here
  446. //Object finder = new MyFinder(this);
  447. MyFinder finder = new MyFinder(this);
  448. finder.start();
  449. }
  450. return okCommand2;
  451. }
  452. //</editor-fold>
  453.  
  454. //<editor-fold defaultstate="collapsed" desc=" Generated Getter: SendSplash ">
  455. /**
  456.  * Returns an initiliazed instance of SendSplash component.
  457.  * @return the initialized component instance
  458.  */
  459. public SplashScreen getSendSplash () {
  460. if (SendSplash == null) {
  461. // write pre-init user code here
  462. SendSplash = new SplashScreen (getDisplay ());
  463. SendSplash.setTitle ("");
  464. SendSplash.setCommandListener (this);
  465. SendSplash.setImage (getImage1 ());
  466. SendSplash.setText ("Sending message");
  467. SendSplash.setTimeout (2000);
  468. // write post-init user code here
  469. Thread t = new Thread(this);
  470. t.start();
  471. }
  472. return SendSplash;
  473. }
  474. //</editor-fold>
  475.  
  476. //<editor-fold defaultstate="collapsed" desc=" Generated Getter: Send ">
  477. /**
  478.  * Returns an initiliazed instance of Send component.
  479.  * @return the initialized component instance
  480.  */
  481. public Command getSend () {
  482. if (Send == null) {
  483. // write pre-init user code here
  484. Send = new Command ("Send", Command.OK, 0);
  485. // write post-init user code here
  486. }
  487. return Send;
  488. }
  489. //</editor-fold>
  490.  
  491. //<editor-fold defaultstate="collapsed" desc=" Generated Getter: image1 ">
  492. /**
  493.  * Returns an initiliazed instance of image1 component.
  494.  * @return the initialized component instance
  495.  */
  496. public Image getImage1 () {
  497. if (image1 == null) {
  498. // write pre-init user code here
  499. try {
  500. image1 = Image.createImage ("/bluetooth_small.jpg");
  501. } catch (java.io.IOException e) {
  502. e.printStackTrace ();
  503. }
  504. // write post-init user code here
  505. }
  506. return image1;
  507. }
  508. //</editor-fold>
  509.  
  510. //<editor-fold defaultstate="collapsed" desc=" Generated Getter: start_searchCommand ">
  511. /**
  512.  * Returns an initiliazed instance of start_searchCommand component.
  513.  * @return the initialized component instance
  514.  */
  515. public Command getStart_searchCommand () {
  516. if (start_searchCommand == null) {
  517. // write pre-init user code here
  518.  
  519. start_searchCommand = new Command ("Start BT s\u00F8gning", Command.ITEM, 0);
  520. // write post-init user code here
  521.  
  522.  
  523. }
  524. return start_searchCommand;
  525. }
  526. //</editor-fold>
  527.  
  528. //<editor-fold defaultstate="collapsed" desc=" Generated Getter: stop_searchCommand ">
  529. /**
  530.  * Returns an initiliazed instance of stop_searchCommand component.
  531.  * @return the initialized component instance
  532.  */
  533. public Command getStop_searchCommand () {
  534. if (stop_searchCommand == null) {
  535. // write pre-init user code here
  536. stop_searchCommand = new Command ("Stop BT s\u00F8gning", Command.ITEM, 0);
  537. // write post-init user code here
  538. }
  539. return stop_searchCommand;
  540. }
  541. //</editor-fold>
  542.  
  543. /**
  544.   * Returns a display instance.
  545.   * @return the display instance.
  546.   */
  547. public Display getDisplay () {
  548. return Display.getDisplay(this);
  549. }
  550.  
  551. /**
  552.   * Exits MIDlet.
  553.   */
  554. public void exitMIDlet() {
  555. switchDisplayable (null, null);
  556. destroyApp(true);
  557. notifyDestroyed();
  558. }
  559.  
  560. /**
  561.   * Called when MIDlet is started.
  562.   * Checks whether the MIDlet have been already started and initialize/starts or resumes the MIDlet.
  563.   */
  564. public void startApp() {
  565. if (midletPaused) {
  566. resumeMIDlet ();
  567. } else {
  568. initialize ();
  569. startMIDlet ();
  570. }
  571. midletPaused = false;
  572. }
  573.  
  574. /**
  575.   * Called when MIDlet is paused.
  576.   */
  577. public void pauseApp() {
  578. midletPaused = true;
  579. }
  580.  
  581. /**
  582.   * Called to signal the MIDlet to terminate.
  583.   * @param unconditional if true, then the MIDlet has to be unconditionally terminated and all resources has to be released.
  584.   */
  585. public void destroyApp(boolean unconditional) {
  586. }
  587.  
  588. public void removeDevice(int i){
  589. choiceGroup.delete(i);
  590.  
  591. }
  592.  
  593. public void stop_device_search(){
  594. choiceGroup.deleteAll();
  595. mainFinder.stop();
  596. }
  597.  
  598. public void start_device_search(){
  599. choiceGroup.deleteAll();
  600. if(mainFinder.searching == false){
  601. mainFinder.start();
  602. }
  603. }
  604. public void addDevice(MyService ms){
  605. String s = ms.getName() + ":" + ms.getAddress();
  606. choiceGroup.append(s, null);
  607. int count = choiceGroup.size();
  608. System.out.println(count);
  609. }
  610.  
  611. public void run() {
  612. Vector urls = new Vector();
  613.  
  614. // Synchronize so reconcile() won't run
  615. // at the same time.
  616. synchronized(mainFinder) {
  617. int n = choiceGroup.size();
  618. boolean[] flags = new boolean[n];
  619. choiceGroup.getSelectedFlags(flags);
  620. for (int i = 0; i < n; i++) {
  621. if (flags[i] == true) {
  622. String url = mainFinder.getURL(i);
  623. urls.addElement(url);
  624. }
  625. }
  626. }
  627.  
  628. for (int i = 0; i < urls.size(); i++) {
  629. String url = (String)urls.elementAt(i);
  630. try { runURL(url); }
  631. catch (IOException ioe) {
  632. System.out.println("BlueChewMIDlet.run(): " + ioe);
  633. }
  634. }
  635.  
  636. }
  637.  
  638. private void runURL(String url) throws IOException {
  639. StreamConnection sc = null;
  640. DataOutputStream dataOut = null;
  641.  
  642. try {
  643. sc = (StreamConnection)
  644. Connector.open(url);
  645. dataOut = new DataOutputStream(
  646. sc.openOutputStream());
  647. LocalDevice bt = LocalDevice.getLocalDevice();
  648. String from = bt.getFriendlyName() + ":" +
  649. bt.getBluetoothAddress();
  650. dataOut.writeUTF(from);
  651. dataOut.writeUTF(textField.getString());
  652. }
  653. finally {
  654. try { if (dataOut != null) dataOut.close(); }
  655. catch (IOException ioe) {}
  656. try { if (sc != null) sc.close(); }
  657. catch (IOException ioe) {}
  658. }
  659. }
  660. }


Finder:

  1.  
  2.  
  3.  
  4. import java.io.IOException;
  5. import java.util.Vector;
  6.  
  7. import javax.bluetooth.*;
  8.  
  9. public class MyFinder implements DiscoveryListener {
  10.  
  11.  
  12. private MainMidlet midlet;
  13. Vector v_services, v_new_services;
  14. boolean searching, trucking;
  15. int service_search_remaining;
  16.  
  17. // initialiserer my_finder værdierne.
  18. public MyFinder(MainMidlet Tmidlet) {
  19. midlet = Tmidlet;
  20. v_services = new Vector();
  21. v_new_services = new Vector();
  22. service_search_remaining = 0;
  23. searching = false;
  24. trucking = true;
  25. }
  26.  
  27. public void start() {
  28. try {
  29. LocalDevice ld = LocalDevice.getLocalDevice();
  30. DiscoveryAgent da = ld.getDiscoveryAgent();
  31. boolean success = da.startInquiry(DiscoveryAgent.GIAC, this);
  32. searching = true;
  33. }
  34. // Checker om inquiriet kan udføres i den nuværende state. Hvis ikke
  35. // dette er muligt, vil en exception blive genereret.
  36. catch (BluetoothStateException bse)
  37. {
  38. System.out.println("start() threw" + bse);
  39. }
  40. }
  41.  
  42. public void stop(){
  43. try{
  44. trucking = false;
  45. LocalDevice ld = LocalDevice.getLocalDevice();
  46. DiscoveryAgent da = ld.getDiscoveryAgent();
  47. da.cancelInquiry(this);
  48. //searching = false;
  49. }
  50. catch(BluetoothStateException bse){
  51. System.out.println("stop() threw" + bse);
  52. }
  53. }
  54.  
  55. public String getURL(int i) {
  56. MyService bcs = (MyService)v_services.elementAt(i);
  57. String url = bcs.getURL();
  58. return url;
  59. }
  60.  
  61. public void deviceDiscovered(RemoteDevice remotedevice, DeviceClass deviceclass) {
  62. try{
  63. LocalDevice ld = LocalDevice.getLocalDevice();
  64. DiscoveryAgent da = ld.getDiscoveryAgent();
  65.  
  66. UUID[] uuidset = { MyService.BLUECHEW_UUID};
  67. service_search_remaining++;
  68. da.searchServices(null, uuidset, remotedevice, this);
  69. }
  70. catch (IOException ioe){
  71. System.out.println("DeviceDiscovered(): " + ioe);
  72. }
  73. catch (Throwable t) {
  74. System.out.println("DeviceDiscovered(): " + t);
  75. searching = false;
  76. }
  77. }
  78.  
  79. public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
  80. for (int i = 0; i< servRecord.length;i++){
  81. try{
  82. RemoteDevice rd = servRecord[i].getHostDevice();
  83. String name = rd.getFriendlyName(false);
  84. String address = rd.getBluetoothAddress();
  85. String URL = servRecord[i].getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
  86. MyService bcs = new MyService(name, address, URL);
  87. v_new_services.addElement(bcs);
  88. }
  89. catch (BluetoothStateException bse){
  90. System.out.println("ServiceDiscovered(): " + bse);
  91. }
  92. catch (IOException ioe) {
  93. System.out.println("ServiceDiscovered(): " + ioe);
  94. }
  95. }
  96. System.out.println("services" + v_services.size());
  97. }
  98.  
  99. public void serviceSearchCompleted(int TransID, int respCode) {
  100. service_search_remaining --;
  101. if( searching==false && service_search_remaining==0) {
  102. reconcile();
  103.  
  104. try{ Thread.sleep(1000); }
  105. catch(InterruptedException ie){}
  106.  
  107. if(trucking == true)
  108. start();
  109. }
  110. }
  111.  
  112. // opdaterer v_services ved at fjerne elementer der ikke er i v_new_services.
  113. public synchronized void reconcile() {
  114. for(int i=0;i<v_services.size();i++){
  115. MyService bcs = (MyService)v_services.elementAt(i);
  116. if(contains(v_new_services, bcs) == false) {
  117. v_services.removeElement(bcs);
  118. midlet.removeDevice(i);
  119. }
  120. }
  121. System.out.println("Test remove v_service " + v_services.size());
  122.  
  123. for(int i=0;i<v_new_services.size();i++){
  124. MyService bcs = (MyService)v_new_services.elementAt(i);
  125. if((contains(v_services, bcs)) == false){
  126. v_services.addElement(bcs);
  127. midlet.addDevice(bcs);
  128. }
  129. }
  130.  
  131. System.out.println("Test remove v_service " + v_services.size());
  132. v_new_services.removeAllElements();
  133. }
  134.  
  135. private boolean contains(Vector v, Object o){
  136. boolean contained = false;
  137. for(int i=0;i< v.size(); i++){
  138. Object oc = v.elementAt(i);
  139. if(oc.equals(o)){
  140. contained = true;
  141. }
  142. }
  143. return contained;
  144. }
  145.  
  146. public void inquiryCompleted(int arg0) {
  147. searching = false;
  148. }
  149.  
  150.  
  151.  
  152.  
  153. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 1,175
Reputation: stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light 
Solved Threads: 125
Featured Poster
stephen84s's Avatar
stephen84s stephen84s is offline Offline
Veteran Poster

Re: Problem stopping thread (I think)

 
0
  #2
Sep 11th, 2008
Your code is almost a hundred lines, and your indentation is inconsistent, it would have definitely helped if you would have followed one indentation pattern at least consistently
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."

"How to ask questions the smart way ?"
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Problem stopping thread (I think)

 
0
  #3
Sep 11th, 2008
and you don't stop threads. You tell them that they should at their leasure do what's needed to terminate (of course after closing and cleaning up resources, etc.).
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 25
Reputation: Dancealot is an unknown quantity at this point 
Solved Threads: 0
Dancealot Dancealot is offline Offline
Light Poster

Re: Problem stopping thread (I think)

 
0
  #4
Sep 12th, 2008
What I am trying to do, is to "terminate" the Finder, when a specific command is called, and then start it again, when another command is called. So the code in the first part, is where I want to have the "terminate" command. I have tried to implement it in the method void stop_search_command, but it doesnt stop it.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 1,175
Reputation: stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light 
Solved Threads: 125
Featured Poster
stephen84s's Avatar
stephen84s stephen84s is offline Offline
Veteran Poster

Re: Problem stopping thread (I think)

 
0
  #5
Sep 12th, 2008
First indent that monster, also read this on how to use code tags correctly and then repost the RELEVANT section of the code.
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."

"How to ask questions the smart way ?"
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 25
Reputation: Dancealot is an unknown quantity at this point 
Solved Threads: 0
Dancealot Dancealot is offline Offline
Light Poster

Re: Problem stopping thread (I think)

 
0
  #6
Sep 12th, 2008
The part I am trying to solve, is how I stop the search of a device, and how I restart it again. The code for finding a device is this

  1.  
  2. import java.io.IOException;
  3. import java.util.Vector;
  4.  
  5. import javax.bluetooth.*;
  6. import javax.microedition.lcdui.ChoiceGroup;
  7.  
  8. public class MyFinder implements DiscoveryListener {
  9.  
  10.  
  11. private MainMidlet midlet;
  12. Vector v_services, v_new_services;
  13. boolean searching, trucking,no_device;
  14. int service_search_remaining;
  15.  
  16. // initialiserer my_finder værdierne.
  17. public MyFinder(MainMidlet Tmidlet) {
  18. midlet = Tmidlet;
  19. v_services = new Vector();
  20. v_new_services = new Vector();
  21. service_search_remaining = 0;
  22. searching = false;
  23. trucking = true;
  24. no_device = false;
  25. }
  26.  
  27. public void start() {
  28. try {
  29. LocalDevice ld = LocalDevice.getLocalDevice();
  30. DiscoveryAgent da = ld.getDiscoveryAgent();
  31. boolean success = da.startInquiry(DiscoveryAgent.GIAC, this);
  32. searching = true;
  33. }
  34. // Checker om inquiriet kan udføres i den nuværende state. Hvis ikke
  35. // dette er muligt, vil en exception blive genereret.
  36. catch (BluetoothStateException bse)
  37. {
  38. System.out.println("start() threw" + bse);
  39. }
  40. }
  41.  
  42. public void stop(){
  43. try{
  44. trucking = false;
  45. LocalDevice ld = LocalDevice.getLocalDevice();
  46. DiscoveryAgent da = ld.getDiscoveryAgent();
  47. da.cancelInquiry(this);
  48. }
  49. catch(BluetoothStateException bse){
  50. System.out.println("stop() threw" + bse);
  51. }
  52. }
  53.  
  54. public String getURL(int i) {
  55. MyService bcs = (MyService)v_services.elementAt(i);
  56. String url = bcs.getURL();
  57. return url;
  58. }
  59.  
  60. public void deviceDiscovered(RemoteDevice remotedevice, DeviceClass deviceclass) {
  61. try{
  62. LocalDevice ld = LocalDevice.getLocalDevice();
  63. DiscoveryAgent da = ld.getDiscoveryAgent();
  64.  
  65. UUID[] uuidset = { MyService.BLUECHEW_UUID};
  66. service_search_remaining++;
  67. da.searchServices(null, uuidset, remotedevice, this);
  68. }
  69. catch (IOException ioe){
  70. System.out.println("DeviceDiscovered(): " + ioe);
  71. no_device = true;
  72. }
  73. catch (Throwable t) {
  74. System.out.println("DeviceDiscovered()1: " + t);
  75. searching = false;
  76. }
  77. }
  78.  
  79. public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
  80. for (int i = 0; i< servRecord.length;i++){
  81. try{
  82. RemoteDevice rd = servRecord[i].getHostDevice();
  83. String name = rd.getFriendlyName(false);
  84. String address = rd.getBluetoothAddress();
  85. String URL = servRecord[i].getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
  86. MyService bcs = new MyService(name, address, URL);
  87. v_new_services.addElement(bcs);
  88. }
  89. catch (BluetoothStateException bse){
  90. System.out.println("ServiceDiscovered(): " + bse);
  91. }
  92. catch (IOException ioe) {
  93. System.out.println("ServiceDiscovered(): " + ioe);
  94. }
  95. }
  96. System.out.println("services" + v_services.size());
  97. }
  98.  
  99. public void serviceSearchCompleted(int TransID, int respCode) {
  100. service_search_remaining --;
  101. if( searching==false && service_search_remaining==0) {
  102. reconcile();
  103.  
  104. try{ Thread.sleep(1000); }
  105. catch(InterruptedException ie){}
  106.  
  107. if(trucking == true)
  108. start();
  109. }
  110. }
  111.  
  112. // opdaterer v_services ved at fjerne elementer der ikke er i v_new_services.
  113. public synchronized void reconcile() {
  114. for(int i=0;i<v_services.size();i++){
  115. MyService bcs = (MyService)v_services.elementAt(i);
  116. if(contains(v_new_services, bcs) == false) {
  117. v_services.removeElement(bcs);
  118. midlet.removeDevice(i);
  119. }
  120. }
  121.  
  122. for(int i=0;i<v_new_services.size();i++){
  123. MyService bcs = (MyService)v_new_services.elementAt(i);
  124. if((contains(v_services, bcs)) == false){
  125. v_services.addElement(bcs);
  126. midlet.addDevice(bcs);
  127. }
  128. }
  129.  
  130. System.out.println("Test remove v_service " + v_services.size());
  131. v_new_services.removeAllElements();
  132. }
  133.  
  134. private boolean contains(Vector v, Object o){
  135. boolean contained = false;
  136. for(int i=0;i< v.size(); i++){
  137. Object oc = v.elementAt(i);
  138. if(oc.equals(o)){
  139. contained = true;
  140. }
  141. }
  142. return contained;
  143. }
  144.  
  145. public void inquiryCompleted(int arg0) {
  146. searching = false;
  147. }
  148. }

What I am trying to do is to make a command that can stop this thread???, and start it again by calling another command. Hope you can help me here.
thx.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC