gpt4 book ai didi

几秒钟后,Android 蓝牙 SPP 连接似乎断开了

转载 作者:行者123 更新时间:2023-11-29 22:24:25 32 4
gpt4 key购买 nike

我有一个相当简单的程序,主要基于此处发布的简单蓝牙测试客户端应用程序:

http://www.anddev.org/code-snippets-for-android-f33/serial-over-bluetooth-simple-test-client-t11106.html

我的应用程序有 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;
}

}
}

最佳答案

Tab 和(根据我使用 6 种不同设备的经验)所有三星设备都有糟糕的蓝牙 SPP 实现。我建议选择不同的设备。

此外,我会检查您是否在 OutputStream 上调用 flush,特别是如果您没有发送行分隔符。

关于几秒钟后,Android 蓝牙 SPP 连接似乎断开了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6326759/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com