1 /* 2 * Author: Stefan Andritoiu <stefan.andritoiu (at) intel.com> 3 * Copyright (c) 2015 Intel Corporation. 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining 6 * a copy of this software and associated documentation files (the 7 * "Software"), to deal in the Software without restriction, including 8 * without limitation the rights to use, copy, modify, merge, publish, 9 * distribute, sublicense, and/or sell copies of the Software, and to 10 * permit persons to whom the Software is furnished to do so, subject to 11 * the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be 14 * included in all copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 */ 24 25 //NOT TESTED!!! 26 public class HM11Sample { 27 28 private static final int BUFSIZ = 1024; 29 30 static { 31 try { 32 System.loadLibrary("javaupm_hm11"); 33 } catch (UnsatisfiedLinkError e) { 34 System.err.println("error in loading native library"); 35 System.exit(-1); 36 } 37 } 38 39 private static void printUsage() { 40 System.out.println("Usage: java HM11Sample [AT command]"); 41 42 System.out.println("If an argument is supplied on the command line, that argument is"); 43 System.out.println("sent to the module and the response is printed out."); 44 System.out.println("If no argument is used, then the address and PIN of the module"); 45 System.out.println("are queried and the results printed out."); 46 47 } 48 49 private static void sendCommand(upm_hm11.HM11 ble, byte[] cmd) { 50 byte[] buffer = new byte[BUFSIZ]; 51 ble.writeData(cmd); 52 53 // wait up to 1 second 54 if (ble.dataAvailable(1000)) { 55 ble.readData(buffer); 56 } else { 57 System.err.println("Timed out waiting for response"); 58 } 59 60 } 61 62 public static void main(String[] args) throws InterruptedException { 63 // ! [Interesting] 64 // Instantiate a HM11 BLE Module on UART 0 65 upm_hm11.HM11 ble = new upm_hm11.HM11(0); 66 67 // make sure port is initialized properly. 9600 baud is the default. 68 if (!ble.setupTty()) { 69 System.err.println("Failed to setup tty port parameters"); 70 System.exit(-1); 71 } 72 73 printUsage(); 74 75 if (args.length > 0) { 76 System.out.println("Sending command line argument (" + args[0] + ")..."); 77 sendCommand(ble, args[0].getBytes()); 78 } else { 79 // query the module address 80 String addr = "AT+ADDR?"; 81 System.out.println("Querying module address (" + addr + ")..."); 82 sendCommand(ble, addr.getBytes()); 83 84 Thread.sleep(1000); 85 86 // query the module address 87 String pin = "AT+PASS?"; 88 System.out.println("Querying module pin (" + pin + ")..."); 89 sendCommand(ble, pin.getBytes()); 90 91 // Other potentially useful commands are: 92 // 93 // AT+VERS? - query module version 94 // AT+ROLE0 - set as slave 95 // AT+ROLE1 - set as master 96 // AT+CLEAR - clear all previous settings 97 // AT+RESET - restart the device 98 // 99 // A comprehensive list is available from the datasheet at: 100 // http://www.seeedstudio.com/wiki/images/c/cd/Bluetooth4_en.pdf 101 102 } 103 // ! [Interesting] 104 } 105 } 106