How can I know when a remoteDevice sends me a data?

44 views
Skip to first unread message

Sergio Alvarado

unread,
Jan 8, 2016, 4:15:17 PM1/8/16
to bluecove-users
is there a way to create something like an listener for recieve data?

I'm trying to work with bluetooth arduino and Java, I have been able to send data from pc to arduino (for example, press a Jbutton, arduino turns on a led), but I don't know how to do that when arduino bluetooh sends me a data the program detect it (for example, pressing an button in  arduino, sends a message and the program print it without I have to touch it)

With SerialPort this is easy, just add a SerialPortEventListener, but I don't know how to do it with Bluetooth.

The connection with the remote device is this

Thanks

try {
ServiceRecord serviceRecord = (ServiceRecord) listener.getServices().get(listener.getDevices().get(0))[0];
if(serviceRecord == null) {
System.out.println("The connection could not be made. Device not discovered");
} else {
String url = serviceRecord.getConnectionURL(ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
if(url == null) {
System.out.println("The connection could not be made. Connection url not found");
} else {
Connection connection = Connector.open(url);
if(connection instanceof StreamConnectionNotifier) {
streamConnNotifier = (StreamConnectionNotifier)connection;
streamConnection = streamConnNotifier.acceptAndOpen();
} else if(connection instanceof StreamConnection) {
streamConnNotifier = null;
streamConnection = (StreamConnection)connection;
} else {
throw new Exception("Connection class not known. " + connection.getClass().getCanonicalName());
}
System.out.println("aqui");
//read string from spp client
Input = streamConnection.openInputStream();
Output = streamConnection.openOutputStream();
retvalue = true;
}
}
} catch(Exception e) {
e.printStackTrace();
System.out.println("The connection could not be made." + e.getMessage());
}

Reply all
Reply to author
Forward
0 new messages