蓝牙与服务器连接不稳定,为什么我会失去蓝牙客户端/服务器连接?

我正尝试通过蓝牙从桌面应用程序(用Java编写)连接到Android应用程序。

对于我正在使用的桌面应用程序BlueCove API。

当我启动服务器(桌面应用程序)并启动Android应用程序时,连接工作正常。 (即,客户端发送“Hello World”,服务器将其打印在控制台中)。但是,当我离开应用程序时(通过按“返回”或“主页”按钮)并返回到它,套接字连接似乎已丢失。

为什么我会失去蓝牙客户端/服务器连接?

如何检查蓝牙插座是否已连接?

我想检查套接字的连接到没有再次连接。

我应该在,onResume方法中写什么(如果是这种情况)?

我想在onDestroy方法我应该关闭套接字。

下面是客户端服务器的源代码:

Server

Client

我使用IntentFilter检查连接的状态也尝试过,但没有奏效。

@Override

public void onCreate(Bundle savedInstanceState) {

// .....

IntentFilter filter1 = new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED);

IntentFilter filter2 = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED);

IntentFilter filter3 = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED);

this.registerReceiver(mReceiver, filter1);

this.registerReceiver(mReceiver, filter2);

this.registerReceiver(mReceiver, filter3);

}

//The BroadcastReceiver that listens for bluetooth broadcasts

private final BroadcastReceiver mReceiver = new BroadcastReceiver() {

@Override

public void onReceive(Context context, Intent intent) {

String action = intent.getAction();

BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

if (BluetoothDevice.ACTION_FOUND.equals(action)) {

//Device found

Toast.makeText(BluetoothClient.this, "Device not found", 2).show();

}

else if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {

//Device is now connected

Toast.makeText(BluetoothClient.this, "Device connected", 2).show();

}

else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {

//Done searching

Toast.makeText(BluetoothClient.this, "Done searching", 2).show();

}

else if (BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED.equals(action)) {

//Device is about to disconnect

Toast.makeText(BluetoothClient.this, "Device about to connect", 2).show();

}

else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {

//Device has disconnected

Toast.makeText(BluetoothClient.this, "Device disconnected", 2).show();

}

}

};

2011-05-19

sfat

+0

你会得到什么错误?我已经看到你已经在UI和Log中都输入了一些错误消息。 –

2011-05-21 05:17:04

+0

客户端应用程序在它再次启动时失败: 'Log.d(TAG,“即将连接”); socket.connect();' 它被卡在connect()方法中的套接字 –

2011-05-21 06:08:06

+0

那么,从我的角度来看,客户端可以在与服务器同时打开更多套接字。所以出于某种原因服务器拒绝创建另一个连接。我相信你会遇到异常情况,将复制/粘贴到堆栈跟踪的内容会很棒。无论如何,我的观点是,除非你离开你的应用程序,否则每次在OnCreate上重新创建连接都是不好的。你应该有一个外部的单身人士。 –

2011-05-21 08:18:26

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值