(now Oracle) documentation on the Java APIs: for the original goes
into much more detail on Socket and getOutputStream(), explaining that
if you close the Output Stream, you also close the socket. Neither of
which should you do on every single line!
On Jun 28, 12:30 pm, Indicator Veritatis <mej1...@yahoo.com> wrote:
> Well, posting this larger code snippet does answer some of the
> questions. But if onCreate() is the only place you call start() on the
> ClientThread, then the problem is clear: you are closing the
> OutputStream and the BluetoothSocket prematurely. Take a look at
> BluetoothChat: neither are closed until the user is really done with
> them, or if there is an Exception BluetoothChat cannot recover from.
> You need to do the same thing.
>
> On Jun 28, 1:33 am, wu mark <androidmark2...@gmail.com> wrote:
>
> > Dear Indicator Veritatis :
>
> > Thanks for your reply.
>
> > I had coed 2 android API to pass the data from a BT Client to BT Server
> > through the Bluetooth.
>
> > It works.
>
> > Now I want to pass the data from the BT client to a non-android server (for
> > example, windows 7 server). So I try to use the hyperterminal to receive &
> > display the data.
>
> > My ckient code is simple. Could you help me to check? Thanks.
>
> > package tw.android.test.BTClient;
> > import java.io.BufferedOutputStream;
> > import java.io.IOException;
> > import java.util.UUID;
> > import android.app.Activity;
> > import android.bluetooth.BluetoothAdapter;
> > import android.bluetooth.BluetoothDevice;
> > import android.bluetooth.BluetoothSocket;
> > import android.content.Intent;
> > import android.os.Bundle;
> > import android.util.Log;
> > public class BTClient extends Activity {
> > private BluetoothAdapter adapter;
>
> > @Override
> > public void onCreate(Bundle savedInstanceState) {
> > super.onCreate(savedInstanceState);
> > setContentView(R.layout.main);
> > adapter = BluetoothAdapter.getDefaultAdapter();
> > if (!adapter.isEnabled()){
> > Intent intent = new
> > Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
> > startActivityForResult(intent, 2);
> > if (!adapter.enable()) finish();
> > }
> > // 遠端藍牙的 MAC
> > new
> > ClientThread(adapter.getRemoteDevice("00:11:94:1A:0D:51")).start();
>
> > }
> > private class ClientThread extends Thread {
> > private final BluetoothSocket mmSocket;
> > private final BluetoothDevice mmDevice;
> > public ClientThread(BluetoothDevice device) {
> > BluetoothSocket tmp = null;
> > mmDevice = device;
> > try {
> > tmp =
> > device.createRfcommSocketToServiceRecord(UUID.fromString("fa87c0d0-afac-11de-8a39-0800200c9a66"));
> > } catch (IOException e) { }
> > mmSocket = tmp;
> > }
> > public void run() {
> > adapter.cancelDiscovery();
> > try {
> > Log.i("BTClient", "start to connect");
> > mmSocket.connect();
> > Log.i("BTClient", "connecting");
> > BufferedOutputStream bout = new
> > BufferedOutputStream(mmSocket.getOutputStream());
> > bout.write("1234567再測試一下".getBytes());
> > bout.flush();
> > bout.close();
> > bout = null;
> > Log.i("BTClient", "connectless");
> > } catch (IOException connectException) {
> > Log.i("BTClient", "not connect");
> > try {
> > mmSocket.close();
> > } catch (IOException closeException) { }
> > return;
> > }
> > }
> > public void cancel() {
> > try {
> > mmSocket.close();
> > } catch (IOException e) { }
> > }
> > }
>
> > }
>
> > Thanks.
>
> > BR,
> > Mark
>
> > 2011/6/28 Indicator Veritatis <mej1...@yahoo.com>> You have not posted quite enough information for us to be sure we can
> > > help you. Can I assume, for example, that your variable 'Socket' is a
> > > BluetoothSocket? Why have you not followed any of the coding standards
> > > for the name, then? The Sun standards, and many others, all call for
> > > class names to start with capital letters, but object and primitive
> > > variables names to start with lower case. If you want people to be
> > > able to help you, make our job easier: follow the standards so that we
> > > can read the code without more effort than we feel helping you is
> > > worth.
>
> > > For that matter, how do we know you did not implement it as
> > > BluetoothServerSocket? This too, is an example of insufficient
> > > information.
>
> > > For my final example of insufficient information, what did you do in
> > > between the "first time" and the second? Did you call run() again? How
> > > do you determine when it is time to call run() again? You will need to
> > > do this to talk to the remote device again, since you closed not only
> > > the BufferedOutputStream, but even the BluetoothSocket (assuming that
> > > is what 'Socket' really is).
>
> > > BTW: how did you decide that you have to close the socket itself
> > > rather than just the output stream? Have you considered leaving the
> > > socket connected? Look at
>
> > >http://developer.android.com/resources/samples/BluetoothChat/src/com/...
> > > and see how it does it.
>
> > > On Jun 27, 2:48 am, mark2011 <androidmark2...@gmail.com> wrote:
> > > > Dear All :
>
> > > > I'm a new to Android.
>
> > > > I try to code a client API to allow the user to input some data in
> > > > an android 2.1 to send out through the bluetooth. The hyperterminal
> > > > can displays the received data in a Windows pc in the first time. But
> > > > When I input data the second time from the Android, the pc can't
> > > > receive the data any more.
>
> > > > My code is the below :
> > > > public void run() {
> > > > adapter.cancelDiscovery();
>
> > > > try {
> > > > Socket.connect();
> > > > BufferedOutputStream bout = new
> > > > BufferedOutputStream(Socket.getOutputStream());
> > > > bout.write(str1.getBytes());
>
> > > > bout.flush();
> > > > bout.close();
> > > > } catch (IOException connectException) {
> > > > Log.i("BT Client", "not connect");
> > > > try {
> > > > Socket.close();
> > > > } catch (IOException closeException) { }
> > > > return;
> > > > }
> > > > }
>
> > > > Can anyone help me?
>
> > > > Thanks in advanced.
>
> > > > BR,
> > > > Mark
>
> > > --
> > > You received this message because you are subscribed to the Google
> > > Groups "Android Developers" group.
> > > To post to this group, send email to android-developers@googlegroups.com
> > > To unsubscribe from this group, send email to
> > > android-developers+unsubscribe@googlegroups.com
> > > For more options, visit this group at
> > >http://groups.google.com/group/android-developers?hl=en
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
No comments:
Post a Comment