1. Download our Official Android App: Forums for Android!

Apps More bluetooth woes.

Discussion in 'Android Development' started by jonbonazza, Aug 4, 2010.

  1. jonbonazza

    jonbonazza Android Expert
    Thread Starter
    163

    Jul 13, 2010
    1,930
    457
    163
    Male
    Ok, well, thanks to the help of a fellow developer from another forum who is having a similar issue,
    I was able to get a little further with this. Basically, I have a J2SE app running on a PC that is broadcasting a string of text over bluetooth (This functionality is working fine. The string is being sent flawlessly), and an app on my android phone that reads this incomeing stream of text via an InputStream object and then sets the text property of a TextView object to contain this string of text.

    My problem is in the android app. It connects via bluetooth fine, but when it goes to read the string, it reads 0 bytes and thus, the text that is placed in the text property of the TextView object is just a bunch of garbled garbage.

    Any ideas?

    Here is the relevant code:

    Code (Text):
    1. public void onCreate(Bundle savedInstanceState)
    2.     {
    3.         super.onCreate(savedInstanceState);
    4.         setContentView(R.layout.main);
    5.         TextView textView = (TextView)findViewById(R.id.TextView);
    6.              mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    7.              mBluetoothAdapter.enable();
    8.              BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
    9.            
    10.      try
    11.       {
    12.          btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
    13.      
    14.          btSocket.connect();
    15.          inStream = btSocket.getInputStream();
    16.       }
    17.       catch (IOException e1)
    18.       {
    19.          e1.printStackTrace();
    20.       }
    21.       BufferedInputStream bis = new BufferedInputStream(inStream);
    22.       byte[] msgBuffer = new byte[1024];
    23.       int numBytes = 0;
    24.       try
    25.       {
    26.          numBytes = bis.read(msgBuffer);
    27.          
    28.       }
    29.       catch (IOException e)
    30.       {
    31.       }
    32.       try
    33.       {
    34.          btSocket.close();
    35.       }
    36.       catch (IOException e)
    37.       {
    38.          e.printStackTrace();
    39.       }
    40.      
    41.       String msg = msgBuffer.toString();
    42.       //String msg = Integer.toString(numBytes);
    43.       textView.setText(msg);
    44.    }
     

    Advertisement

Share This Page

Loading...