Skip to content

Instantly share code, notes, and snippets.

@Lauszus
Last active December 18, 2015 01:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lauszus/5708155 to your computer and use it in GitHub Desktop.
Save Lauszus/5708155 to your computer and use it in GitHub Desktop.
Java native Bluetooth
package com.tkjelectronics.balanduinoapp;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.LocalDevice;
import javax.microedition.io.StreamConnection;
public class Inquiry {
static Object lock = new Object();
final static int sizeX = 500;
final static int sizeY = 200;
final static double textX = (double)sizeX*1.0/100.0;
final static double textY = (double)sizeY*9.0/10.0;
static DataOutputStream out;
static DataInputStream in;
private static void initWindow() {
/*StdDraw.setCanvasSize(sizeX, sizeY); // Adjust the size of the window
StdDraw.setXscale(0, sizeX); // Set size of window
StdDraw.setYscale(0, sizeY);*/
}
public static void printText(double x, double y, String string) {
/*StdDraw.setPenColor(StdDraw.BLACK);
StdDraw.setPenRadius(2.0/((double)sizeX*10.0));
StdDraw.textLeft(x, y, string);*/
}
public static void main(String[] args) {
initWindow();
try {
// 1
LocalDevice localDevice = LocalDevice.getLocalDevice();
// 2
DiscoveryAgent agent = localDevice.getDiscoveryAgent();
// 3
agent.startInquiry(DiscoveryAgent.GIAC, new MyDiscoveryListener());
try {
synchronized (lock) {
lock.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Connected to device");
//StdDraw.clear();
printText(textX,textY,"Connected to device");
} catch (Exception e) {
e.printStackTrace();
}
while(true);
}
public static void deviceReading(StreamConnection con) {
synchronized (lock) {
lock.notify();
}
try {
out = con.openDataOutputStream();
in = con.openDataInputStream();
Thread.sleep(1000);
// write data into serial stream
out.writeBytes(new String("Hello Arduino\r\n"));
out.flush();
while(true) {
if(in.available() > 0) {
byte[] buffer = new byte[30];
if(in.read(buffer,0,in.available()) != -1) {
String string = new String(buffer,"US-ASCII");
System.out.println(string);
//StdDraw.clear();
printText(textX,textY,string);
}
}
Thread.sleep(500);
}
// Finish, close output stream and connection
//out.close();
//in.close();
//con.close();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
package com.tkjelectronics.balanduinoapp;
import javax.bluetooth.BluetoothStateException;
import javax.bluetooth.DataElement;
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 MyDiscoveryListener implements DiscoveryListener {
public final static int SERIALPORT_UUID = 0x1101;
public final static int RFCOMM_UUID = 0x0003;
public final static int L2CAP_UUID = 0x0100;
UUID[] uuidSet = { new UUID(SERIALPORT_UUID), new UUID(RFCOMM_UUID), new UUID(L2CAP_UUID) };
int[] attrIDs = new int[] { 0x0100 }; // Service Name
DiscoveryAgent agent;
@Override
public void deviceDiscovered(RemoteDevice btDevice, DeviceClass arg1) {
String name;
try {
name = btDevice.getFriendlyName(false);
} catch (Exception e) {
name = btDevice.getBluetoothAddress();
}
System.out.println("Device found: " + name);
scanForServices(btDevice,name);
}
private void scanForServices(RemoteDevice btDevice, String name) {
System.out.println("Scanning for services");
//StdDraw.clear();
Inquiry.printText(Inquiry.textX,Inquiry.textY,"Device found: " + name);
Inquiry.printText(Inquiry.textX,Inquiry.textY-Inquiry.textY/8.0,"Scanning for services...");
try {
LocalDevice localDevice = LocalDevice.getLocalDevice();
agent = localDevice.getDiscoveryAgent();
agent.searchServices(attrIDs, uuidSet, btDevice, this);
} catch (BluetoothStateException e1) {
e1.printStackTrace();
}
}
@Override
public void inquiryCompleted(int arg0) {
}
@Override
public void serviceSearchCompleted(int arg0, int arg1) {
System.out.println("Service Search Completed");
}
@Override
public void servicesDiscovered(int arg0, ServiceRecord[] services) {
System.out.println("Service Discovered");
for (int i = 0; i < services.length; i++) {
String url = services[i].getConnectionURL(ServiceRecord.AUTHENTICATE_NOENCRYPT, false);
if (url == null)
continue;
DataElement serviceName = services[i].getAttributeValue(0x0100); // Service Name
String string = "";
if (serviceName != null)
string = "service " + serviceName.getValue() + " found " + url;
else
string = "service found " + url;
System.out.println(string);
//StdDraw.clear();
Inquiry.printText(Inquiry.textX,Inquiry.textY,"Service found");
if (serviceName.getValue().equals("TKJSP")) {
connectToDevice(url);
agent.cancelInquiry(this);
agent.cancelServiceSearch(arg0);
}
}
}
private static void connectToDevice(String serverURL) {
try {
System.out.println("Connecting to " + serverURL);
//StdDraw.clear();
Inquiry.printText(Inquiry.textX,Inquiry.textY,"Connecting to device");
StreamConnection con = (StreamConnection) Connector.open(serverURL);
Inquiry.deviceReading(con);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment