Abubakar Saad
Abubakar Saad

Reputation: 222

Is there a way to properly read raw data from ionic bluetooth module?

I'm trying to read raw data from a device through ionic Bluetooth serial. The device sends the app 506 bytes per transmission and waits for app response when the app response is "OK", device sends next 506 bytes.

Sometimes the app does not receive 506 bytes in one transmission, which is very weird because a device is sending 506 bytes (this has been confirmed). For some reason, the app does not receive 506 bytes. It only receives a chunk of it and whatever was dropped or left out gets send afterward. Every 506 bytes has CRC and sometimes CRC fails (which is fine) but when the app asked for resending of the last 506 bytes, CRC will fail again. But there is nothing wrong with the device (this has been confirmed). My assumption something is happening when the data gets to the Bluetooth buffer in ionic/Cordova.

My question is I'm doing something wrong here? Is it possible I'm not reading the data correctly? has anyone run into this type of problem?

Things I tried: I tried adding time delays before I start asking for the data. That did not work at all. I tried asking the device to restart the process of sending data from the beginning, that also does not work.

async getBTRawData(): Promise<any> {
        let result = new Array();

        const res = this.bluetoothSerial.subscribeRawData().subscribe((data) => {
            let buffer = new Uint8Array(data);

            console.log("buffer items: ", buffer);

            buffer.forEach((item) => {
                result.push(item);
            });

            // this.bluetoothSerial.clear().then(data => {
            //     console.log("Is buffer clear before reciving new messages?:", data);
            // });
        }, err => {
            console.log("err: ", err);
        });

        return result;
    }


I expect to get 506 bytes every time without a loss since the cellphone and the device are right beside each other. I understand if CRC fails one or two times, but resend should fix that issue. It'll be awesome if the 506 bytes do not break into chunk when received by the app.

Edit: Data looks like this: Raw data

Edit2: This is how the data is being read by the bluetooth serial: Data from bluetooth serial

Edit3: Raw Data from subscribe: enter image description here

Upvotes: 0

Views: 1124

Answers (1)

Abubakar Saad
Abubakar Saad

Reputation: 222

@T.J Crowder Thank you for the guidance. Yes, I did have to use observable in order to read the data from the device, as data is a stream. Everything works now.

Upvotes: 0

Related Questions