Android的蓝牙SPP连接似乎几秒钟后去死 [英] Android Bluetooth SPP connection to seems to go dead after a couple of seconds

查看:338
本文介绍了Android的蓝牙SPP连接似乎几秒钟后去死的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这里张贴在一个简单的蓝牙测试客户端应用程序主要是基于一个非常简单的程序:

<一个href=\"http://www.anddev.org/$c$c-snippets-for-android-f33/serial-over-bluetooth-simple-test-client-t11106.html\" rel=\"nofollow\">http://www.anddev.org/$c$c-snippets-for-android-f33/serial-over-bluetooth-simple-test-client-t11106.html

我的应用程序有4个按钮,每个按钮通过蓝牙连接发送数据的不同字节。

这似乎只是正常工作了几秒钟。在建立连接时,RFCOMM插座连接和前几秒的数据在连接上发送过(并且在收到对方)
完美的几秒钟后,但是,数据停止可以通过。那么无论其4个按键的我preSS什么也不会发生。

当我preSS的退出按钮(尝试使用.close()函数关闭蓝牙插座)突然所有尚未通过突然得到的数据经过一次全部(好像

然后它被储存起来在缓冲液中)在连接关闭立刻之前。和recieving器件返回到发现模式。

我不明白,为什么连接断开,并开始存储数据了,任何想法?

谢谢,
   詹姆斯

目标:Galaxy Tab的Andr​​oid版@ 2.3.3结果
Recieving装置:TI EZ430-RF2560套件的eval

 包com.launcher.LaunchControl;进口java.io.IOException异常;
