2010-09-21 129 views
2

I嘗試連接藍牙適配器,它在發送握手信息後向我發送一些數據。 我的問題是,直到適配器正在發送,代碼工作正常,但如果它停止,in.read(xxx命令塊沒有任何豁免,我必須關閉適配器並等待嚴重秒,然後發生異常, ?守則恢復 如何解決這個(我刪除了更好的理解ErrorHandling中)Android 2.1藍牙SPP出現Inputstream.read錯誤

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()); 

回答

2

那對我工作:

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; 
    } 
0

是不是「堵」是指在。 read()阻塞線程,直到數據來自藍牙?

所以如果這個監聽器在自己的線程中,你不需要任何is.available()?

+1

不,它只是阻止並且不會讀取數據,但解決方法運行時間爲1.6,並且問題似乎沒有發生在2.2+ – 2red13 2011-05-13 12:42:15