- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在基于著名的 BluetoothChat 示例开发具有蓝牙功能的应用程序。基本上,使用此应用程序,客户端可以向服务器发送一些数据包。
我已经使用两部 Xperia 智能手机(Xperia X8 和 Xperia Sola,android 2.1 和 4.0)测试了该应用程序,一切正常:它们都可以充当客户端或服务器。
不幸的是,如果我使用 HTC Desire (android 2.3) 作为服务器,它将无法接受来自 Xperia 客户端之一的传入连接。似乎客户端 connect()
返回时一切正常,但服务器却在其 accept()
上被阻塞,就好像什么都没发生一样。
相关代码 fragment :
<强>1。 “接受话题”
private class BluetoothAcceptThread extends Thread
{
private final BluetoothServerSocket serverSocket;
public BluetoothAcceptThread()
{
BluetoothServerSocket tmpSocket = null;
try
{
Method m = bluetoothAdapter.getClass().getMethod("listenUsingRfcommOn", new Class[] {int.class});
tmpSocket = (BluetoothServerSocket) m.invoke(bluetoothAdapter, APP_BT_CHANNEL);
}
catch (NoSuchMethodException e)
{
e.printStackTrace();
}
catch (InvocationTargetException e)
{
Log.e(MainActivity.ERROR_TAG, "BluetoothAcceptThread listen() (with reflection) failed", e);
}
catch (IllegalAccessException e)
{
e.printStackTrace();
}
serverSocket = tmpSocket;
Log.d(MainActivity.DEBUG_TAG, "BluetoothAcceptThread ServerSocket created");
}
@Override
public void run()
{
BluetoothSocket socket = null;
try
{
Log.d(MainActivity.DEBUG_TAG, "BluetoothAcceptThread calling accept()...");
socket = serverSocket.accept();
Log.d(MainActivity.DEBUG_TAG, "BluetoothAcceptThread accept() returned");
}
catch (IOException e)
{
Log.e(MainActivity.ERROR_TAG, "BluetoothAcceptThread accept() failed: " + e.getMessage());
}
if (socket != null)
{
Log.d(MainActivity.DEBUG_TAG, "BluetoothAcceptThread accept() successfully");
synchronized (BluetoothManager.this)
{
if (currentState == SocketState.LISTENING || currentState == SocketState.CONNECTING)
startBluetoothConnection(socket); // all is ok, it can proceed
else if (currentState == SocketState.INACTIVE || currentState == SocketState.CONNECTED)
cancel(socket);
}
}
}
@Override
public void cancel()
{
try
{
serverSocket.close();
Log.d(MainActivity.DEBUG_TAG, "BluetoothAcceptThread ServerSocket closed");
}
catch (IOException e)
{
Log.e(MainActivity.ERROR_TAG, "BluetoothAcceptThread close() failed", e);
}
}
private void cancel(BluetoothSocket newSocket)
{
try
{
newSocket.close();
Log.d(MainActivity.DEBUG_TAG, "BluetoothAcceptThread client socket closed");
}
catch (IOException e)
{
Log.e(MainActivity.ERROR_TAG, "BluetoothAcceptThread client socket close() failed", e);
}
}
}
<强>2。 “连接线程”
private class BluetoothConnectThread extends Thread
{
private final BluetoothSocket socket;
private final BluetoothDevice device;
public BluetoothConnectThread(BluetoothDevice d)
{
device = d;
BluetoothSocket tmpSocket = null;
try
{
Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
tmpSocket = (BluetoothSocket) m.invoke(device, APP_BT_CHANNEL);
}
catch (NoSuchMethodException e)
{
e.printStackTrace();
}
catch (InvocationTargetException e)
{
Log.e(MainActivity.ERROR_TAG, "BluetoothConnectThread create() (with reflection) failed", e);
}
catch (IllegalAccessException e)
{
e.printStackTrace();
}
socket = tmpSocket;
Log.d(MainActivity.DEBUG_TAG, "BluetoothConnectThread client socket created");
}
@Override
public void run()
{
stopBluetoothDiscovery(); // otherwise it will slow down the connection
try
{
socket.connect();
Log.d(MainActivity.DEBUG_TAG, "BluetoothConnectThread connect() successfully");
}
catch (IOException e)
{
Log.e(MainActivity.ERROR_TAG, "BluetoothConnectThread connect() failed", e);
String deviceName = device != null ? device.getName() : "none";
connectionFailed(deviceName); // notify UI thread
return;
}
synchronized (BluetoothManager.this)
{
bluetoothConnectThread = null;
}
startBluetoothConnection(socket); // create the "Communication" Thread
}
@Override
public void cancel()
{
try
{
socket.close();
Log.d(MainActivity.DEBUG_TAG, "BluetoothConnectThread client socket closed");
}
catch (IOException e)
{
Log.e(MainActivity.ERROR_TAG, "BluetoothConnectThread close() failed", e);
}
}
}
<强>3。 “通信线程”(在 BluetoothChat 示例中又称为 ConnectedThread)
private class BluetoothCommunicationThread extends Thread
{
private final BluetoothSocket socket;
private final InputStream inputStream;
private final OutputStream outputStream;
public BluetoothCommunicationThread(BluetoothSocket s)
{
socket = s;
InputStream in = null;
OutputStream out = null;
try
{
in = socket.getInputStream();
out = socket.getOutputStream();
}
catch (IOException e)
{
Log.e(MainActivity.ERROR_TAG, "BluetoothCommunicationThread failed to get streams", e);
}
inputStream = in;
outputStream = out;
}
@Override
public void run()
{
byte[] buffer = new byte[BT_BUFF_SIZE];
int readBytes;
while (true)
{
try
{
readBytes = inputStream.read(buffer, 0, buffer.length);
if (readBytes != -1)
{
Message message = messageHandler.obtainMessage(DATA_MSG, readBytes, -1, buffer);
message.sendToTarget(); // notify to UI thread the bytes counter
}
else
{
BluetoothDevice device = socket.getRemoteDevice();
String deviceName = device != null ? device.getName() : "none";
connectionLost(deviceName);
break;
}
}
catch (IOException e)
{
Log.e(MainActivity.ERROR_TAG, "BluetoothCommunicationThread read() failed", e);
BluetoothDevice device = socket.getRemoteDevice();
String deviceName = device != null ? device.getName() : "none";
connectionLost(deviceName);
break;
}
}
}
public void write(byte[] buffer)
{
try
{
outputStream.write(buffer);
}
catch (IOException e)
{
Log.e(MainActivity.ERROR_TAG, "BluetoothCommunicationThread write() failed", e);
}
}
@Override
public void cancel()
{
try
{
socket.close();
Log.d(MainActivity.DEBUG_TAG, "BluetoothCommunicationThread socket closed");
}
catch (IOException e)
{
Log.e(MainActivity.ERROR_TAG, "BluetoothCommunicationThread close() failed", e);
}
}
}
所以问题的步骤如下:
accept()
connect()
connect
返回,就像连接已建立一样accept()
read()
;此函数抛出 java.io.IOException: Software caused connection abort
,可能是因为套接字未连接。最后这些是相关的 logcats:
Xperia 客户端:
09-20 00:44:23.562 9106-9106/com.powertester D/[PowerTester Debug]﹕ BluetoothConnectThread client socket created
09-20 00:44:25.704 9106-9579/com.powertester D/[PowerTester Debug]﹕ BluetoothConnectThread connect() successfully
09-20 00:44:25.734 9106-9579/com.powertester D/[PowerTester Debug]﹕ BluetoothCommunicationThread started and I/O streams ready
09-20 00:44:25.764 9106-9589/com.powertester E/[PowerTester Error]﹕ BluetoothCommunicationThread read() failed
java.io.IOException: Software caused connection abort
at android.bluetooth.BluetoothSocket.readNative(Native Method)
at android.bluetooth.BluetoothSocket.read(BluetoothSocket.java:333)
at android.bluetooth.BluetoothInputStream.read(BluetoothInputStream.java:96)
at com.powertester.net.BluetoothManager$BluetoothCommunicationThread.run(BluetoothManager.java:518)
09-20 00:44:25.844 9106-9106/com.powertester D/[PowerTester Debug]﹕ BluetoothCommunicationThread socket closed
HTC 服务器:
09-19 15:47:07.591 2422-2422/com.powertester D/[PowerTester Debug]﹕ BluetoothAcceptThread ServerSocket created
09-19 15:47:07.591 2422-2484/com.powertester D/[PowerTester Debug]﹕ BluetoothAcceptThread calling accept()...
真正奇怪的是,HTC Desire 工作如果用作客户端,其中一台 Xperia 用作服务器。
那么,是我的应用程序有问题还是 HTC Desire 蓝牙堆栈有问题?
最佳答案
经过一些麻烦后,我意识到问题出在反射本身和蓝牙 channel 的显式使用上。
使用正常方式(即 the not-hidden bluetooth methods )我的应用程序运行完美。
关于android-bluetooth - Bluetooth ServerSocket accept() 在 HTC Desire 上不返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18905973/
Bluetooth Low Energy 中计步的服务、特征值 (UUID) 是什么?蓝牙文档的相应页面未列出任何步骤。如何计算步数?步数设备是否使用运行速度和节奏服务?谢谢。 最佳答案 某些设备确实
我有 2 个 Raspberry Pi,运行 Raspbian,是最新的。每个 Pi 都有一个蓝牙 USB 加密狗。我可以使用 this. 让它们通过 RFCOMM 进行通信 现在,我想在蓝牙物理链路
在低功耗蓝牙中,一个主设备可以连接的最大从设备数量是多少?主设备可以连接的最大从设备数量是多少? Bluetooth Low Energy 有 40 个 channel ,但为什么它们只使用 3 个
我正在尝试关闭蓝牙服务,但 Bluecove 在连接关闭方法 ( https://code.google.com/p/bluecove/issues/detail?id=90 ) 上有错误,我正在尝试
我想开发像蓝牙 LE 外围设备这样的应用程序,它停止在与蓝牙 LE 中央设备连接时做广告,并限制与多个蓝牙 LE 中央设备连接的蓝牙 LE 外围设备。 一个蓝牙 LE 外围设备一次只能与一个蓝牙 LE
Eclipse 一直要我定义一个 startActivity 方法,但我不知道该在该方法中放置什么。我已经在我的 androidmanifest.xml 中启用了蓝牙。 我在下面提供了我的代码的摘录:
我想基于 BlueZ 构建一个 c 文件,但我的系统中似乎没有 bluetooth.h 文件。 fatal error: bluetooth/bluetooth.h: No such file or
我正在使用运行 jb 的 root 三星 galaxy nexus 手机,由于某种原因,我没有从蓝牙连接服务接收到任何广播 Intent 。您将在下面找到我的接收器 list 和广播接收器代码。任何提
嗨,在下面的代码中,已经打开了蓝牙电话,但它仍然要求我打开下面代码中的弹出窗口。。谁能帮帮我,我哪里做错了。。允许没有必要问已经蓝牙是
有没有人在调用 bluetoothDeive.createBond() 时遇到与以下错误消息相同的问题? android 4.4 api的方法? java.lang.SecurityException
我正在寻找有关 JSR-82 的帮助。我找了很久,甚至还找了大学的项目主管来帮忙,但到目前为止,我还是没有运气。 我的问题... 我有一个项目,我只需要扫描蓝牙设备 我还无法扫描我的蓝牙设备。我在 C
我正在基于著名的 BluetoothChat 示例开发具有蓝牙功能的应用程序。基本上,使用此应用程序,客户端可以向服务器发送一些数据包。 我已经使用两部 Xperia 智能手机(Xperia X8 和
我正在处理 Java 和 BlueCove 以找到蓝牙设备。 我创建了一个 Maven 项目并添加了 BlueCove 依赖项: net.sf.bluecove bluecove
首先,我上次尝试将 c 程序编译为 linux-arm 架构时遇到了问题,以便稍后在 android 应用程序中使用它。我成功地这样做了,稍后我会写一篇关于如何使用 NDK 独立工具链的文章。 现在我
作为我的类(class)项目之一,我想通过蓝牙耳机或任何此类蓝牙设备按下它们上的可用按钮来启动/触发我自己的 android 应用程序。我将按下蓝牙耳机上的任何特定按钮,我的应用程序应该能够检测到该按
我的项目是使用 Micro:bit 微处理器创建一个交互式程序我正在构建一个游戏,它使用钻机作为 Controller 读取旋转方向和速度作为控制输入 但是我的导师也说在游戏运行的同时给电路板供电会很
我对 Android 编程完全陌生,从技术上讲,这是我在 Android Studio 上工作的第一个大项目。我正在尝试创建一个 android 应用程序,它通过蓝牙连接到我的 Arduino 设备并
我了解蓝牙配置文件包含其他配置文件和协议(protocol)。我不明白蓝牙设备之间究竟是如何交互的。例如,如果您使用任何实现 A2DP 配置文件的附件并将其连接到 iPad,那么 iPad 会将所有音
我想检查设备上是否启用了蓝牙(以便应用程序可以在没有用户交互的情况下使用它)。有没有办法做到这一点?我也可以分别检查蓝牙和低功耗蓝牙吗? 最佳答案 我使用 Radio 完成了此操作类(class)。
我是开发带有蓝牙连接到外围设备的移动应用程序的新手。我搜索了GATT是用于蓝牙LE通信的相关配置文件,但是我们的客户建议我们使用UART服务。现在我很困惑 1.这两件事是如何关联的 2.我们是否必须选
我是一名优秀的程序员,十分优秀!