đồ án tốt nghiệp tìm hiểu công nghệ bluetooth và viết ứng dụng minh họa

64 637 0
đồ án tốt nghiệp tìm hiểu công nghệ bluetooth và viết ứng dụng minh họa

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

1 NHẬN XÉT CỦA GVHD SVTH: Đặng Quốc Nghi LỜI CÁM ƠN Em xin bày tỏ lòng biết ơn chân thành đến thầy Nguyễn Đình Thuân, người tận tình hướng dẫn, giúp đỡ em suốt thời gian thực luận văn Em xin chân thành cám ơn quý Thầy cô Khoa Công nghệ thông tin, trường Đại học Nha Trang tận tình giảng dạy tạo điều kiện cho em thực tốt luận văn Mặc dù cố gắng, song chắn luận văn không khỏi thiếu sót Em mong nhận bảo góp ý q thầy bạn SVTH: Đặng Quốc Nghi LỜI MỞ ĐẦU Ngày nay, xã hội phát triển mạnh mẽ, kỹ thuật ngày đại nên nhu cầu trao đổi thơng tin, giải trí, nhu cầu điều khiển thiết bị từ xa… ngày cao Và hệ thống dây cáp phức tạp lại đáp ứng tốt nhu cầu này, khu vực chật hẹp, nơi xa xôi, phương tiện vận chuyển… Vì cơng nghệ khơng dây đời phát triển mạnh mẽ, tạo nhiều thuận lợi cho người đời sống ngày Kỹ thuật không dây phục vụ nhiều nhu cầu khác người, từ làm việc, học tập đến giải trí chơi game, xem phim, nghe nhạc… Với nhu cầu đa dạng phức tạp đó, kỹ thuật không dây đưa nhiều chuẩn với đặc điểm kỹ thuật khác để phù hợp với nhu cầu, mục đích khả người sử dụng IrDA, WLAN với chuẩn 802.11, ZigBee, OpenAir, UWB, Bluetooth… Mỗi chuẩn kỹ thuật có ưu, khuyết điểm riêng nó, Bluetooth dần lên kỹ thuật không dây tầm ngắn có nhiều ưu điểm, thuận lợi cho thiết bị di động Với tổ chức nghiên cứu đông đảo, đại số lượng nhà sản xuất hỗ trợ kỹ thuật Bluetooth vào sản phẩm họ ngày tăng, Bluetooth dần lan rộng khắp giới, xâm nhập vào lĩnh vực thiết bị điện tử tương lai thiết bị điện tử hỗ trợ kỹ thuật Xuất phát từ lý trên, em thực đề tài “TÌM HIỂU CƠNG NGHỆ BLUETOOTH VÀ VIẾT ỨNG DỤNG MINH HỌA” Mục tiêu đề tài tìm hiểu cơng nghệ Bluetooth xây dựng chương trình điều khiển máy tính điện thoại di động thơng qua Bluetooth để minh họa hoạt động kỹ thuật này… Các nội dung đề tài bao gồm:  Tìm hiểu hoạt động kỹ thuật Bluetooth  Tìm hiểu hệ điều hành Symbian series 60  Xây dựng ứng dụng minh họa SVTH: Đặng Quốc Nghi MỤC LỤC Chương 1: CÔNG NGHỆ BLUETOOTH 1.1 Bluetooth gì? 1.2 Các đặc điểm Bluetooth 1.3 Một số khái niệm dùng công nghệ Bluetooth 1.3.1 Master Unit 1.3.2 Slaver Unit 1.3.3 Piconet 1.3.4 Scatternet 10 1.4 Bluetooth Radio 11 1.4.1 Ad-hoc Radio Connectivity 11 1.4.2 Kiến trúc hệ thống Bluetooth Radio 12 1.5 Kĩ thuật trải phổ nhảy tần công nghệ Bluetooth 14 1.5.1 Khái niệm trải phổ công nghệ không dây 14 1.5.2 Kĩ thuật nhảy tần số công nghệ Bluetooth 15 1.6 Cơ chế truyền sửa lỗi 16 1.7 Vấn đề sử dụng lượng Bluetooth 17 1.8 Vấn đề an toàn bảo mật Bluetooth 22 1.8.1 An toàn bảo mật Bluetooth 22 1.8.2 Các giải pháp an toàn bảo mật 23 1.9 Ưu nhược điểm Bluetooth 24 1.9.1 Ưu điểm 24 1.9.2 Nhược điểm 25 1.10 So sánh Bluetooth với số công nghệ không dây khác 25 Chương 2: HỆ ĐIỀU HÀNH SYMBIAN VÀ LẬP TRÌNH GIAO TIẾP BLUETOOTH TRÊN SYMBIAN VỚI JAVA 28 2.1 Khái niệm hệ điều hành Symbian 28 2.2 Đặc điểm hệ điều hành Symbian 28 SVTH: Đặng Quốc Nghi 2.3 Kiến trúc hệ điều hành Symbian 29 2.3.1 Symbian OS kernel 29 2.3.2 Middleware 29 2.3.3 Application Engine 30 2.3.4 User Interface framework 30 2.3.5 Kĩ thuật đồng - Synchronization technology 30 2.3.6 Java vitual machine implementation 30 2.4 Giới thiệu hệ Series 60 31 2.5 Lập trình ứng dụng cho Symbian 32 2.6 Lập trình giao tiếp Bluetooth Symbian với Java 32 2.6.1 Tổng quan Bluetooth API 32 2.6.2 Phân nhóm hàm Bluetooth API 33 2.6.3 Quan hệ nhóm Bluetooth API 34 2.6.4 Bluetooth socket 35 2.6.5 Mở cấu hình Bluetooth socket 36 2.6.6 DiscoveryListener 37 2.6.7 ServiceRecord 37 2.6.8 DiscoveryAgent 37 2.6.9 LocalDevice 37 Chương 3: PHÂN TÍCH VÀ XÂY DỰNG ỨNG DỤNG 38 3.1 Khảo sát trạng 38 3.2 Phân tích xác định yêu cầu 38 3.3 Xây dựng ứng dụng 39 3.3.1 Xây dựng chương trình server 40 3.3.2 Xây dựng chương trình client 42 TỔNG KẾT 48 TÀI LIỆU THAM KHẢO 51 PHỤ LỤC 52 SVTH: Đặng Quốc Nghi Chƣơng 1: CÔNG NGHỆ BLUETOOTH 1.1 Bluetooth gì? Bluetooth cơng nghệ khơng dây cho phép thiết bị điện, điện tử giao tiếp với khoảng cách ngắn sóng vơ tuyến qua băng tần chung ISM (Industrial, Scientific, Medical) dãy tầng 2.40- 2.48 GHz Đây dãy băng tầng không cần đăng ký dành riêng để dùng cho thiết bị không dây công nghiệp, khoa học, y tế Bluetooth thiết kế nhằm mục đích thay dây cáp, kết nối vô tuyến thiết bị điện tử lại với cách thuận lợi với giá thành rẻ Khi kích hoạt, Bluetooth tự động định vị thiết bị khác có chung cơng nghệ vùng xung quanh bắt đầu kết nối với chúng Nó định hướng sử dụng cho việc truyền liệu lẫn tiếng nói 1.2 Các đặc điểm Bluetooth - Tiêu thụ lượng thấp, cho phép ứng dụng nhiều loại thiết bị, bao gồm thiết bị cầm tay - Giá thành thấp - Khoảng cách giao tiếp cho phép:  Khoảng cách hai thiết bị đầu cuối lên đến 10m ngồi trời 5m tịa nhà  Khoảng cách thiết bị đầu cuối Access point lên tới 100m ngồi trời 30m tịa nhà - Bluetooth sử dụng dãy băng tần không cần đăng ký 2.4 GHz băng tần ISM Tốc độ truyền liệu đạt tới mức tối đa Mbps (do sử dụng tần số cao) mà thiết bị không cần phải thấy trực tiếp (light-of-sight requirements) - Dễ dàng việc phát triển ứng dụng: Bluetooth kết nối ứng dụng với ứng dụng khác thơng qua chuẩn “Bluetooth profiles”, độc lập phần cứng hệ điều hành sử dụng - Bluetooth dùng giao tiếp liệu tiếng nói: có kênh để truyền tiếng nói kênh để truyền liệu mạng cá nhân - An tồn bảo mật: tích hợp với xác nhận mã hóa (build in authentication and encryption) - Tính tương thích cao, nhiều nhà sản xuất phần cứng phần mềm hỗ trợ SVTH: Đặng Quốc Nghi Một số khái niệm dùng công nghệ Bluetooth 1.3.1 Master Unit Là thiết bị Piconet, Master thiết lập đồng hồ đếm xung kiểu bước nhảy (hopping) để đồng tất thiết bị piconet mà quản lý, thường thiết bị chuyển đổi liệu Master định số kênh truyền thơng Mỗi Piconet có kiểu hopping 1.3.2 Slaver Unit Là tất thiết bị cịn lại piconet, thiết bị khơng Master phải Slave Tối đa Slave dạng Active 255 Slave dạng Parked (Inactive) Piconet Có dạng Slave Piconet: 1.3  Active: Slave hoạt động, có khả trao đổi thơng tin với Master Slave Active khác Piconet Các thiết bị trạng thái phân biệt thông qua địa MAC (Media Access Control) hay AMA (Active Member Address) số gồm bit Nên Piconet có tối đa thiết bị trang thái (1 cho Master cho Slave)  Standby: Standby dạng inactive, thiết bị trạng thái khơng trao đổi liệu, sóng radio khơng có tác động lên, cơng suất giảm đến tối thiểu để tiết kiệm lượng, thiết bị khơng có khả dò mã truy cập Có thể coi thiết bị nằm ngồi vùng kiểm soát Master  Parked: dạng inactive, thiết bị Piconet thường xuyên đồng với Piconet, khơng có địa MAC Chúng trạng thái "ngủ" Master gọi dậy tín hiệu "beacon" (tín hiệu báo hiệu) Các thiết bị trạng thái Packed đánh địa thông qua địa PMA (Packed Member Address) Đây số bits để phân biệt packed Slave với có tối đa 255 thiết bị trạng thái Piconet 1.3.3 Piconet Picotnet tập hợp thiết bị kết nối thơng qua kỹ thuật Bluetooth theo mơ hình ad-hoc (đây kiểu mạng thiết lập cho nhu cầu truyền liệu hành tức thời, tốc độ nhanh kết nối tự động huỷ sau truyền xong) Trong Piconet có thiết bị Master Đây thường thiết bị tạo kết nối, có vai trị định số kênh truyền thông thực đồng thành phần Piconet, thiết bị lại Slave Đó thiết bị gửi yêu cầu đến Master Hai Slave muốn thực liên lạc phải thông qua Master SVTH: Đặng Quốc Nghi chúng không kết nối trực tiếp với Master đồng Slave thời gian tần số Các mơ hình Piconet:  Piconet có Slave:  Piconet gồm nhiều Slave: Giới hạn thiết bị Piconet (3 bit MAC cho thiết bị) Tất thiết bị Bluetooth ngang hàng mang chức xác định Tuy nhiên thành lập Piconet, thiết bị đóng vai Master để đồng tần số thời gian truyền phát thiết bị khác làm Slave Có trạng thái thiết bị Bluetooth piconet: - Inquiring device (inquiry mode): Thiết bị phát tín hiệu tìm thiết bị Bluetooth khác - Inquiry scanning device (inquiry scan mode): Thiết bị nhận tín hiệu inquiry inquiry device trả lời - Paging device (page mode): Thiết bị phát tín hiệu yêu cầu kết nối với thiết bị inquiry từ trước - Page scanning device (page scan mode): Thiết bị nhận yêu cầu kết nối từ paging device trả lời Một Piconet tạo cách:  Có Master rồi, Master thực Paging để kết nối với Slave  Một Unit (Master hay Slave) lắng nghe tín hiệu (code) mà thiết bị truy cập SVTH: Đặng Quốc Nghi  Khi có chuyển đổi vai trị Master Slave  Khi có Unit chuyển sang trang thái Active Để thiết lập kết nối mới, tiến trình INQUIRY hay PAGE bắt đầu Tiến trình Inquiry cho phép Unit phát Unit khác tầm hoạt động với địa đồng hồ chúng Tiến trình Paging thực tạo kết nối Kết nối thực thiết bị mang địa Bluetooth Unit thiết lập kết nối phải thực tiến trình paging tự động trở thành Master kết nối Trong tiến trình paging, áp dụng vài chiến lược paging Có chiến lược paging bắt buộc tất thiết bị Bluetooth phải hỗ trợ, chiến lược dùng Unit gặp lần đầu tiên, trường hợp tiến trình paging theo sau tiến trình inquiry Hai Unit sau kết nối nhờ dùng chiến lược bắt buộc này, sau chọn chiến lược paging khác Sau thủ tục Paging (PAGE), Master thăm dò Slave cách gửi packet POLL thăm dò hay packet NULL rỗng theo Slave yêu cầu Chỉ có Master gửi tín hiệu POLL cho Slave, ngược lại khơng có Q trình truy vấn tạo kết nối Các vai trò thiết bị Piconet là:  Stand by: Khơng làm  Inquiry: Tìm thiết bị vùng lân cận  Paging: Kết nối với thiết bị cụ thể  Connecting: Nhận nhiệm vụ Khi thiết bị tạo paging muốn tạo kết nối tầng trên, gửi yêu cầu kết nối host theo nghi thức LMP (Link Manament Protocol) Khi Unit quản lý host nhận thơng điệp, thông báo cho host biết kết nối Thiết bị từ xa chấp nhận (gửi thơng điệp chấp nhận theo nghi thức LMP) không chấp nhận kết nối (gửi thông điệp không chấp nhận theo nghi thức LMP) Khi thiết bị không yêu cầu thủ tục thiết lập liên kết từ xa cả, gửi thơng điệp "thiết lập hồn thành" Thiết bị nhận yêu cầu từ thiết bị khác Khi thiết bị khác sẵn sàng tạo liên kết, gửi SVTH: Đặng Quốc Nghi 10 thơng điệp "thiết lập hồn thành" Sau thiết bị trao đổi packet kênh logic khác với LMP 1.3.4 Scatternet Là hay nhiều Piconet độc lập không đồng bộ, Piconet kết hợp lại truyền thơng với Một thiết bị vừa Master Piconet này, vừa Slave Piconet khác Vai trò thiết bị Piconet khơng cố định, có nghĩa thay đổi từ Master thành Slave ngược lại, từ Slave thành Master Ví dụ Master khơng đủ khả cung cấp tài nguyên phục vụ cho Piconet chuyển quyền cho Slave khác giàu tài nguyên hơn, mạnh hơn, piconet Clock kiểu Hopping đồng sẵn Có cách hình thành Scatternet:  Piconet cử Slave làm Slave Piconet (các Piconet độc lập với không đồng bộ) Slave phân chia time slots (TS), vài TS Piconet này, vài TS Piconet  Một Slave Piconet trở thành Master Piconet khác Cũng cách chia TS cách Cách cho phép Piconet đồng clock (xung nhịp) kiểu hopping (kiểu nhảy tần số) SVTH: Đặng Quốc Nghi 50 - Xây dựng ứng dụng dạng framework cho phép lập trình viên khác thêm vào plugin giúp mở rộng tính chương trình - Khai thác chức camera: lưu thành phim, dùng webcam cho chương trình chat - Mở rộng phạm vi ứng dụng sang thiết bị di động hãng khác có sử dụng Symbian hỗ trợ Bluetooth SVTH: Đặng Quốc Nghi 51 TÀI LIỆU THAM KHẢO 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 Jan Beutel, Oliver Kasten, Matthias Ringwald, Frank Siegemund, Lothar Thiele, Bluetooth Smart Nodes for Mobile Ad-hoc Networks, Swiss National Science Foundation, 2003 Nupur Mittal, Bluetooth Technology Models and Future, Exforsys Inc, 2005 Charlie White, Bluetooth: Past, Present and Future, CEN talks with Mike Foley, Executive Director, Bluetooth SIG, Digital Media Online, 2005 Forum Nokia – Bluetooth Technology Overview – Nokia, 2003 Atmel Corporation– The Bluetooth™ Wireless Technology White Paper, 2000 Sil Janssens, Preliminary study:BLUETOOTH SECURITY, 2004 Jahanzeb Khan, Anis Khwaja, Building Secure Wireless Networks with 802.11, Wiley Publishing, Inc., Indianapolis, Indiana, 2003 Steven Vittitoe, Bluetooth Security, SANS Institute 2004 By Michelle Man, BLUETOOTH AND WI-FI, Socket Communications, Inc., 2002 Pico Communications, A Comparison of Bluetooth™ and Wi-Fi™ (802.11b), Pico Communications, 2001 Dr Peter Driessen, Bluetooth Evaluation Project, BlueSuit Research, 2001 Bluetooth– Specification of the Bluetooth System, Bluetooth, 2004 Forum Nokia – Developer Platform 1.0 for Series 60: Getting Started with C++ Application Development - Nokia, 2003 Forum Nokia – Series 60 Application Framework Handbook - Nokia, 2002 Forum Nokia – Introduction to Series 60 Applications for C++ Developers Nokia, 2002 Forum Nokia – Setting Up and Using Bluetooth Hardware with Development Tools - Nokia, 2004 Forum Nokia – Designing Bluetooth Applications in C++- Nokia, 2004 Nokia – Series 60 SDK Help – Nokia Series 60 SDK 1.2 Forum Nokia – Using Contact APIs – Nokia, 2004 Symbian, http://www.symbian.com Forum Nokia, http://www.forum.nokia.com http://www.bluetooth.com http://www.bluetooth.org Series 60, www.Series60.com SVTH: Đặng Quốc Nghi 52 PHỤ LỤC  Một số phƣơng thức chƣơng trình server  public Bluetooth(RemoteServer rs) { try { this.rs = rs; local = LocalDevice.getLocalDevice(); local.setDiscoverable(DiscoveryAgent.GIAC); server = (StreamConnectionNotifier) Connector.open(url); } catch (Exception e) { rs.displayMessage("Exception Occured, \nMake sure bluetooth adapter is connected, \nPlease restart\nException decription:\n" + e.toString()); } }  public void listen() { try { Thread t = new Thread(this); t.start(); } catch (Exception e) { System.out.println("Exception " + e.toString()); } }  public void close() { try { conn.close(); } catch (Exception e) { System.out.println("Exception " + e.toString()); } }  public void run() { try { boolean listening = true; boolean firstTime = true; String cmd = ""; while (true) { SVTH: Đặng Quốc Nghi 53 try { if (listening) { conn = server.acceptAndOpen(); /* - Client Connected waiting for incoming data */ din = new DataInputStream(conn.openInputStream()); dout = new DataOutputStream(conn.openOutputStream()); rs.displayMessage("Client Connected "); listening = false; firstTime = false; } else { cmd = din.readUTF(); System.out.println("Received: " + cmd); if (cmd.equals("ACK")) remoteReady = true; else rs.cmdReceived(cmd); } } catch (Exception e) { rs.displayMessage("Waiting for new connection"); close(); if (firstTime) { rs.displayMessage("Exception Occured, \nMake sure bluetooth adapter is connected, \nPlease restart\nException decription:\n" + e.toString()); return; } listening = true; } } } catch (Exception e) { rs.displayMessage("Exception Occured, \nMake sure bluetooth adapter is connected, \nPlease restart\nException decription:\n" + e.toString()); } }  public RemoteServer() { try { SVTH: Đặng Quốc Nghi 54 gui = new RemoteServerGUI(); mouse = new MouseControl(this); keyboard = new KeyboardControl(this); java.awt.Dimension sdimension java.awt.Toolkit.getDefaultToolkit().getScreenSize(); sx = sdimension.width; sy = sdimension.height; gui.setSize(250, 200); gui.setLocation(sdimension.width - 250, sdimension.height - 250); gui.setVisible(true); gui.jScrollPane1.setLocation(1, 1); gui.jScrollPane1.setSize(245, 150); gui.InfoLabel.setText("Setting device to be discoverable "); = /* - Initialising the Bluetooth Stuck -*/ bluetooth = new Bluetooth(this); gui.InfoLabel.setText("Start advertising service "); /* - Listening for incoming connections */ gui.InfoLabel.setText("Waiting for incoming connection "); bluetooth.listen(); msgbox(); } catch (Exception e) { gui.InfoLabel.setText("Exception Occured, \nMake sure bluetooth adapter is connected, \nPlease restart\nException decription:\n" + e.toString()); } }  public void cmdReceived(String cmd){ /* Parse the command -*/ if (cmd.equalsIgnoreCase("MUP")) { mouse.moveMouse(0, -1); //Move mouse up } if (cmd.equalsIgnoreCase("MDOWN")) { SVTH: Đặng Quốc Nghi 55 mouse.moveMouse(0, 1); //Move mouse down } if (cmd.equalsIgnoreCase("MLEFT")) { mouse.moveMouse(-1, 0); //Move mouse left } if (cmd.equalsIgnoreCase("MRIGHT")) { mouse.moveMouse(1, 0); //Move mouse right } if (cmd.equalsIgnoreCase("SM")) { mouse.stopMouse(); } //Stop moving mouse if (cmd.equalsIgnoreCase("MLCLICK")) { mouse.mClick(MouseControl.LCLICK); //Mouse left click pressed } if (cmd.equalsIgnoreCase("SMLCLICK")) { mouse.mRelease(MouseControl.LCLICK); //Mouse left click released } if (cmd.equalsIgnoreCase("MRCLICK")) { mouse.mClick(MouseControl.RCLICK); } //Mouse right click pressed if (cmd.equalsIgnoreCase("SMRCLICK")) { mouse.mRelease(MouseControl.RCLICK); //Mouse right click released } if (cmd.equalsIgnoreCase("MWPRESS")) { mouse.mClick(MouseControl.WCLICK); } SVTH: Đặng Quốc Nghi //Mouse wheel pressed 56 if (cmd.equalsIgnoreCase("SMWPRESS")) { mouse.mRelease(MouseControl.WCLICK); //Mouse wheel released } if (cmd.equalsIgnoreCase("MWUP")) { mouse.mWheel(-1); } //Move wheel up if (cmd.equalsIgnoreCase("MWDOWN")) { mouse.mWheel(1); } //Move wheel down if (cmd.startsWith("K")) { String scode = cmd.substring(1); keyboard.keyPress(Integer.parseInt(scode)); } //Key pressed if (cmd.startsWith("SK")) { //Key released String scode = cmd.substring(2); keyboard.keyRelease(Integer.parseInt(scode)); } if (cmd.startsWith("run")) { //Invoke a run command String command = cmd.substring(4); try { Runtime.getRuntime().exec(new String[]{"cmd.exe", "/c", command}); } catch (Exception e) { } } if (cmd.startsWith("msg")) { String message = cmd.substring(4); showMsgbox(message); SVTH: Đặng Quốc Nghi "start", //Send message has been send 57 } if (cmd.startsWith("DIM")) { //The screen simensions int ypos = cmd.indexOf("Y"); screenx = Integer.parseInt(cmd.substring(3, ypos)); screeny = Integer.parseInt(cmd.substring(ypos+1)); } if (cmd.equalsIgnoreCase("applist")) //Send the application list try{ util.SendAppList.startSending(this); }catch(Exception e){ gui.InfoLabel.setText("Exception Occured " + e.toString()); } if (cmd.equalsIgnoreCase("CLOSE")) bluetooth.close(); //close connection if (cmd.startsWith("SCRC")) { int size = Integer.parseInt(cmd.substring(5)); byte[] tempsrc = new byte[size]; try { bluetooth.din.readFully(tempsrc, 0, size); gui.jLabel1.setIcon(new javax.swing.ImageIcon(tempsrc)); gui.jScrollPane1.setViewportView(gui.jLabel1); }catch (Exception e) { } } }  public static void startSending(remoteserver.RemoteServer rs) throws IOException, SAXException, ParserConfigurationException { SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(false); XMLReader xmlReader = factory.newSAXParser().getXMLReader(); xmlReader.setContentHandler(new SendAppList(rs)); SVTH: Đặng Quốc Nghi 58 xmlReader.parse("applist.xml"); }  @Override public void startElement(String uri, String lname, String qname, Attributes attributes) { if (qname.equals("applist")) rs.bluetooth.SendData("APPTOTAL " + attributes.getValue("total")); else if (qname.equals("app")) rs.bluetooth.SendData("APPNAME " + attributes.getValue("name")); else if (qname.equals("launch")) { rs.bluetooth.SendData("LAUNCH " + attributes.getValue("name")); rs.bluetooth.SendData("COM " + attributes.getValue("com")); } else if (qname.equals("cmd")) { rs.bluetooth.SendData("CMDKEYS " + attributes.getValue("keys")); rs.bluetooth.SendData("CMDNAME " + attributes.getValue("name")); } else if (qname.equals("key")) rs.bluetooth.SendData("KEY " + attributes.getValue("value")); }  @Override public void endElement(String uri, String lname, String qname) { if (qname.equals("applist")) rs.bluetooth.SendData("END"); }  public KeyboardControl(remoteserver.RemoteServer rs){ try{ this.rs = rs; robot = new Robot(); }catch(Exception e){} }  public void keyPress(int keyCode){ SVTH: Đặng Quốc Nghi 59 robot.keyPress(keyCode); }  public void keyRelease(int keyCode){ robot.keyRelease(keyCode); }  public MouseControl(remoteserver.RemoteServer rs) { try { this.rs = rs; robot = new Robot(); } catch (Exception e) { } }  public void mClick(int button) { robot.mousePress(button); }  public void mRelease(int button) { robot.mouseRelease(button); sendScreen(); }  public void mWheel(int direction) { robot.mouseWheel(direction); sendScreen(); }  public void moveMouse(int x, int y) { this.x = x; this.y = y; cont = true; t = new Thread(this); t.start(); }  public void run() { while (cont) { Point point = MouseInfo.getPointerInfo().getLocation(); robot.mouseMove(point.x + x * step, point.y + y * step); sendScreen(); SVTH: Đặng Quốc Nghi 60 try{ Thread.sleep(100); }catch(Exception e){} } t = null; }  public void sendScreen() { Point point = MouseInfo.getPointerInfo().getLocation(); int startx, starty; if (point.x - (int) (rs.screenx / 2) < 0) { startx = 0; } else if (point.x + (int) (rs.screenx / 2) > rs.sx) { startx = rs.sx - rs.screenx; } else { startx = point.x - (int) (rs.screenx / 2); } if (point.y - (int) (rs.screeny / 2) < 0) { starty = 0; } else if (point.y + (int) (rs.screeny / 2) > rs.sy) { starty = rs.sy - rs.screeny; } else { starty = point.y - (int) (rs.screeny / 2); } Rectangle screenrect = new Rectangle(startx, starty, rs.screenx, rs.screeny); BufferedImage bscreen = robot.createScreenCapture(screenrect); java.io.ByteArrayOutputStream bout = new java.io.ByteArrayOutputStream(); try { javax.imageio.ImageIO.write(bscreen, "png", bout); byte[] tempimage = bout.toByteArray(); if (rs.bluetooth.SendDataIfReady("SCRC " + tempimage.length)) rs.bluetooth.SendData(tempimage); } catch (Exception e) { System.out.println("error" + e.toString()); SVTH: Đặng Quốc Nghi 61 } }  Một số phƣơng thức chƣơng trình client  public void run() { while (true) { try { String cmd = din.readUTF(); if (cmd.startsWith("SCRC")) { int size = Integer.parseInt(cmd.substring(5)); byte[] tempsrc = new byte[size]; din.readFully(tempsrc, 0, size); remote.mainCanvas.setScreen(tempsrc); remote.bluetooth.sendData("ACK"); } else remote.commandsTable.commandReceived(cmd); } catch (Exception e) { if (remote.mode != Remote.MOUSE_MODE) break; } } }  public String connetToServer(int serverIndex) throws IOException { ServiceRecord service = (ServiceRecord) services.elementAt(serverIndex); String url = service.getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false); connetToServer(url); return url; }  public void connetToServer(String url) throws IOException { this.disconnect(); conn = (StreamConnection) Connector.open(url); //establish the connection dout = new DataOutputStream(conn.openOutputStream());//Get the output stream din = new DataInputStream(conn.openInputStream()); SVTH: Đặng Quốc Nghi 62 Thread t = new Thread(this); t.start(); }  private void disconnect() { try { if (dout != null) dout.writeUTF("CLOSE"); conn.close(); dout.close(); din.close(); } catch (Exception ex) { } conn = null; dout = null; din = null; }  public void deviceDiscovered(RemoteDevice remoteDevice, DeviceClass deviceClass) { devices.addElement(remoteDevice); }  public void servicesDiscovered(int transID, ServiceRecord[] serviceRecord) { for (int x = 0; x < serviceRecord.length; x++) services.addElement(serviceRecord[x]); servers.addElement(devices.elementAt(currentDevice)); }  public void inquiryCompleted(int param) { if (param == DiscoveryListener.INQUIRY_COMPLETED) //Inquiry completed normally if (devices.size() > 0) //At least one device has been found try { services = new java.util.Vector(); this.FindServices((RemoteDevice) devices.elementAt(0)); //Check if the first device offers the service } catch (BluetoothStateException ex) { SVTH: Đặng Quốc Nghi 63 remote.searchFinished(null); } }  public void serviceSearchCompleted(int transID, int respCode) { if (currentDevice == devices.size() - && cancel == false) { //all devices have been searched devices = null; if (servers.size() > 0) { String[] serversArray = new String[servers.size()]; for (int x = 0; x < servers.size(); x++) { try { serversArray[x] = ((RemoteDevice) servers.elementAt(x)).getFriendlyName(false); } catch (IOException e) { remote.doAlert("Erron in initiating search", 4000, null); } } remote.searchFinished(serversArray); } else { remote.searchFinished(null); } } else { //search next device try { if(cancel == false){ currentDevice++; this.FindServices((RemoteDevice) devices.elementAt(currentDevice)); } } catch (BluetoothStateException ex) { remote.searchFinished(null); } } }  private void FindServices(RemoteDevice BluetoothStateException { this.disconnect(); SVTH: Đặng Quốc Nghi device) throws 64 UUID[] uuids = new UUID[1]; uuids[0] = new UUID("27012f0c68af4fbf8dbe6bbaf7aa432a", false); UUID of the ech service local = LocalDevice.getLocalDevice(); agent = local.getDiscoveryAgent(); agent.searchServices(null, uuids, device, this); }  public void FindDevices() throws BluetoothStateException { cancel = false; devices = new Vector(); servers = new Vector(); local = LocalDevice.getLocalDevice(); agent = local.getDiscoveryAgent(); agent.startInquiry(DiscoveryAgent.GIAC, this); } SVTH: Đặng Quốc Nghi //The ... “TÌM HIỂU CÔNG NGHỆ BLUETOOTH VÀ VIẾT ỨNG DỤNG MINH HỌA” Mục tiêu đề tài tìm hiểu cơng nghệ Bluetooth xây dựng chương trình điều khiển máy tính điện thoại di động thông qua Bluetooth để minh họa. .. sau: - Tìm hiểu cơng nghệ Bluetooth, công nghệ không dây phát triển mạnh có tầm ứng dụng rộng rãi Nắm cách thức hoạt động, đặc điểm kĩ thuật khả công nghệ Bluetooth - Trong trình tìm hiểu Bluetooth, ... gồm:  Tìm hiểu hoạt động kỹ thuật Bluetooth  Tìm hiểu hệ điều hành Symbian series 60  Xây dựng ứng dụng minh họa SVTH: Đặng Quốc Nghi MỤC LỤC Chương 1: CÔNG NGHỆ BLUETOOTH 1.1 Bluetooth

Ngày đăng: 31/08/2014, 17:03

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan