我正在使用一个Android应用程序,它使用蓝牙连接在我的Android智能手机和非Android蓝牙模块之间传输数据,使用SPP配置文件.我使用Android Developer网站的蓝牙聊天示例作为参考.
我已经成功地将两个设备相互连接,并将简单的字符串从智能手机发送到蓝牙模块.但是我在读取从模块发回的数据时遇到了一些错误.我使用以下代码,与蓝牙聊天示例完全相同,从InputStream读取数据
while (true) {
try {
// Read from the InputStream
bytes = mmInStream.read(buffer);
String str = new String(buffer);
Log.i(TAG, "mmInStream - " + str);
// Send the obtained bytes to the UI Activity
mHandler.obtainMessage(BluetoothChat.MESSAGE_READ, bytes, -1, buffer)
.sendToTarget();
} catch (IOException e) {
Log.e(TAG, "disconnected", e);
connectionLost();
break;
}
}
Run Code Online (Sandbox Code Playgroud)
当我的蓝牙模块向手机发送一个简单的字符串时,没有正确接收该字符串.它以随机的方式分成几个部分.例如,如果我将三次"1234567890abcdef1234567890abcdef0123456789"发送到手机,Eclipse上的Logcat将记录这些:
mmInstream - 12345678910abcdef????????(continuing null)
mmInstream - 1????????(continuing null)
mmInstream - 2345678910abcdef0123456789????????(continuing null)
Run Code Online (Sandbox Code Playgroud)
首次.在第二次和第三次传输数据时,它会收到差异:
mmInstream - 1234567891???????(continuing null)
mmInstream - 0abcdef012???????(continuing null)
mmInstream - 3456789?????????(continuing null)
mmInstream …Run Code Online (Sandbox Code Playgroud) 我正在开发一个运行Linux的嵌入式系统的应用程序.
就我而言,我有一个非常大的文件(与系统的功能相比)作为输入.该文件有一个小标题,其大小只有几百字节.在我的应用程序中,我需要从文件中删除该标头,以便该文件没有标头并仅包含相关数据.通常,我实现如下(伪代码):
char *input_file = "big_input.bin";
char *tmp_file1 = "header.bin";
char *tmp_file2 = "data.bin";
/* Copy the content of header from input file to tmp_file1 */
_copy_header(tmp_file1, input_file);
/* Copy the data from input file to tmp_file2 */
_copy_data(tmp_file2, input_file);
/* Rename temp file to input file */
unlink(input_file);
rename(tmp_file2, input_file);
Run Code Online (Sandbox Code Playgroud)
这种方法的问题在于它创建了一个临时文件,tmp_file2其大小几乎与输入文件一样大(因为标头非常小).在我的系统中,一切都存储在RAM中,这是非常有限的.创建大型临时文件会导致内存不足错误.
那么如何避免创建一个大的临时文件呢?
提前致谢!
我正在做 Rustlings 课程 Traits4.rs 练习。任务基本上是为compare_license_types函数选择正确的签名。使用impl Trait如下语法效果很好:
pub trait Licensed {
fn licensing_info(&self) -> String {
"some information".to_string()
}
}
struct SomeSoftware {}
struct OtherSoftware {}
impl Licensed for SomeSoftware {}
impl Licensed for OtherSoftware {}
fn compare_license_types(software: impl Licensed, software_two: impl Licensed) -> bool
{
software.licensing_info() == software_two.licensing_info()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn compare_license_information() {
let some_software = SomeSoftware {};
let other_software = OtherSoftware {};
assert!(compare_license_types(some_software, other_software));
}
#[test]
fn compare_license_information_backwards() { …Run Code Online (Sandbox Code Playgroud)