使用J2ME搜索蓝牙设备,并与搜到的设备通信
2009-09-12 00:00:00 来源:WEB开发网由于代码来自我目前的项目,所以不能给出完整代码(非常多),只给出关键代码,对于一般J2ME程序员绝对看得懂!
首先,给出J2ME搜索蓝牙设备的代码:
1. public void commandAction(Command command, Displayable displayable)
2.
3. {
4.
5. if(command==cmd_Search)
6.
7. {
8.
9. try
10.
11. {
12.
13. LocalDevice localDevice = LocalDevice.getLocalDevice();
14.
15. DiscoveryAgent discoveryAgent = localDevice.getDiscoveryAgent();
16.
17. discoveryAgent.startInquiry(DiscoveryAgent.GIAC,new MyDiscoveryListener(this));
18.
19. }
20.
21. catch (Exception e)
22.
23. {
24.
25. MyClass.MessageBox("搜索出错,找不到蓝牙设备!", display);
26.
27. return;
28.
29. }
30.
31.
32.
33. Menu.removeCommand(cmd_Search);
34.
35. }
36.
37. }
38.
39. }
40.
41.
42.
43.
44.
45. class MyDiscoveryListener implements DiscoveryListener {
46.
47. //用于保存搜索到的设备
48.
49. Vector devices = new Vector();
50.
51. SearchDevices midlet;
52.
53.
54.
55. public MyDiscoveryListener(SearchDevices midlet) {
56.
57. this.midlet = midlet;
58.
59. }
60.
61.
62.
63. public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
64.
65. devices.addElement(btDevice);
66.
67. }
68.
69.
70.
71. public void inquiryCompleted(int discType) {
72.
73. for (int i=0;i<devices.size();i++)
74.
75. {
76.
77. RemoteDevice btDevice = (RemoteDevice)devices.elementAt(i);
78.
79. try
80.
81. {
82.
83. String device_address=btDevice.getBluetoothAddress();//取得蓝牙设备的全球唯一地址
84.
85. }
86.
87. catch (Exception e) {e.printStackTrace();}
88.
89. }
90.
91. }
92.
93. public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {}
94.
95. public void serviceSearchCompleted(int transID, int responseCode) {}
96.
97.
98.
99. }
100.
101.
102.
103. 接下来就是根据搜索所得的蓝牙设备地址来连接设备,并与设备通信:
104.
105. import java.io.DataInputStream;
106.
107. import java.io.DataOutputStream;
108.
109. import java.io.IOException;
110.
111. import javax.bluetooth.ServiceRecord;
112.
113. import javax.microedition.io.Connector;
114.
115. import javax.microedition.io.StreamConnection;
116.
117. import javax.microedition.lcdui.Command;
118.
119. import javax.microedition.lcdui.CommandListener;
120.
121. import javax.microedition.lcdui.Display;
122.
123. import javax.microedition.lcdui.Displayable;
124.
125. import javax.microedition.lcdui.Form;
126.
127. import javax.microedition.lcdui.TextField;
128.
129. import javax.microedition.midlet.MIDlet;
130.
131. import javax.microedition.midlet.MIDletStateChangeException;
132.
133.
134.
135. public class MIDlet_ConnDevice extends MIDlet
136.
137. implements Runnable, CommandListener {
138.
139. private Display display = null;
140.
141.
142.
143.
144.
145. private Form form = new Form("蓝牙 ");
146.
147. //用于输入要发送的消息
148.
149. private TextField tfData = new TextField("请输入发送的消息",
150.
151. "",255,TextField.ANY);
152.
153.
154.
155. private Command cmdExit = new Command("退出", Command.EXIT, 0);
156.
157. private Command cmdSend = new Command("发送", Command.SCREEN, 1);
158.
159. private Command cmdConnect = new Command("连接", Command.SCREEN, 1);
160.
161.
162.
163. //线程运行标志
164.
165. private boolean isRunning = true;
166.
167.
168.
169. StreamConnection client = null;
170.
171. //服务器服务记录
172.
173. ServiceRecord record=null;
174.
175. private DataInputStream dis = null;
176.
177. private DataOutputStream dos = null;
178.
179.
180.
181. public MIDlet_ConnDevice() {
182.
183. super();
184.
185. form.append(tfData);
186.
187. form.addCommand(cmdExit);
188.
189. form.addCommand(cmdConnect);
190.
191. form.setCommandListener(this);
192.
193. }
194.
195. protected void startApp() throws MIDletStateChangeException {
196.
197. display = Display.getDisplay(this);
198.
199. display.setCurrent(form);
200.
201. }
202.
203.
204.
205. protected void pauseApp() {
206.
207. isRunning = false;
208.
209. close();
210.
211. }
212.
213.
214.
215. protected void destroyApp(boolean arg0)
216.
217. throws MIDletStateChangeException {
218.
219. isRunning = false;
220.
221. close();
222.
223. }
224.
225.
226.
227. /**
228.
229. * 处理命令按钮事件
230.
231. */
232.
233. public void commandAction(Command cmd, Displayable d) {
234.
235. if (cmd == cmdExit) {
236.
237. isRunning = false;
238.
239. notifyDestroyed();
240.
241. } else if(cmd == cmdSend) {
242.
243. //发送数据
244.
245. new Thread() {
246.
247. public void run() {
248.
249. if (dos == null) {
250.
251. return;
252.
253. }
254.
255. //把输入的字符串变为数字
256.
257. try {
258.
259.
260.
261. dos.writeUTF(tfData.getString());
262.
263. dos.flush();
264.
265. } catch (IOException e) {
266.
267. form.append("Send error");
268.
269. }
270.
271. }
272.
273. }.start();
274.
275. } else if (cmd == cmdConnect) {
276.
277. //开始服务器线程
278.
279. new Thread(this).start();
280.
281. }
282.
283. }
284.
285.
286.
287. /**
288.
289. * 客户端接收线程
290.
291. */
292.
293. public void run() {
294.
295. isRunning = true;
296.
297. client = null;
298.
299. String device_address;//搜索所得的设备地址
300.
301. String conURL = "btspp://"+device_address+":1";
302.
303. try {
304.
305. client = (StreamConnection)Connector.open(conURL);
306.
307. form.append("Connected!\n");
308.
309. dis = client.openDataInputStream();
310.
311. dos = client.openDataOutputStream();
312.
313. form.removeCommand(cmdConnect);
314.
315. form.addCommand(cmdSend);
316.
317. } catch (Exception e) {
318.
319. form.append("connect error");
320.
321. close();
322.
323. return;
324.
325. }
326.
327. while(isRunning) {
328.
329. try {
330.
331. if(dis.available()>1)
332.
333. {
334.
335. byte[] rec_package=new byte[dis.available()];//强烈建议这里使用byte[]
336.
337. dis.read(rec_package);
338.
339. String str=new String(rec_package);
340.
341. }
342.
343. }
344.
345. catch (Exception e) {
346.
347. form.append("rec error");
348.
349. break;
350.
351. }
352.
353. }
354.
355. close();
356.
357. form.removeCommand(cmdSend);
358.
359. form.addCommand(cmdConnect);
360.
361. }
362.
363.
364.
365. /**
366.
367. * 关闭连接
368.
369. */
370.
371. public void close() {
372.
373. try {
374.
375. if (dis != null) {
376.
377. dis.close();
378.
379. dis = null;
380.
381. }
382.
383.
384.
385. if (dos != null) {
386.
387. dos.close();
388.
389. dos = null;
390.
391. }
392.
393.
394.
395. if (client != null) {
396.
397. client.close();
398.
399. client = null;
400.
401. }
402.
403. } catch (Exception e) {
404.
405. e.printStackTrace();
406.
407. }
408.
409. }
410.
411.
412.
413. }
更多精彩
赞助商链接