English 中文(简体)
Error in Pultooth SPP with Inputstream.read
原标题:Error in Android 2.1 Bluetooth SPP with Inputstream.read

I Try to connect a Bluetooth Adapter, which send me some Data after sending a Handshake. My Problem is, until the Adapter is sending, the code works fine, but if it stopped, the in.read(xxx Command blocks whithout any exeption. I have to turne off the adapter and wait severeal seconds, then an exception occurs and the Code resumed. How to solve this? (I removed the errorhandling for better understanding)

BluetoothAdapter bt_adapter = BluetoothAdapter.getDefaultAdapter();
BluetoothDevice bt_device = null;

//Bluetooth not supportet
if (bt_adapter == null) {
    return false;
}

//enable Bluetooth
if (!bt_adapter.isEnabled()) {
    Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
    return false;
}    

//search K01-Blue Adapter
Set<BluetoothDevice> pairedDevices =  bt_adapter.getBondedDevices();
if (pairedDevices.size() > 0) {
for (BluetoothDevice device : pairedDevices) {
    String name = device.getName();
    if (name.contains("K01-Blue")) {
        bt_device = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(device.getAddress());
        break;
    }
}
}

//Exit if no Adapter found
if(bt_device == null){ return false; }

//Create Socket         
try {
UUID uuid =UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
GlobalVars.bluetooth.bt_socket =GlobalVars.bluetooth.bt_device.createRfcommSocketToServiceRecord(uuid);
    } catch (Exception e) {
    e.printStackTrace();
    }

//Exit if socket not created              
if(bt_socket == null){ return false; }

/Connect to Socket, otherwise exit
try {bt_socket.connect();
    } catch (IOException e1) {return false;}

//get InputStream   
InputStream in = null;
try {in = GlobalVars.bluetooth.bt_socket.getInputStream();
    } catch (IOException e1) {return false;}

//get OutputStream
OutputStream out = null;
try {out = GlobalVars.bluetooth.bt_socket.getOutputStream()
    } catch (IOException e1)  {return false;}

//Say hello to K01 Adapter  
byte[] hello = {0x2F, 0x3F, 0x21, 0x0D, 0x0A};

try {out.write(hello);
   } catch (IOException e1) {return false;}

//Listen to Answer of K01 Adapter
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead;
byte[] data = new byte[1024];

try {
    nRead = in.read(data, 0, data.length);
    while (nRead != -1) {
        nRead = in.read(data, 0, data.length);
        buffer.write(data, 0, nRead);
    }
   } catch (IOException e1) {e1.printStackTrace();}


//Create String from Bytearray
String str1 = new String(buffer.toByteArray());
最佳回答

这对我来说是有用的:

static ByteArrayOutputStream readbuffer_to()
     {
        ByteArrayOutputStream found = new ByteArrayOutputStream();
        long start = System.currentTimeMillis();
        try {
              if(GlobalVars.bluetooth.in.available() == 0){return found;}
              int nRead = GlobalVars.bluetooth.in.read();
              boolean catched = false;
              while (true) {
                  if(GlobalVars.bluetooth.in.available() > 0)
                  {
                      found.write(nRead);
                      nRead = GlobalVars.bluetooth.in.read(); 
                      start = System.currentTimeMillis();

                  }
                    if(System.currentTimeMillis() - start > 5000) {break;} 

              } 


        } catch (IOException e1) {


        }
         return found;
     }
问题回答

难道“锁定”意味着在(......)表面上阻挡了胎面,直到从蓝色到洋的数据?

因此,如果这位听众自己听起来,你就不需要任何东西。





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签