Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Personalization of UUID in bluetooth connection #398

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/android/com/megster/cordova/BluetoothSerial.java
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ public boolean execute(String action, CordovaArgs args, CallbackContext callback
discoverIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, discoverableDuration);
cordova.getActivity().startActivity(discoverIntent);

} else {
} else {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are we changing this formatting?

validAction = false;

}
Expand Down Expand Up @@ -337,12 +337,18 @@ private JSONObject deviceToJSON(BluetoothDevice device) throws JSONException {
}

private void connect(CordovaArgs args, boolean secure, CallbackContext callbackContext) throws JSONException {
String macAddress = args.getString(0);
String connectTo = args.getString(0);
String[] parts = connectTo.split("\\|");
String macAddress = parts[0];
String uuid = null;
if(parts.length > 1) {
uuid = parts[1];
}
BluetoothDevice device = bluetoothAdapter.getRemoteDevice(macAddress);

if (device != null) {
connectCallback = callbackContext;
bluetoothSerialService.connect(device, secure);
bluetoothSerialService.connect(device, secure, uuid);
buffer.setLength(0);

PluginResult result = new PluginResult(PluginResult.Status.NO_RESULT);
Expand Down
19 changes: 14 additions & 5 deletions src/android/com/megster/cordova/BluetoothSerialService.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,9 @@ public synchronized void start() {
* Start the ConnectThread to initiate a connection to a remote device.
* @param device The BluetoothDevice to connect
* @param secure Socket Security type - Secure (true) , Insecure (false)
* @param uuid A string representing the UUID to connect to. If null, the default is used.
*/
public synchronized void connect(BluetoothDevice device, boolean secure) {
public synchronized void connect(BluetoothDevice device, boolean secure, String uuid) {
if (D) Log.d(TAG, "connect to: " + device);

// Cancel any thread attempting to make a connection
Expand All @@ -129,7 +130,7 @@ public synchronized void connect(BluetoothDevice device, boolean secure) {
if (mConnectedThread != null) {mConnectedThread.cancel(); mConnectedThread = null;}

// Start the thread to connect with the given device
mConnectThread = new ConnectThread(device, secure);
mConnectThread = new ConnectThread(device, secure, uuid);
mConnectThread.start();
setState(STATE_CONNECTING);
}
Expand Down Expand Up @@ -339,19 +340,27 @@ private class ConnectThread extends Thread {
private final BluetoothDevice mmDevice;
private String mSocketType;

public ConnectThread(BluetoothDevice device, boolean secure) {
public ConnectThread(BluetoothDevice device, boolean secure, String uuid) {
mmDevice = device;
BluetoothSocket tmp = null;
mSocketType = secure ? "Secure" : "Insecure";

UUID remoteUUID = null;
try {
remoteUUID = UUID.fromString(uuid);
Log.v(TAG, "Connect using user-defined UUID " + uuid);
} catch(Exception e) {
Log.e(TAG, "Invalid UUID provided for connection: " + uuid + ". Fallback to default.", e);
}

// Get a BluetoothSocket for a connection with the given BluetoothDevice
try {
if (secure) {
// tmp = device.createRfcommSocketToServiceRecord(MY_UUID_SECURE);
tmp = device.createRfcommSocketToServiceRecord(UUID_SPP);
tmp = device.createRfcommSocketToServiceRecord((remoteUUID != null) ? remoteUUID : UUID_SPP);
} else {
//tmp = device.createInsecureRfcommSocketToServiceRecord(MY_UUID_INSECURE);
tmp = device.createInsecureRfcommSocketToServiceRecord(UUID_SPP);
tmp = device.createInsecureRfcommSocketToServiceRecord((remoteUUID != null) ? remoteUUID : UUID_SPP);
}
} catch (IOException e) {
Log.e(TAG, "Socket Type: " + mSocketType + "create() failed", e);
Expand Down
3 changes: 1 addition & 2 deletions www/bluetoothSerial.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,7 @@ module.exports = {

setDiscoverable: function (discoverableDuration) {
cordova.exec(null, null, "BluetoothSerial", "setDiscoverable", [discoverableDuration]);
}

},

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this necessary?


};

Expand Down