gpt4 book ai didi

android - 使用android设备通过SPP通过蓝牙发送数据

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:06:01 25 4
gpt4 key购买 nike

我正在尝试将数据从 Android 设备发送到支持串行端口配置文件 (SPP) 的远程蓝牙设备。我注意到每次按下按钮后打开和关闭 socket 时,它都太慢了。 Run() 和 Onclick() 函数中应该执行哪些套接字命令?下面是一个做蓝牙IO的类:

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)
...

我做错了什么?

谢谢。

最佳答案

如果您确定连接建立没有任何错误并且您可以获得套接字,请尝试在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() 调用

之前确保与主设备的连接没有丢失

关于android - 使用android设备通过SPP通过蓝牙发送数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11688336/

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