これは些細なことですが、私には意味がありません。Javaはポインタ/参照をパラメータとして渡すことはできませんが、read()関数にはaが渡さbuffer into which the data is read
れ、intは。としてのみ返されthe total number of bytes read into the buffer
ます。
このデバイスから5つの個別のバイトを読み取ることを期待していますが、関数にバッファーを渡し、後でアクセスしようとすると、引き続きnull
。関数からの戻り値を出力すると、int 5
期待どおりのが得られます。しかし、実際にバッファに入れられたデータにアクセスするにはどうすればよいですか?
これがJavaDocsへのリンクです...。
編集:
これは、read関数への最初の呼び出しです。
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 );
}
}
この読み取り関数は、BluetoothServiceと呼ばれる私のクラスで宣言された関数であることに注意してください。このクラスには、InputStream読み取りを呼び出すConnectedThreadと呼ばれる別のクラスが含まれています...
これが私の読み取り機能です。
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;
}
そして、これがConnectedThreadクラスで、read自体を呼び出します。
/**
* 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);
}
}
}