I'm new to reactive programming and this library and I'm trying to write an android app that will be able to send messages back and forth with other phones having the same app.
I've written a BleAdvertiser
class which basically broadcasts the service UUID, I then use RxAndroidBle
to identify other devices and attempt a connection. It would appear that I'm not actually making a connection, however, no errors are being thrown. What am I doing wrong?
On create I look for other phones with the same app. The Log statement that prints device name and MAC address is working as expected, I can see my test device. So I'm good there.
@Override
protected void onCreate(Bundle savedInstanceState) {
...
Intent intent = new Intent(this, BluetoothLeService.class);
startService(intent);
BleAdvertiser bleAdvertiser = new BleAdvertiser(new AdvertisingPacket(new Date(System.currentTimeMillis())));
Thread t = new Thread(bleAdvertiser);
t.start();
SERVICE = UUID.fromString("e12b9e62-5c03-4ca2-88a5-1034e494f4dc");
CHARACTERISTIC = UUID.fromString("201274dd-04dc-4ce6-943c-a830df466b33");
PUUID = ParcelUuid.fromString(SERVICE.toString());
rxBleClient = RxBleClient.create(this);
scanSubscription = rxBleClient.scanBleDevices(
new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_BALANCED).build(),
new com.polidea.rxandroidble.scan.ScanFilter[]{new ScanFilter.Builder().setServiceUuid(PUUID).build()}
)
.subscribe(
scanResult -> {
//process new ble device.
Log.d("Scan Result: ", scanResult.getBleDevice().getMacAddress() + " : " + scanResult.getBleDevice().getName() );
ConnectToDevice(scanResult.getBleDevice());
},
throwable -> {
Toast.makeText(this,throwable.getMessage(),Toast.LENGTH_LONG ).show();
}
);
...
}
So we try to connect:
private void ConnectToDevice(RxBleDevice device) {
device.establishConnection(true)
.flatMap(rxBleConnection -> rxBleConnection.readCharacteristic(CHARACTERISTIC)
.doOnNext(bytes-> {
//process read data (convert to message? )
try {
Toast.makeText(this,"stuff happened in what we think is read bytes.",Toast.LENGTH_SHORT).show();
ArrayList<TinCanMessage> receivedMessages = (ArrayList<TinCanMessage>) Serializer.deserialize(bytes);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
})
.flatMap(bytes -> {
Toast.makeText(this,"stuff happened in what we think is write bytes.",Toast.LENGTH_SHORT).show();
try {
return rxBleConnection.createNewLongWriteBuilder()
.setCharacteristicUuid(CHARACTERISTIC)
.setBytes(Serializer.serialize(new TinCanMessage("New messages will go here.", "kyle")))
.build();
} catch (IOException e) {
e.printStackTrace();
}
return null;
})
)
.subscribe(
bytes -> {
//Written data
//assign to fragment and update adapter?
Toast.makeText(this, "stuff happened in the written data section", Toast.LENGTH_SHORT).show();
},
throwable -> {
Toast.makeText(this, throwable.getMessage(), Toast.LENGTH_LONG);
}
);
}
All help is appreciated.
Update
The devices actually are connecting. I've verified this by printing out to the debugger. I thought that I had an issue because I was trying to verify the connection with toast messages which are not displaying. This was discovered via comments from Darkusz Seweryn, so I've accepted his answer. My question was built on faulty assumptions :P