2

I intend to establish communication between my laptop and Arduino via Bluetooth. I'm using BlueCove Java API. I'm unable to understand how to send data to Arduino using this. There are no clear examples available on the internet regarding how to establish the communication between these two. Could somebody provide any information on this? I just want to send some string to Arduino, based on which it must send back something to the laptop. Any help on this would be greatly appreciated. Thank you.

2

I was able to establish bluetooth communication between Arduino and my laptop successfully. I made some observations, which I'd like to share. BlueCove library which I've been using, ultimately communicates to the Arduino by constructing a bluetooth URL. This bluetooth URL is of the form btspp://bluetooth_address_without_colon:1;authenticate=false;encrypt=false;master=false. To find the bluetooth address of a device, enter the following command in the terminal

hcitool scan

This will display a list of devices along with their addresses. For instance, if my HC-05 has an address ​ 20:16:10:09:47:93 then the bluetooth URL can be formulated as btspp://201610094793:1;authenticate=false;encrypt=false;master=false. Note that the '1' here represents the rfcomm channel. By observation, I've found that HC-05 modules use rfcomm channel 1. In your project, ensure that you import the two jars of BlueCove libraries viz bluecove-2.1.0.jar and bluecove-gpl-2.1.0.jar (latest version). You can download them from here. This is the Java code for communicating with the Arduino to turn an LED ON/OFF:

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.bluetooth.DeviceClass;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.DiscoveryListener;
import javax.bluetooth.LocalDevice;
import javax.bluetooth.RemoteDevice;
import javax.bluetooth.ServiceRecord;
import javax.bluetooth.UUID;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
public class HC05 {
boolean scanFinished = false;
String hc05Url =
"btspp://201610094738:1;authenticate=false;encrypt=false;master=false"; //Replace this with your bluetooth URL
public static void main(String[] args) {
try {
new HC05().go();
} catch (Exception ex) {
Logger.getLogger(HC05.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void go() throws Exception {
StreamConnection streamConnection = (StreamConnection)
Connector.open(hc05Url);
OutputStream os = streamConnection.openOutputStream();
InputStream is = streamConnection.openInputStream();
os.write("1".getBytes()); //'1' means ON and '0' means OFF
os.close();
byte[] b = new byte[200];
Thread.sleep(200);
is.read(b);
is.close();
streamConnection.close();
System.out.println("received " + new String(b));
}
}

This the Arduino code for controlling the LED:

int ledPin = 13;
int flag = 0;
int receivedData;
void setup() {
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
Serial.begin(9600);
}
void loop() {
if(Serial.available()>0){
receivedData = Serial.read();
flag = 1;
}
if(flag == 1){
switch(receivedData){
case '0':
digitalWrite(ledPin, LOW);
Serial.println("LED Turned OFF");
break;
case '1':
digitalWrite(ledPin, HIGH);
Serial.println("LED Turned ON");
break;
default:
Serial.println("Nothing here");
break;
}
flag = 0;
receivedData = 12;
}
Serial.flush();
}

Thanks to @Sigma for sharing some valuable information

Reference:

https://stackoverflow.com/questions/33473926/best-practice-java-serial-bluetooth-connection-hc-05

1

Are using a bluetooth module like HC-05 or HC-06? Then first establish an connection to the module. Then start sending bytes(a string). But first thing first start the connection. Ive only done it with an Android to Arduino, but still the Android is written with Java.

  • Yeah, I'm using HC-05. I've been able to detect it by using the BlueCove library. I referred the code from here snapshot.bluecove.org/bluecove/apidocs/…. But, I have no clue what to do after this. I'm a beginner in Java. – Clement Mar 7 '17 at 4:54
  • 1
    I followed this one Github repo bluetooth Android, I understand that it isn't exactly Java but found a similar ex in Java with almost the same code, they follow the same practice. Open bluetooth, find the device-history of paired BT, set up an socket to that device, start sending and reciving message. Or else there is this one on SO link – Sigma Mar 7 '17 at 20:29
  • The SO link worked out. It wasn't direct though. First I ran the Service Search program from here, and found out the url of my HC-05 device, after getting bombarded with exceptions few times. After that, I used that URL directly and sent data to the Arduino via bluetooth. But, while receiving data from Arduino on my laptop, there's loss of bytes. I send "LED" from Arduino, and end up receiving only "L". Thanks – Clement Mar 8 '17 at 6:35
  • 1
    Do you have a example code from Arduino and PC? Could be that your Read-method isn't running more then once. Are you sending the message from the Arduino, can you verify that? Are you using SoftwareSerial on Arduino? What BaudRate is everything running on, 9600-115200? Anyway you are doing progress, there is communication. Lets debug, is the problem on Arduino or PC?! – Sigma Mar 8 '17 at 12:15
  • 1
    I'm running the Arduino code program from the SO link that you gave. I modified the code by just removing the delay(). I added a sleep of 200ms in my Java code after writing the request to Arduino. 200ms was enough for the Arduino to process my request. Then I read from the Arduino. This time it worked out well. But, I'm not sure if using Thread.sleep() is advisable in Java. I read somewhere that sleep() must be avoided. – Clement Mar 9 '17 at 8:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.