进口java.io.OutputStream中;
进口java.util.UUID中;
进口android.app.Activity;
进口android.bluetooth.BluetoothAdapter;
进口android.bluetooth.BluetoothDevice;
进口android.bluetooth.BluetoothSocket;
进口android.os.Bundle;
进口android.util.Log;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.Toast;ThinBTClient扩展活动的公共类实现OnClickListener {        私有静态最后弦乐TAG =THINBTCLIENT;
        公共静态最后弦乐ADDRESS =ADDRESS;
        私有静态最终布尔D =真实的;
        私人BluetoothAdapter mBluetoothAdapter = NULL;
        私有的BluetoothSocket btSocket = NULL;
        私人的OutputStream outStream = NULL;
        字节[] = msgBuffer {0×01,0×02,×03,0×04};        私有静态最后的UUID MY_UUID = //蓝牙UUID解析到SSP端口1 ^^
                        UUID.fromString(00001101-0000-1000-8000-00805F9B34FB);        私有静态字符串地址;        / **当第一次创建活动调用。 * /
        @覆盖
        公共无效的onCreate(捆绑savedInstanceState){
                super.onCreate(savedInstanceState);
                的setContentView(R.layout.mainlaunch);                findViewById(R.id.ExitButton).setOnClickListener(本);
                findViewById(R.id.LaunchButton1).setOnClickListener(本);
                findViewById(R.id.LaunchButton2).setOnClickListener(本);
                findViewById(R.id.LaunchButton3).setOnClickListener(本);
                findViewById(R.id.LaunchButton4).setOnClickListener(本);                如果(D)的
                        Log.e(TAG,+++上创建+++);                mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
                如果(mBluetoothAdapter == NULL){
                        Toast.makeText(这一点,
                                蓝牙无法使用。
                                Toast.LENGTH_LONG).show();
                        完();
                        返回;
                }                如果(!mBluetoothAdapter.isEnabled()){
                        Toast.makeText(这一点,
                                请让您的BT和重新运行此程序。
                                Toast.LENGTH_LONG).show();
                        完();
                        返回;
                }                如果(D)的
                        Log.e(TAG,+++做过上创建,GOT LOCAL BT ADAPTER +++);                捆绑额外= getIntent()getExtras()。
                如果(临时演员!= NULL){
                    如果(extras.getString(地址)!= NULL){;
                    地址= extras.getString(地址);
                    }
                }
        }        @覆盖
        公共无效调用onStart(){
                super.onStart();
                如果(D)的
                        Log.e(TAG,++ ON START ++);
        }        @覆盖
        公共无效onResume(){
                super.onResume();                如果(D){
                        Log.e(TAG,+ ON RESUME +);
                        Log.e(TAG,+即将尝试客户端连接+);
                }                BluetoothDevice类设备= mBluetoothAdapter.getRemoteDevice(地址);                尝试{
                        btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
                }赶上(IOException异常五){
                        Log.e(TAG,ON RESUME:套接字创建失败,E);
                }
                mBluetoothAdapter.cancelDiscovery();                尝试{
                        btSocket.connect();
                        Log.e(TAG,ON RESUME:BT建立连接,数据传输链路开放。);
                }赶上(IOException异常五){
                        尝试{
                                btSocket.close();
                        }赶上(IOException异常E2){
                                Log.e(TAG,
                                        ON RESUME:无法连接失败时关闭套接字,E2);
                        }
                }
        }        @覆盖
        公共无效的onPause(){
                super.onPause();                如果(D)的
                        Log.e(TAG - ON PAUSE - );                如果(outStream!= NULL){
                        尝试{
                                outStream.flush();
                        }赶上(IOException异常五){
                                Log.e(TAG,关于暂停:莫非没有刷新输出流,E);
                        }
                }                尝试{
                        btSocket.close();
                }赶上(IOException异常E2){
                        Log.e(TAG,关于暂停:无法关闭套接字,E2);
                }
        }        @覆盖
        公共无效的onStop(){
                super.onStop();
                如果(D)的
                        Log.e(TAG - ON STOP - );
        }        @覆盖
        公共无效的onDestroy(){
                super.onDestroy();
                如果(D)的
                        Log.e(TAG---将其销毁---);
                尝试{
                    btSocket.close();
                }赶上(IOException异常E1){
                    Log.e(TAG,ON RESUME:无法连接失败时关闭套接字);
                }
        }        @覆盖
        公共无效的onClick(视图v){
            开关(v.getId())
            {
            案例R.id.ExitButton:                尝试{
                    btSocket.close();
                }赶上(IOException异常E1){
                    Log.e(TAG,ON RESUME:无法连接失败时关闭套接字);
                }
                this.finish();
                打破;
            案例R.id.LaunchButton1:                尝试{
                    outStream = btSocket.getOutputStream();
                    }
                赶上(IOException异常五){
                    Log.e(TAG,ON RESUME:输出流创建失败,E);
                    }                尝试{
                        outStream.write(msgBuffer [0]);
                }赶上(IOException异常五){
                        Log.e(TAG,ON RESUME:异常写入十一五期间,电子);
                }
                outStream = NULL;
                打破;
            案例R.id.LaunchButton2:
                尝试{
                    outStream = btSocket.getOutputStream();
                    }
                赶上(IOException异常五){
                    Log.e(TAG,ON RESUME:输出流创建失败,E);
                    }                尝试{
                        outStream.write(msgBuffer [1]);
                }赶上(IOException异常五){
                        Log.e(TAG,ON RESUME:异常写入十一五期间,电子);
                }
                outStream = NULL;
                打破;
            案例R.id.LaunchButton3:
                尝试{
                    outStream = btSocket.getOutputStream();
                    }
                赶上(IOException异常五){
                    Log.e(TAG,ON RESUME:输出流创建失败,E);
                    }                尝试{
                        outStream.write(msgBuffer [2]);
                }赶上(IOException异常五){
                        Log.e(TAG,ON RESUME:异常写入十一五期间,电子);
                }
                outStream = NULL;
                打破;            案例R.id.LaunchButton4:
                尝试{
                    outStream = btSocket.getOutputStream();
                    }
                赶上(IOException异常五){
                    Log.e(TAG,ON RESUME:输出流创建失败,E);
                    }                尝试{
                        outStream.write(msgBuffer [3]);
                }赶上(IOException异常五){
                        Log.e(TAG,ON RESUME:异常写入十一五期间,电子);
                }
                outStream = NULL;
                打破;
            }        }
}


解决方案

标签并(在我的6个不同的设备经验)所有三星设备有可怕的蓝牙SPP实现。我建议选择不同的设备。

另外,我会检查您呼叫刷新上的OutputStream,特别是如果你不发送行分隔符。

I have a fairly simple program based largely on a simple bluetooth test client application posted here:

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

My application has 4 buttons, each button sending a different byte of data over the bluetooth connection.

It seems to work just fine for a few seconds. The connection is made, the RFCOMM socket connects and for the first few seconds data is sent over the over the connection (and recieved at the other side) After a few seconds of perfection, however, the data stops getting through. Then no matter which of the 4 buttons I press nothing happens.

Then when I press the "Exit" button (which attempts to close the bluetooth socket using the .close() function) suddenly all the data that had not been getting through suddenly goes through all at once (as if it was being stored up in a buffer) immediatly before the connection is closed. and the recieving device goes back into discovery mode.

I do not understand why the connection drops, and starts storing the data up, any Ideas?

Thanks, James

Target: Galaxy Tab @ Android 2.3.3
Recieving Device: TI EZ430-RF2560 eval kit

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

        }
}

解决方案

The Tab and (in my experience with 6 different devices) all Samsung devices have TERRIBLE Bluetooth SPP implementations. I'd suggest choosing a different device.

Also, I would check that you are calling flush on the OutputStream, particularly if you are not sending line separators.

这篇关于Android的蓝牙SPP连接似乎几秒钟后去死的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