├── bin └── bluetooth-java-client.jar ├── src └── main │ ├── resources │ ├── bluecove-2.1.1-SNAPSHOT.jar │ ├── bluecove-emu-2.1.1-SNAPSHOT.jar │ ├── bluecove-gpl-2.1.1-SNAPSHOT.jar │ └── bluecove-bluez-2.1.1-SNAPSHOT.jar │ └── java │ └── co │ └── aurasphere │ └── bluetooth │ └── client │ ├── DeviceDiscoveredLoggingCallback.java │ ├── IncomingMessagesLoggingRunnable.java │ └── IrcBluetoothClient.java ├── .gitignore ├── README.md ├── LICENSE └── pom.xml /bin/bluetooth-java-client.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rifkyprayoga/bluetooth-java-client/HEAD/bin/bluetooth-java-client.jar -------------------------------------------------------------------------------- /src/main/resources/bluecove-2.1.1-SNAPSHOT.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rifkyprayoga/bluetooth-java-client/HEAD/src/main/resources/bluecove-2.1.1-SNAPSHOT.jar -------------------------------------------------------------------------------- /src/main/resources/bluecove-emu-2.1.1-SNAPSHOT.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rifkyprayoga/bluetooth-java-client/HEAD/src/main/resources/bluecove-emu-2.1.1-SNAPSHOT.jar -------------------------------------------------------------------------------- /src/main/resources/bluecove-gpl-2.1.1-SNAPSHOT.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rifkyprayoga/bluetooth-java-client/HEAD/src/main/resources/bluecove-gpl-2.1.1-SNAPSHOT.jar -------------------------------------------------------------------------------- /src/main/resources/bluecove-bluez-2.1.1-SNAPSHOT.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rifkyprayoga/bluetooth-java-client/HEAD/src/main/resources/bluecove-bluez-2.1.1-SNAPSHOT.jar -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled class file 2 | *.class 3 | 4 | # Log file 5 | *.log 6 | 7 | # BlueJ files 8 | *.ctxt 9 | 10 | # Mobile Tools for Java (J2ME) 11 | .mtj.tmp/ 12 | 13 | # Package Files # 14 | *.jar 15 | *.war 16 | *.ear 17 | *.zip 18 | *.tar.gz 19 | *.rar 20 | 21 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 22 | hs_err_pid* 23 | /target/ 24 | 25 | # Eclipse files 26 | /.classpath 27 | /.project 28 | /.settings -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Donate](https://img.shields.io/badge/Donate-PayPal-orange.svg)](https://www.paypal.com/donate/?cmd=_donations&business=8UK2BZP2K8NSS) 2 | 3 | # Bluetooth Java Client 4 | Simple Bluetooth Java client based on the Bluecove library. 5 | 6 | This program can be used to perform the following operations: 7 | 8 | - perform Bluetooth device discovery by passing no arguments 9 | - connect to a Bluetooth device and open an IRC-style chat by passing a Bluetooth RFCOMM address 10 | 11 | A Bluetooth RFCOMM address has the following format: 12 | 13 | btspp://: 14 | 15 | For reference, here's my Arduino Bluetooth address: 16 | 17 | btspp://98D3318041DE:1. 18 | 19 | The program needs that the device to which connect is paired before-hand (through your PC settings). If working with an Arduino, remember that the passcode is something like 1234. 20 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Donato Rimenti 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | co.aurasphere 5 | bluetooth-java-client 6 | 1.0.0 7 | Bluetooth Java Client 8 | Simple Bluetooth Java client based on the Bluecove library. 9 | 10 | 11 | 12 | net.sf.bluecove 13 | bluecove 14 | 2.1.1-SNAPSHOT 15 | system 16 | ${basedir}/src/main/resources/bluecove-2.1.1-SNAPSHOT.jar 17 | 18 | 19 | net.sf.bluecove 20 | bluecove-bluez 21 | 2.1.1-SNAPSHOT 22 | system 23 | ${basedir}/src/main/resources/bluecove-bluez-2.1.1-SNAPSHOT.jar 24 | 25 | 26 | net.sf.bluecove 27 | bluecove-emu 28 | 2.1.1-SNAPSHOT 29 | system 30 | ${basedir}/src/main/resources/bluecove-emu-2.1.1-SNAPSHOT.jar 31 | 32 | 33 | net.sf.bluecove 34 | bluecove-gpl 35 | 2.1.1-SNAPSHOT 36 | system 37 | ${basedir}/src/main/resources/bluecove-gpl-2.1.1-SNAPSHOT.jar 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | src/main/resources 46 | 47 | *.jar 48 | 49 | false 50 | 51 | 52 | 53 | 54 | 55 | org.apache.maven.plugins 56 | maven-dependency-plugin 57 | 3.0.2 58 | 59 | 60 | src-dependencies 61 | package 62 | 63 | unpack-dependencies 64 | 65 | 66 | ${project.build.directory}/classes 67 | 68 | 69 | 70 | 71 | 72 | 73 | maven-assembly-plugin 74 | 75 | 76 | 77 | co.aurasphere.bluetooth.client.IrcBluetoothClient 78 | 79 | 80 | false 81 | 82 | jar-with-dependencies 83 | 84 | 85 | 86 | 87 | make-assembly 88 | package 89 | 90 | single 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | -------------------------------------------------------------------------------- /src/main/java/co/aurasphere/bluetooth/client/DeviceDiscoveredLoggingCallback.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Donato Rimenti 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | package co.aurasphere.bluetooth.client; 26 | 27 | import java.io.IOException; 28 | 29 | import javax.bluetooth.DeviceClass; 30 | import javax.bluetooth.DiscoveryListener; 31 | import javax.bluetooth.RemoteDevice; 32 | import javax.bluetooth.ServiceRecord; 33 | 34 | /** 35 | * Callback to handle device discovery event. It only logs the names and 36 | * addresses of the device discovered. It ignores the services discovered. 37 | * 38 | * @author Donato Rimenti 39 | * 40 | */ 41 | public class DeviceDiscoveredLoggingCallback implements DiscoveryListener { 42 | 43 | /* 44 | * (non-Javadoc) 45 | * 46 | * @see javax.bluetooth.DiscoveryListener#servicesDiscovered(int, 47 | * javax.bluetooth.ServiceRecord[]) 48 | */ 49 | public void servicesDiscovered(int transID, ServiceRecord[] services) { 50 | // Not supported. 51 | } 52 | 53 | /* 54 | * (non-Javadoc) 55 | * 56 | * @see javax.bluetooth.DiscoveryListener#serviceSearchCompleted(int, int) 57 | */ 58 | public void serviceSearchCompleted(int transID, int respCode) { 59 | // Not supported. 60 | } 61 | 62 | /* 63 | * (non-Javadoc) 64 | * 65 | * @see javax.bluetooth.DiscoveryListener#inquiryCompleted(int) 66 | */ 67 | public void inquiryCompleted(int discType) { 68 | // Logs the end of the device discovery. 69 | System.out.println("Device discovery completed!"); 70 | 71 | // Very basic synchronization mechanism. 72 | synchronized (IrcBluetoothClient.class) { 73 | IrcBluetoothClient.class.notify(); 74 | } 75 | } 76 | 77 | /* 78 | * (non-Javadoc) 79 | * 80 | * @see javax.bluetooth.DiscoveryListener#deviceDiscovered(javax.bluetooth. 81 | * RemoteDevice, javax.bluetooth.DeviceClass) 82 | */ 83 | public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) { 84 | // Logs the device discovered. 85 | String address = btDevice.getBluetoothAddress(); 86 | try { 87 | String name = btDevice.getFriendlyName(false); 88 | System.out.println("New device discovered: [" + address + " - " + name + "]"); 89 | } catch (IOException e) { 90 | System.err.println("Error while retrieving name for device [" + address + "]"); 91 | e.printStackTrace(); 92 | } 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /src/main/java/co/aurasphere/bluetooth/client/IncomingMessagesLoggingRunnable.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Donato Rimenti 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | package co.aurasphere.bluetooth.client; 26 | 27 | import java.io.BufferedInputStream; 28 | import java.io.IOException; 29 | import java.io.InputStream; 30 | 31 | import javax.bluetooth.RemoteDevice; 32 | import javax.microedition.io.StreamConnection; 33 | 34 | /** 35 | * Runnable which logs the incoming Bluetooth messages from the remote device. 36 | * 37 | * @author Donato Rimenti 38 | * 39 | */ 40 | public class IncomingMessagesLoggingRunnable implements Runnable { 41 | 42 | /** 43 | * The Bluetooth connection between this device and the remote one. 44 | */ 45 | private StreamConnection connection; 46 | 47 | /** 48 | * Instantiates a new IncomingMessagesListenerRunnable. 49 | * 50 | * @param connection 51 | * the {@link #connection} 52 | */ 53 | public IncomingMessagesLoggingRunnable(StreamConnection connection) { 54 | this.connection = connection; 55 | } 56 | 57 | /* 58 | * (non-Javadoc) 59 | * 60 | * @see java.lang.Runnable#run() 61 | */ 62 | public void run() { 63 | // Opens the connection. If this fails, the whole listening service 64 | // fails. 65 | InputStream input = null; 66 | RemoteDevice device = null; 67 | try { 68 | input = new BufferedInputStream(connection.openInputStream()); 69 | device = RemoteDevice.getRemoteDevice(connection); 70 | } catch (IOException e) { 71 | System.err.println("Listening service failed. Incoming messages won't be displayed."); 72 | e.printStackTrace(); 73 | return; 74 | } 75 | 76 | // Main loop of the program: reads a string incoming from the 77 | // Bluetooth connection and prints it. 78 | while (true) { 79 | byte buffer[] = new byte[1024]; 80 | int bytesRead; 81 | try { 82 | bytesRead = input.read(buffer); 83 | String incomingMessage = new String(buffer, 0, bytesRead); 84 | System.out.println("[" + device.getFriendlyName(false) + " - " + device.getBluetoothAddress() + "]: " 85 | + incomingMessage); 86 | } catch (IOException e) { 87 | // Don't rethrow this exception so if one message is lost, the 88 | // service continues listening. 89 | System.err.println("Error while reading the incoming message."); 90 | e.printStackTrace(); 91 | } 92 | } 93 | 94 | } 95 | 96 | } 97 | -------------------------------------------------------------------------------- /src/main/java/co/aurasphere/bluetooth/client/IrcBluetoothClient.java: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Donato Rimenti 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | package co.aurasphere.bluetooth.client; 26 | 27 | import java.io.BufferedReader; 28 | import java.io.IOException; 29 | import java.io.InputStreamReader; 30 | import java.io.OutputStream; 31 | import java.nio.charset.StandardCharsets; 32 | import java.util.concurrent.ExecutorService; 33 | import java.util.concurrent.Executors; 34 | 35 | import javax.bluetooth.BluetoothStateException; 36 | import javax.bluetooth.DiscoveryAgent; 37 | import javax.bluetooth.LocalDevice; 38 | import javax.microedition.io.Connector; 39 | import javax.microedition.io.StreamConnection; 40 | 41 | /** 42 | * Simple Bluetooth Java client based on the Bluecove library. It implements two 43 | * methods to perform device discovery and Bluetooth connection in IRC 44 | * style.
45 | *
46 | * This script assumes that the Bluetooth device is already paired using your PC 47 | * settings (if you are using Arduino default passcodes should be something like 48 | * 1234). 49 | * 50 | * @author Donato Rimenti 51 | * 52 | */ 53 | public class IrcBluetoothClient { 54 | 55 | /** 56 | * Starts the Bluetooth devices discovery. Close-by devices are printed in 57 | * console. 58 | * 59 | * @throws BluetoothStateException 60 | * @throws InterruptedException 61 | */ 62 | private static void startDiscovery() throws BluetoothStateException, InterruptedException { 63 | DiscoveryAgent agent = LocalDevice.getLocalDevice().getDiscoveryAgent(); 64 | System.out.println("Starting device discovery..."); 65 | agent.startInquiry(DiscoveryAgent.GIAC, new DeviceDiscoveredLoggingCallback()); 66 | 67 | // Very basic synchronization mechanism. 68 | synchronized (IrcBluetoothClient.class) { 69 | IrcBluetoothClient.class.wait(); 70 | } 71 | } 72 | 73 | /** 74 | * Opens up a connection to the specified address. After the connection is 75 | * open, it's possible to send and receive messages like in an IRC. 76 | * 77 | * @param address 78 | * the address to which connect 79 | * @throws IOException 80 | */ 81 | private static void openConnection(String address) throws IOException { 82 | // Tries to open the connection. 83 | StreamConnection connection = (StreamConnection) Connector.open(address); 84 | if (connection == null) { 85 | System.err.println("Could not open connection to address: " + address); 86 | System.exit(1); 87 | } 88 | 89 | // Initializes the streams. 90 | OutputStream output = connection.openOutputStream(); 91 | InputStreamReader isr = new InputStreamReader(System.in); 92 | BufferedReader reader = new BufferedReader(isr); 93 | 94 | // Starts the listening service for incoming messages. 95 | ExecutorService service = Executors.newSingleThreadExecutor(); 96 | service.submit(new IncomingMessagesLoggingRunnable(connection)); 97 | 98 | // Main loop of the program: reads a string and sends to the Bluetooth 99 | // device. 100 | System.out.println("Connection opened, type in console and press enter to send a message to: " + address); 101 | LocalDevice localDevice = LocalDevice.getLocalDevice(); 102 | while (true) { 103 | String toSend = reader.readLine(); 104 | byte[] toSendBytes = toSend.getBytes(StandardCharsets.US_ASCII); 105 | output.write(toSendBytes); 106 | System.out.println( 107 | "[" + localDevice.getFriendlyName() + " - " + localDevice.getBluetoothAddress() + "]: " + toSend); 108 | } 109 | } 110 | 111 | /** 112 | * Main method of this program. It's behavior depends on the number of 113 | * arguments passed: 114 | * 120 | * 121 | * An RFCOMM Bluetooth URL follows the structure: 122 | * 127 | * 128 | * For reference, here's an example address from my Arduino: 129 | * btspp://98D3318041DE:1. 130 | * 131 | * @param args 132 | * an optional Bluetooth address to which connect 133 | * @throws IOException 134 | * @throws InterruptedException 135 | */ 136 | public static void main(String[] args) throws IOException, InterruptedException { 137 | // Prints some informations at startup about this device. 138 | LocalDevice local = LocalDevice.getLocalDevice(); 139 | System.out.println("----------- LOCAL DEVICE INFORMATION -----------"); 140 | System.out.println("Address: " + local.getBluetoothAddress()); 141 | System.out.println("Name: " + local.getFriendlyName()); 142 | 143 | // Checks the number of arguments passed. 144 | switch (args.length) { 145 | case 0: 146 | // If no arguments are passed, performs device discovery. 147 | startDiscovery(); 148 | break; 149 | case 1: 150 | // If 1 argument is passed, it is the Bluetooth address to connect 151 | // with. 152 | openConnection(args[0]); 153 | break; 154 | default: 155 | // If more arguments are passed, the help is printed. 156 | System.err.println("Usage:"); 157 | System.err.println(" -> starts device discovery"); 158 | System.err.println(" -> opens IRC chat with that device through Bluetooth"); 159 | break; 160 | } 161 | } 162 | 163 | } 164 | --------------------------------------------------------------------------------