- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
基本上,我一直在研究使用蓝牙或 wifi 的无线鼠标。我已经完成了所有工作,包括阅读和编写消息。但是,通过蓝牙传输数据的速度太慢,无法弥补。我到处都看了看,但我无法弄清楚是什么导致了这种速度。我正在使用专用线程来执行所有写入操作。
这是我的 ConnectedThread 代码(几乎与 Android SDK 示例中的完全一样)
package com.tutorials.jurko.androidmouse;
import android.bluetooth.BluetoothSocket;
import android.net.ConnectivityManager;
import java.io.BufferedOutputStream;
import java.io.IOError;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* Created by Jurko on 14/02/2015.
*/
public class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final BufferedOutputStream mmOutStream;
public static int count = 0;
public ConnectedThread(BluetoothSocket socket) {
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
try {
tmpIn = mmSocket.getInputStream();
tmpOut = mmSocket.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
mmInStream = tmpIn;
mmOutStream = new BufferedOutputStream(tmpOut);
}
public void write(Byte[] bytes) {
count++;
try {
byte x = bytes[0].byteValue();
byte y = bytes[1].byteValue();
System.out.println("Count: " + count);
byte buf[] = {x, y};
mmOutStream.write(buf);
mmOutStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
这是我的服务器代码(接收消息)
import javax.bluetooth.LocalDevice;
import javax.bluetooth.RemoteDevice;
import javax.bluetooth.UUID;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import javax.microedition.io.StreamConnectionNotifier;
import java.awt.*;
import java.awt.event.InputEvent;
import java.io.*;
/**
* Class that implements an SPP Server which accepts single line of
* message from an SPP client and sends a single line of response to the client.
*/
public class SimpleSPPServer {
//start server
private void startServer() throws IOException, AWTException {
Robot r = new Robot();
//Create a UUID for SPP
UUID uuid = new UUID("1101", true);
//Create the servicve url
String connectionString = "btspp://localhost:" + uuid +";name=Sample SPP Server";
//open server url
StreamConnectionNotifier streamConnNotifier = (StreamConnectionNotifier)Connector.open( connectionString );
//Wait for client connection
System.out.println("\nServer Started. Waiting for clients to connect...");
StreamConnection connection=streamConnNotifier.acceptAndOpen();
RemoteDevice dev = RemoteDevice.getRemoteDevice(connection);
System.out.println("Remote device address: "+dev.getBluetoothAddress());
System.out.println("Remote device name: "+dev.getFriendlyName(true));
//read string from spp client
InputStream inStream=connection.openInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inStream));
byte[] lineRead = new byte[2];
while(inStream.read(lineRead) != -1) {
System.out.println(lineRead[0] + " " + lineRead[1]);
// Code to control mouse here
}
//send response to spp client
OutputStream outStream=connection.openOutputStream();
PrintWriter pWriter=new PrintWriter(new OutputStreamWriter(outStream));
pWriter.write("Response String from SPP Server\r\n");
pWriter.flush();
pWriter.close();
streamConnNotifier.close();
}
public static void main(String[] args) throws IOException, AWTException {
//display local device address and name
LocalDevice localDevice = LocalDevice.getLocalDevice();
System.out.println("Address: "+localDevice.getBluetoothAddress());
System.out.println("Name: "+localDevice.getFriendlyName());
SimpleSPPServer sampleSPPServer=new SimpleSPPServer();
sampleSPPServer.startServer();
}
}
最佳答案
有人问了答案,所以在这里。完全出于巧合(我猜是无聊),我决定修复 Android Studio 给我的所有警告。
其中一个警告表明我正在 onDraw 函数中实例化新对象。事实证明蓝牙并不慢,但实际上 onDraw 花费了很长时间,它延迟了新消息的传输,使其看起来好像存在一致的延迟。
tl;dr:不要在 onDraw 函数中实例化新对象。
关于java - Android BluetoothSocket 输出流写入速度太慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28553514/
我有一个同时运行 3 个线程的应用程序。一个线程用于在手机和另一个蓝牙设备(Arduino)之间建立蓝牙连接。线程 2 通过蓝牙播放从另一部手机传入的音频。线程 3 录制音频并通过蓝牙将音频发送到另一
嗨我正在尝试实现一个蓝牙库,我想在其中连接一次 rfcomm 套接字,然后在所有调用中重用它。我想知道它是否已连接,以便知道我是否应该调用 connect方法。我在蓝牙套接字的源代码中找不到任何东西,
我是 Android 的新手。现在我正在尝试使用蓝牙 API 制作一个两人 Pong 游戏。我或多或少尝试过复制 Android 网站上的 BluetoothChat 教程,但在切换到 Connect
我们的设备通过蓝牙发送数据,在android应用中我们需要读取这些数据。 我能够建立蓝牙连接,接下来我调用线程以使用 BluetoothDevice 建立 BluetoothSocket 连接。此处读
我有一个应用程序,它通过蓝牙连接到 RaspberryPi,并在接收一些数据时将相同的数据循环给它。 我在连接时遇到了一些问题,因此需要使用此解决方法将我的 Android 手机连接到 Raspber
我已经创建了一个 Android 应用程序,如果我的 Android 有一个蓝牙连接到我的电脑并且我关闭了我的电脑,isConnected 仍然返回 true。有人知道这是否可以修复吗? 不知道有没有
我希望在我的 Android 应用程序中通过蓝牙执行设备到设备的文件传输。目前我已经编写了在多个蓝牙设备之间建立连接所需的代码,并且我已经检索到连接的 BluetoothSockets 以进行数据交换
我已经编写了一个用于连接外部配件的蓝牙 API。API 的设计方式是有一堆阻塞调用,例如 getTime、setTime、getVolume、setVolume等这些工作的方式是它们创建一个有效负载来
我以这种方式创建了 gpsSocket: final BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
基本上,我一直在研究使用蓝牙或 wifi 的无线鼠标。我已经完成了所有工作,包括阅读和编写消息。但是,通过蓝牙传输数据的速度太慢,无法弥补。我到处都看了看,但我无法弄清楚是什么导致了这种速度。我正在使
我应该在蓝牙输入流的 readLine 上插入超时。 BluetoothDevice device = BluetoothAdapter.getDefaultAdapter()
我遇到了这种罕见的情况,在这种情况下,我尝试将 BluetoothSocket 连接到服务器,但连接方法不会返回。这是我的代码: device = _adapter.getRe
我正在编写一个将从另一台设备接收串行数据的应用程序(不是 Android,如果重要的话它将是 Bluesmirf Silver 模块,但现在我只在我的笔记本电脑蓝牙适配器上尝试)。这是我正在使用的代码
我的程序与充当服务器的嵌入式蓝牙设备连接。我成功找到了附近的蓝牙设备,但是当我尝试连接到新设备时,我的程序因调用 BluetoothSocket.connect() 时出现 IO 异常而崩溃。我无法弄
我无法启动 Java 服务器(在 Windows 7 x64 上使用 bluecove 2.1.1,外部蓝牙加密狗)和 Android 客户端(操作系统版本 2.3。 6). 设备发现工作正常,但我无
是否有机会为设置超时 BluetoothSocket.connect(); 以便该方法在一定时间后取消? 谢谢! 最佳答案 如果你想在设备在一定时间内没有连接时取消连接尝试,那么运行一个线程。 run
我使用修改后的Google Bluetooth Chat应用程序在两个 Android 设备(Android 5 和 Android 6)之间进行客户端-服务器蓝牙 RFCOMM 通信。 我的客户端应
我正在查看 Bluetooth Chat sample application来自 Google,他们在 UI 线程上写入 BluetoothSocket 的 OutputStream。那是对的吗?通
我正在尝试连接到配对的蓝牙 设备(baracoda d-fly 条形码阅读器)。我尝试了市场上的程序 GetBlueDemo,它成功地从它的套接字中读取。 我写了自己的概念证明,但当我尝试连接到设备时
连接到蓝牙后,我需要 child 通过那个套接字发送数据。但是如何将套接字转移到 childActivity? 最佳答案 不确定它是否是最好的设计,但我在这里所做的并且它在我的简单应用程序上工作是将蓝
我是一名优秀的程序员,十分优秀!