Question

I am creating an Android application which connects my device to an embedded chip via Bluetooth. I am modeling my design after a program which already exists on the Palm Pilot( written in C ). I am using the Bluetooth Chat example to get my code running, and have gotten stuck. I am pretty new to the whole coding world still, and am confusing myself slightly.

I want to convert the following code from C to Java....

SrmReceive(portId,&c,1,(t*5L*Sec)/1000L,&err)

The only real problem I'm having here, is the third parameter. The 1 represents the number of bytes to receive, as stated in the palm docs, and is of type UInt32.

The code from the Bluetooth Chat example has the following function for reading in data...

            public void run() {
                Log.i( TAG, "BEGIN mConnectedThread" );
                buffer = new byte[1024];
                int bytes;

                // Keep listening to the InputStream while connected
                while( true ) {
                try {
                    // Read from the InputStream
                    bytes = mmInStream.read( buffer );

                    // Send the obtained bytes to the UI Activity
                    mHandler.obtainMessage( BtHelperHandler.MessageType.READ, bytes, buffer).sendToTarget();
                } catch ( IOException e ) {
                    Log.e( TAG, "disconnected", e );
                    sendErrorMessage( 1 );
                    break;
                }
            }
        }

I want to change the run() method to have a parameter for the certain number of bytes to read. I am slightly confused as to what to do. I was thinking...

public void run( int size ) {
    buffer = new byte[size];
    ...
}

But that doesn't seem right to me, I feel like I'm mixing up my definition of byte. May sound stupid but I'm not confident with this information enough yet. It is confusing to me to convert a UInt32 to java, since java does not have an equivalent type. If anyone knows please let me know.

Was it helpful?

Solution

No reason to be confused, what you're doing is correct. Assuming mmInStream is an instance of InputStream;

byte[] buffer = new byte[1024];
int bytes = mmInStream.read(buffer);

Will read whatever is available up to a maximum of 1024 bytes, and return however many bytes it read. This is a low level approach to data streams, which is fine, it just means that you have to know what to do with this byte array now that you have it.

If you're reading primitives a lot, you might want to take a look at DataInputStream, use it like so;

DataInputStream dat = new DataInputStream(mmInStream);
int x = dat.readInt();

Will read 32 bits of data (4 bytes) from your stream, and line them up into a java int primitive. Things to watch out for, are endianness, and signed/unsigned conflation. If you run into problems, reading off bytes into a 32 bit int is achieved using the << operator, adding each byte in turn, then left-shifting by 8 bits.

In your situation, what I would do is just take a look at the values in buffer after a read, and figure out what exactly is being sent, eyeballing the stream could be important because of the two reasons I give above.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top