我有一个相当简单的程序,主要基于这里发布的简单蓝牙测试客户端应用程序:
我的应用程序有4个按钮,每个按钮通过蓝牙连接发送不同的数据字节。
似乎工作得很好几秒钟。建立连接,RFCOMM插座连接,并在最初几秒内通过连接发送数据(并在另一侧收到) 然而,经过几秒钟的完美,数据不再通过。那么无论我按下4个按钮中的哪一个都没有发生任何事情。
然后,当我按下“退出”按钮(尝试使用.close()函数关闭蓝牙套接字)时,突然所有未通过的数据突然全部通过(就好像它一样)在连接关闭之前立即存储在缓冲区中。并且接收设备返回发现模式。
我不明白为什么连接会丢失,并开始存储数据,任何想法?
谢谢, 詹姆斯
目标:Galaxy Tab @ Android 2.3.3
接收设备:TI EZ430-RF2560评估套件
package com.launcher.LaunchControl;
import java.io.IOException;
import java.io.OutputStream;
import java.util.UUID;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
public class ThinBTClient extends Activity implements OnClickListener {
private static final String TAG = "THINBTCLIENT";
public static final String ADDRESS = "ADDRESS";
private static final boolean D = true;
private BluetoothAdapter mBluetoothAdapter = null;
private BluetoothSocket btSocket = null;
private OutputStream outStream = null;
byte [] msgBuffer = {0x01, 0x02, 0x03, 0x04};
private static final UUID MY_UUID = //Bluetooth UUID to resolve to SSP Port 1 ^^
UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
private static String address;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainlaunch);
findViewById(R.id.ExitButton).setOnClickListener(this);
findViewById(R.id.LaunchButton1).setOnClickListener(this);
findViewById(R.id.LaunchButton2).setOnClickListener(this);
findViewById(R.id.LaunchButton3).setOnClickListener(this);
findViewById(R.id.LaunchButton4).setOnClickListener(this);
if (D)
Log.e(TAG, "+++ ON CREATE +++");
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
Toast.makeText(this,
"Bluetooth is not available.",
Toast.LENGTH_LONG).show();
finish();
return;
}
if (!mBluetoothAdapter.isEnabled()) {
Toast.makeText(this,
"Please enable your BT and re-run this program.",
Toast.LENGTH_LONG).show();
finish();
return;
}
if (D)
Log.e(TAG, "+++ DONE IN ON CREATE, GOT LOCAL BT ADAPTER +++");
Bundle extras = getIntent().getExtras();
if(extras !=null) {
if (extras.getString(ADDRESS) != null){;
address = extras.getString(ADDRESS);
}
}
}
@Override
public void onStart() {
super.onStart();
if (D)
Log.e(TAG, "++ ON START ++");
}
@Override
public void onResume() {
super.onResume();
if (D) {
Log.e(TAG, "+ ON RESUME +");
Log.e(TAG, "+ ABOUT TO ATTEMPT CLIENT CONNECT +");
}
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
try {
btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
Log.e(TAG, "ON RESUME: Socket creation failed.", e);
}
mBluetoothAdapter.cancelDiscovery();
try {
btSocket.connect();
Log.e(TAG, "ON RESUME: BT connection established, data transfer link open.");
} catch (IOException e) {
try {
btSocket.close();
} catch (IOException e2) {
Log.e(TAG,
"ON RESUME: Unable to close socket during connection failure", e2);
}
}
}
@Override
public void onPause() {
super.onPause();
if (D)
Log.e(TAG, "- ON PAUSE -");
if (outStream != null) {
try {
outStream.flush();
} catch (IOException e) {
Log.e(TAG, "ON PAUSE: Couldn't flush output stream.", e);
}
}
try {
btSocket.close();
} catch (IOException e2) {
Log.e(TAG, "ON PAUSE: Unable to close socket.", e2);
}
}
@Override
public void onStop() {
super.onStop();
if (D)
Log.e(TAG, "-- ON STOP --");
}
@Override
public void onDestroy() {
super.onDestroy();
if (D)
Log.e(TAG, "--- ON DESTROY ---");
try {
btSocket.close();
} catch (IOException e1) {
Log.e(TAG, "ON RESUME: Unable to close socket during connection failure");
}
}
@Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.ExitButton:
try {
btSocket.close();
} catch (IOException e1) {
Log.e(TAG, "ON RESUME: Unable to close socket during connection failure");
}
this.finish();
break;
case R.id.LaunchButton1:
try {
outStream = btSocket.getOutputStream();
}
catch (IOException e) {
Log.e(TAG, "ON RESUME: Output stream creation failed.", e);
}
try {
outStream.write(msgBuffer[0]);
} catch (IOException e) {
Log.e(TAG, "ON RESUME: Exception during write.", e);
}
outStream = null;
break;
case R.id.LaunchButton2:
try {
outStream = btSocket.getOutputStream();
}
catch (IOException e) {
Log.e(TAG, "ON RESUME: Output stream creation failed.", e);
}
try {
outStream.write(msgBuffer[1]);
} catch (IOException e) {
Log.e(TAG, "ON RESUME: Exception during write.", e);
}
outStream = null;
break;
case R.id.LaunchButton3:
try {
outStream = btSocket.getOutputStream();
}
catch (IOException e) {
Log.e(TAG, "ON RESUME: Output stream creation failed.", e);
}
try {
outStream.write(msgBuffer[2]);
} catch (IOException e) {
Log.e(TAG, "ON RESUME: Exception during write.", e);
}
outStream = null;
break;
case R.id.LaunchButton4:
try {
outStream = btSocket.getOutputStream();
}
catch (IOException e) {
Log.e(TAG, "ON RESUME: Output stream creation failed.", e);
}
try {
outStream.write(msgBuffer[3]);
} catch (IOException e) {
Log.e(TAG, "ON RESUME: Exception during write.", e);
}
outStream = null;
break;
}
}
}
答案 0 :(得分:2)
Tab和(根据我对6种不同设备的体验)所有三星设备都有可怕的蓝牙SPP实现。我建议选择其他设备。
另外,我会检查你是否在OutputStream上调用flush
,特别是如果你没有发送行分隔符。