2012-07-27 230 views
4

我試圖將數據從android設備發送到支持串行端口配置文件(SPP)的遠程藍牙設備。每次按下按鈕後,我都會注意到每次打開和關閉套接字時,它都太慢了。 Run()和Onclick()函數應該執行哪些套接字命令?以下是這確實藍牙IO類:使用Android設備通過SPP通過藍牙發送數據

public class Selecteddevice extends Activity implements OnClickListener { 

private static final String TAG = "THINBTCLIENT"; 
private BluetoothAdapter mBluetoothAdapter = null; 
private BluetoothDevice device; 
private BluetoothSocket btSocket = null; 
private OutputStream outStream = null; 

private static final UUID MY_UUID = 
     UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); 
public static String address; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    this.setContentView(R.layout.selecteddevice); 
    findViewById(R.id.toggleButton1).setOnClickListener(this); 
    findViewById(R.id.toggleButton2).setOnClickListener(this); 
    findViewById(R.id.toggleButton3).setOnClickListener(this); 

} 

@Override 
public void onStart() { 
    super.onStart(); 

    String address = getIntent().getStringExtra("address"); 
    TextView tv1 = (TextView) findViewById(R.id.textView_address); 
    tv1.setText("      DEVICE ADDRESS:  " + address); 

    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
    device = mBluetoothAdapter.getRemoteDevice(address); 
    run(); 
} 

public void run(){ 
    mBluetoothAdapter.cancelDiscovery(); 
    try { 
     btSocket = device.createRfcommSocketToServiceRecord(MY_UUID); 

    } catch (IOException e) 
    { 
     Log.e(TAG, "ON START: Socket creation failed.", e); 
    } 

    try { 
     btSocket.connect(); 

    } catch (IOException e) 
    {  
     Log.e(TAG, "sendTestByte: Socket connection failed.", e); 
    } 
} 

public void sendTestString(String s){ 
    try { 
     outStream = btSocket.getOutputStream(); 

    } catch (IOException e) 
    {  
     Log.e(TAG, "sendTestByte: OutputStream creation failed.", e); 
    } 

    try { 
     outStream.write(s.getBytes()); 
     Log.d(TAG, "sendTestByte: OutputStream write succeeded."); 

    } catch (IOException e) 
    {  
     Log.e(TAG, "sendTestByte: OutputStream writefailed.", e); 
    }  
} 


public void onClick(View v){ 
    switch(v.getId()) 
    { 
    case R.id.toggleButton1: 
     this.sendTestString("1"); 
     break; 
    case R.id.toggleButton2: 
     this.sendTestString("2"); 
     break; 
    case R.id.toggleButton3: 
     this.sendTestString("3"); 
     break; 
    } 
}   

@Override 
public void onPause() { 
    super.onPause(); 
    if (outStream != null) { 
     try { 
      outStream.flush(); 
     } catch (IOException e5) 
     { 
      Log.e(TAG, "ON PAUSE: Couldn't flush output stream.", e5); 
     } 
    } 
    try { 
     btSocket.close(); 
    } catch (IOException e6) 
    { 
     Log.e(TAG, "ON PAUSE: Unable to close socket.", e6); 
    } 
} 

@Override 
public void onStop() { 
    super.onStop(); 

} 

@Override 
public void onDestroy() { 
    super.onDestroy(); 

} 

}


我與錯誤信息配對後的程序崩潰:

07-27 13:00:57.483: E/THINBTCLIENT(7855): sendTestByte: OutputStream writefailed. 
    07-27 13:00:57.483: E/THINBTCLIENT(7855): java.io.IOException: socket closed 
    07-27 13:00:57.483: E/THINBTCLIENT(7855): at 
    android.bluetooth.BluetoothSocket.write(BluetoothSocket.java:331) 
    ... 

我在做什麼錯?

謝謝。

+0

之前不會丟失什麼是您需要的那種藍牙I/O的最小測試用例。所有的藍牙操作都可能發生在一個測試功能之內嗎?如果你可以提供,它可能更容易調試。 – speciousfool 2012-07-31 09:56:47

+0

您是否在清單中設置了正確的權限? – ligi 2012-07-31 10:54:43

+0

我有同樣的問題..你有沒有得到任何解決方案? – 2012-08-09 05:29:34

回答

3

如果你確信沒有任何錯誤連接建立後,你可以得到的插座,嘗試在run()方法分配的OutputStream成員如下:

public void run() 
{ 
    mBluetoothAdapter.cancelDiscovery(); 
    try 
    { 
     btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);   
    } catch (IOException e) 
    { 
     Log.e(TAG, "ON START: Socket creation failed.", e); 
    } 

    try 
    { 
     btSocket.connect();  
    } catch (IOException e) 
    {  
     Log.e(TAG, "sendTestByte: Socket connection failed.", e); 
    } 

    try 
    { 
     outStream = btSocket.getOutputStream(); 
    } catch (IOException e) 
    {  
     Log.e(TAG, "sendTestByte: OutputStream creation failed.", e); 
    } 
} 

public void sendTestString(String s) 
{  
    try 
    { 
     outStream.write(s.getBytes()); 
     outSttream.flush(); // <-- Try flush to force sending data in buffer 
     Log.d(TAG, "sendTestByte: OutputStream write succeeded."); 
    } catch (IOException e) 
    {  
     Log.e(TAG, "sendTestByte: OutputStream writefailed.", e); 
    }  
} 

你實際上並沒有關閉套接字,但這應該管用。確保與主設備的連接在write()調用

相關問題