I know this is trivial, but it doesn’t make sense to me. Java can’t pass pointers/references as parameters, yet the read() function is passed a buffer into which the data is read, and only returns an int as the total number of bytes read into the buffer.
I am expecting to read five separate bytes from this device, but when I pass the function a buffer, and try to access it afterwards it continues to be null. If I print out the return value from the function it gives me the int 5, which is expected. But how can I access the data which was actually put into the buffer?
Here is a link to the JavaDocs….
EDIT:
This is the original call to the read function.
public boolean onOptionsItemSelected( MenuItem item ) {
switch( item.getItemId() ) {
case R.id.connect:
startActivityForResult( new Intent( this, DeviceList.class ), 1 );
return true;
case R.id.readTest:
Log.i(TAG, "Before write." );
byte[] b = {'$'};
for( int i = 0 ; i < 3 ; i++ ) {
mService.write( b );
}
Log.i(TAG, "After write." );
return true;
case R.id.readData:
byte[] c = mService.read( 5 );
Toast.makeText(this, Integer.toString( mService.bytes ), Toast.LENGTH_LONG).show();
default:
return super.onContextItemSelected( item );
}
}
Note, this read function is a function declared in my class called BluetoothService. This class contains another class called ConnectedThread, which calls the InputStream read…
Here is MY read function….
public byte[] read( int length ) {
Log.i( TAG, "Inside read." );
ConnectedThread r;
buffer = null;
synchronized( this ) {
if( mState != STATE_CONNECTED ) return null;
r = mConnectedThread;
}
Log.i(TAG, "Before run." );
r.run( length );
Log.i( TAG, "After run." );
Log.i( TAG, Integer.toString( bytes ) );
return buffer;
}
And here is the ConnectedThread class, which calls read itself….
/**
* This thread runs during a connection with a remote device.
* It handles all incoming and outgoing transmissions.
*/
private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
public ConnectedThread(BluetoothSocket socket, String socketType) {
Log.d(TAG, "create ConnectedThread: " + socketType);
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
// Get the BluetoothSocket input and output streams
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) {
Log.e(TAG, "temp sockets not created", e);
}
mmInStream = tmpIn;
mmOutStream = tmpOut;
}
public void run(int length) {
Log.i(TAG, "BEGIN mConnectedThread");
byte[] buffer = new byte[1024];
// Keep listening to the InputStream while connected
//while (true) {
try {
// Read from the InputStream
bytes = mmInStream.read(buffer, 0, length);
// Send the obtained bytes to the UI Activity
mHandler.obtainMessage(MainMenu.MESSAGE_READ, bytes, -1, buffer)
.sendToTarget();
} catch (IOException e) {
Log.e(TAG, "disconnected", e);
connectionLost();
// Start the service over to restart listening mode
BluetoothService.this.start();
//break;
}
// }
Log.i(TAG, "MADE IT HERE" );
}
/**
* Write to the connected OutStream.
* @param buffer The bytes to write
*/
public void write(byte[] buffer) {
try {
mmOutStream.write(buffer);
// Share the sent message back to the UI Activity
mHandler.obtainMessage(MainMenu.MESSAGE_WRITE, -1, -1, buffer)
.sendToTarget();
} catch (IOException e) {
Log.e(TAG, "Exception during write", e);
}
}
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) {
Log.e(TAG, "close() of connect socket failed", e);
}
}
}
You have to initialize the array, otherwise it will continue to remain null.