use of nodomain.freeyourgadget.gadgetbridge.service.devices.pebble.ble.PebbleLESupport in project Gadgetbridge by Freeyourgadget.
the class PebbleIoThread method connect.
@Override
protected boolean connect() {
String deviceAddress = gbDevice.getAddress();
GBDevice.State originalState = gbDevice.getState();
gbDevice.setState(GBDevice.State.CONNECTING);
gbDevice.sendDeviceUpdateIntent(getContext());
try {
// contains only one ":"? then it is addr:port
int firstColon = deviceAddress.indexOf(":");
if (firstColon == deviceAddress.lastIndexOf(":")) {
mIsTCP = true;
InetAddress serverAddr = InetAddress.getByName(deviceAddress.substring(0, firstColon));
mTCPSocket = new Socket(serverAddr, Integer.parseInt(deviceAddress.substring(firstColon + 1)));
mInStream = mTCPSocket.getInputStream();
mOutStream = mTCPSocket.getOutputStream();
} else {
mIsTCP = false;
if (gbDevice.getVolatileAddress() != null && prefs.getBoolean("pebble_force_le", false)) {
deviceAddress = gbDevice.getVolatileAddress();
}
BluetoothDevice btDevice = mBtAdapter.getRemoteDevice(deviceAddress);
if (btDevice.getType() == BluetoothDevice.DEVICE_TYPE_LE) {
LOG.info("This is a Pebble 2 or Pebble-LE/Pebble Time LE, will use BLE");
mInStream = new PipedInputStream();
mOutStream = new PipedOutputStream();
mPebbleLESupport = new PebbleLESupport(this.getContext(), btDevice, (PipedInputStream) mInStream, (PipedOutputStream) mOutStream);
} else {
ParcelUuid[] uuids = btDevice.getUuids();
if (uuids == null) {
return false;
}
for (ParcelUuid uuid : uuids) {
LOG.info("found service UUID " + uuid);
}
mBtSocket = btDevice.createRfcommSocketToServiceRecord(uuids[0].getUuid());
mBtSocket.connect();
mInStream = mBtSocket.getInputStream();
mOutStream = mBtSocket.getOutputStream();
}
}
} catch (IOException e) {
e.printStackTrace();
gbDevice.setState(originalState);
gbDevice.sendDeviceUpdateIntent(getContext());
mInStream = null;
mOutStream = null;
mBtSocket = null;
return false;
}
mPebbleProtocol.setForceProtocol(prefs.getBoolean("pebble_force_protocol", false));
mIsConnected = true;
write(mPebbleProtocol.encodeFirmwareVersionReq());
gbDevice.setState(GBDevice.State.CONNECTED);
gbDevice.sendDeviceUpdateIntent(getContext());
return true;
}
Aggregations