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

Fix and minor changes regarding MTU in network.bluetoothLE #136

Open
wants to merge 3 commits into
base: develop
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
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ public class PBluetoothLEClient extends ProtoBase implements WhatIsRunningInterf
private BluetoothGatt mGatt;
private ReturnInterface mCallbackDevice;
private ReturnInterface mCallbackCharacteristic;
private int mtuSize = 500;
private ReturnInterface mCallbackMtu;
// Callback for peripherals
private final BluetoothPeripheralCallback peripheralCallback = new BluetoothPeripheralCallback() {
@Override
Expand Down Expand Up @@ -178,13 +180,26 @@ public void onCharacteristicWrite(
) {
MLog.d(TAG, "onCharWrite");
}

@Override
public void onMtuChanged(BluetoothPeripheral peripheral, int mtu, int status) {
ReturnObject ret = new ReturnObject();
ret.put("deviceMac", peripheral.getAddress());
ret.put("deviceName", peripheral.getName());
ret.put("mtu", mtu);
ret.put("success", status == GATT_SUCCESS);

mHandler.post(() -> {
if (mCallbackMtu != null) mCallbackMtu.event(ret);
});
}
};
// Callback for central
private final BluetoothCentralCallback bluetoothCentralCallback = new BluetoothCentralCallback() {

@Override
public void onConnectedPeripheral(BluetoothPeripheral peripheral) {
peripheral.requestMtu(500);
peripheral.requestMtu(mtuSize);

MLog.d(TAG, "connected to '%s' " + peripheral.getName());
ReturnObject ret = new ReturnObject();
Expand All @@ -204,6 +219,13 @@ public void onConnectionFailed(BluetoothPeripheral peripheral, final int status)
@Override
public void onDisconnectedPeripheral(final BluetoothPeripheral peripheral, final int status) {
MLog.d(TAG, "disconnected '%s' with status %d" + " " + peripheral.getName() + " " + status);
ReturnObject ret = new ReturnObject();
ret.put("deviceMac", peripheral.getAddress());
ret.put("deviceName", peripheral.getAddress());
ret.put("status", "disconnected");
mHandler.post(() -> {
if (mCallbackDevice != null) mCallbackDevice.event(ret);
});

// Reconnect to this device when it becomes available again if autoConnect is true
if (!autoConnect) return;
Expand Down Expand Up @@ -248,12 +270,8 @@ public PBluetoothLEClient connectDevice(String macAddress) {
}

public PBluetoothLEClient connectDevice(String macAddress, int mtuSize) {
BluetoothPeripheral peripheral = central.getPeripheral(macAddress);
peripheral.requestConnectionPriority(CONNECTION_PRIORITY_HIGH);
peripheral.requestMtu(mtuSize);
central.connectPeripheral(peripheral, peripheralCallback);

return this;
this.mtuSize = mtuSize;
return this.connectDevice(macAddress);
}

public PBluetoothLEClient autoConnect(boolean autoConnect) {
Expand Down Expand Up @@ -288,15 +306,24 @@ public PBluetoothLEClient onNewData(ReturnInterface callback) {
return this;
}

public PBluetoothLEClient onMtuChanged(ReturnInterface callback) {
this.mCallbackMtu = callback;
return this;
}

public PBluetoothLEClient write(String value, String macAddress, String serviceUUID, String charUUID) {
return this._write(value.getBytes(), macAddress, serviceUUID, charUUID);
}

public PBluetoothLEClient _write(byte[] bytes, String macAddress, String serviceUUID, String charUUID) {
BluetoothPeripheral peripheral = central.getPeripheral(macAddress);

UUID sUUID = UUID.fromString(serviceUUID);
UUID cUUID = UUID.fromString(charUUID);
BluetoothGattCharacteristic btChar = peripheral.getCharacteristic(sUUID, cUUID);

// peripheral.writeCharacteristic(btChar, value, type);
peripheral.writeCharacteristic(btChar, value.getBytes(), WRITE_TYPE_DEFAULT);
peripheral.writeCharacteristic(btChar, bytes, WRITE_TYPE_DEFAULT);

return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,15 +217,15 @@ public void onProviderDisabled(String provider) {
satItem.put("prn", sat.getPrn());
satItem.put("snr", sat.getSnr());
sats.add(satItem);

ReturnObject ret = new ReturnObject();
ret.put("satellites", sats);
ret.put("satellitesInView", satellitesCount);
ret.put("satellitesInFix", satellitesInFix);

if (mSatellitesCallback != null) mSatellitesCallback.event(ret);
}
// MLog.d(TAG, satellitesCount + " Used In Last Fix ("+satellitesInFix+")");

ReturnObject ret = new ReturnObject();
ret.put("satellites", sats);
ret.put("satellitesInView", satellitesCount);
ret.put("satellitesInFix", satellitesInFix);

if (mSatellitesCallback != null) mSatellitesCallback.event(ret);
});
}

Expand Down