Skip to content

Instantly share code, notes, and snippets.

@jribal
Last active March 19, 2019 09:13
Show Gist options
  • Save jribal/e542387a4e5ba799a2c2c9ca374f3701 to your computer and use it in GitHub Desktop.
Save jribal/e542387a4e5ba799a2c2c9ca374f3701 to your computer and use it in GitHub Desktop.
libsubsurface-handler.java
public class MainActivity extends Activity
{
static {
System.loadLibrary("subsurface-bridge");
}
public native String libsubsurfaceExtract(long bluetoothsocket);
private BluetoothAdapter bluetoothAdapter = null;
BluetoothSocket socket = null;
OutputStream out = null;
InputStream in = null;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
if (pairedDevices.size() > 0) {
// There are paired devices. Get the name and address of each paired device.
for (BluetoothDevice device : pairedDevices) {
String deviceName = device.getName();
String deviceHardwareAddress = device.getAddress(); // MAC address
LOG.d("bluetooth", deviceName + deviceHardwareAddress);
}
}
final UUID SERIAL_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); //UUID for serial connection
String mac = "00:80:25:34:CE:A5"; //my laptop's mac adress
BluetoothDevice device = bluetoothAdapter.getRemoteDevice(mac); //get remote device by mac, we assume these two devices are already paired
// Get a BluetoothSocket to connect with the given BluetoothDevice
try {
socket = device.createRfcommSocketToServiceRecord(SERIAL_UUID);
} catch (IOException e) {
Log.d("bluetooth", e.getMessage());
}
try {
socket.connect();
out = socket.getOutputStream();
in = socket.getInputStream();
LOG.d("plugin", "library loaded");
LOG.d("plugin", libsubsurfaceExtract(12));
} catch (Exception e) {
Log.d("bluetooth", e.getMessage());
}
return "Native called worked";
}
void writeToSocket(byte[] data){
try {
try {
Thread.sleep(300);
if (socket.isConnected()) {
out.write(data);
//LOG.d("output", String.valueOf(data.length) );
}
else
LOG.d("outputstream", "socket is not connected");
} catch (InterruptedException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment