Java 用于Android蓝牙流媒体的缓冲读卡器
嘿,伙计们,我正在尝试从蓝牙设备读取一个流,连续流式传输整数,如下所示:Java 用于Android蓝牙流媒体的缓冲读卡器,java,android,bluetooth,stream,arduino,Java,Android,Bluetooth,Stream,Arduino,嘿,伙计们,我正在尝试从蓝牙设备读取一个流,连续流式传输整数,如下所示: -11 121 123 1234 -11 我在网上找到了一些代码,但要处理一些数字需要是int而不是字符串,parseInt占用了太多的CPU,我尝试使用缓冲流,但没有效果 以下是当前的方法: void beginListenForData() { final Handler handler = new Handler(); final byte delimiter = 10; //This is
-11
121
123
1234
-11
我在网上找到了一些代码,但要处理一些数字需要是int而不是字符串,parseInt占用了太多的CPU,我尝试使用缓冲流,但没有效果
以下是当前的方法:
void beginListenForData()
{
final Handler handler = new Handler();
final byte delimiter = 10; //This is the ASCII code for a newline character
stopWorker = false;
readBufferPosition = 0;
readBuffer = new byte[1024];
workerThread = new Thread(new Runnable()
{
public void run()
{
while(!Thread.currentThread().isInterrupted() && !stopWorker)
{
try
{
int bytesAvailable = mmInputStream.available();
if(bytesAvailable > 0)
{
byte[] packetBytes = new byte[bytesAvailable];
mmInputStream.read(packetBytes);
for(int i=0;i<bytesAvailable;i++)
{
byte b = packetBytes[i];
if(b == delimiter)
{
byte[] encodedBytes = new byte[readBufferPosition];
System.arraycopy(readBuffer, 0, encodedBytes, 0, encodedBytes.length);
final String data = new String(encodedBytes, "US-ASCII");
readBufferPosition = 0;
handler.post(new Runnable()
{
public void run()
{
myLabel.setText(data);
}
});
}
else
{
readBuffer[readBufferPosition++] = b;
}
}
}
}
catch (IOException ex)
{
stopWorker = true;
}
}
}
});
workerThread.start();
}
void beginListenForData()
{
最终处理程序=新处理程序();
final byte delimiter=10;//这是换行符的ASCII码
stopWorker=false;
readBufferPosition=0;
readBuffer=新字节[1024];
workerThread=新线程(new Runnable())
{
公开募捐
{
而(!Thread.currentThread().isInterrupted()&&!stopWorker)
{
尝试
{
int bytesavable=mmInputStream.available();
如果(字节可用>0)
{
byte[]packetBytes=新字节[bytesAvailable];
mmInputStream.read(packetBytes);
对于(int i=0;i使用围绕BufferedInputStream
的DataInputStream
和readInt()
方法。这当然假定网络字节顺序为整数
忘记所有这些arraycopy()
东西。使用DataInputStream
包装在BufferedInputStream
周围,以及readInt()
方法。这当然假设网络字节顺序为整数
忘记所有这些arraycopy()
东西。我尝试过使用:final-BufferedInputStream-inputStream=new-BufferedInputStream(mmInputStream);final-DataInputStream-DataInputStream=new-DataInputStream(inputStream);仍然没有显示upI尝试使用此选项:final BufferedInputStream inputStream=new BufferedInputStream(mmInputStream);final DataInputStream DataInputStream=new DataInputStream(inputStream);仍然没有显示任何内容