- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当我将“M”字符串发送到设备时,我从制作字符串的地方调用时间函数。
代码:
` mManButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
man = 1;
clearScreen();
mManButton.setBackgroundResource(R.color.button_pressed);
mStartButton.setBackgroundResource(R.color.button_default);
mCalButton.setBackgroundResource(R.color.button_default);
mTestButton.setBackgroundResource(R.color.button_default);
mLinearButtton.setBackgroundResource(R.color.button_default);
mAutoButton.setBackgroundResource(R.color.button_default);
// Send a message using content of the edit text widget
sendMessage("M");
time();
}
});`
然后调用 time() 函数。在这里,如果我的日期是星期一,则变量日期设置为 1。这意味着在这个函数中我正在创建一个字符串,其中包含日期格式值。该字符串从“A”开始,以“B”结束。
代码:
private void time()
{
int day = 0;
Date now = new Date();
String sdf = new SimpleDateFormat("EEEE", Locale.ENGLISH).format(now);
switch(sdf){
case ("Monday"):
day = 1;
break;
case("Tuesday"):
day = 2;
break;
case ("Wednesday"):
day = 3;
break;
case ("Thursday"):
day = 4;
break;
case("Friday"):
day = 5;
break;
case ("Saturday"):
day = 6;
break;
case("Sunday"):
day = 7;
break;
}
int mm = Calendar.getInstance().get(Calendar.MINUTE);
int HH = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
int dd = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
int MM = Calendar.getInstance().get(Calendar.MONTH)+1;
int yy = Calendar.getInstance().get(Calendar.YEAR)%100;
if(mm<10) {
String time1 = "A" + "0" + mm + HH + "0" + day + dd + MM + yy + "B"; //suppose time1 = A041303211216B
tv7.setText("Please Wait..");
int p = 0;
while (p < time1.length())
{
char zx = time1.charAt(p);
String xz = String.valueOf(zx);
sendMessage(xz);
p++;
}
}
else if(mm>=10) {
String time2 = "A" + mm + HH + "0" + day + dd + MM + yy + "B"; **//suppose time2 = A151303211216B**
tv7.setText("Please Wait..");
int k = 0;
while (k < time2.length())
{
char zx = time2.charAt(k);
String xz = String.valueOf(zx);
sendMessage(xz);
k++;
}
}
}
创建字符串后,我将字符串的每个字符发送到 sendMessage()。
代码:
private void sendMessage(String message) {
// Check that we're actually connected before trying anything
if (mChatService.getState() !=
com.example.hasani.bluetoothterminal.BluetoothChatService.STATE_CONNECTED) {
Toast.makeText(getActivity(), R.string.not_connected, Toast.LENGTH_SHORT).show();
mStartButton.setBackgroundResource(R.color.button_default);
mCalButton.setBackgroundResource(R.color.button_default);
mTestButton.setBackgroundResource(R.color.button_default);
mManButton.setBackgroundResource(R.color.button_default);
mAutoButton.setBackgroundResource(R.color.button_default);
return;
}
// Check that there's actually something to send
if (message.length() > 0) {
// Get the message bytes and tell the BluetoothChatService to write
byte[] send = message.getBytes();
mChatService.write(send);
// Reset out string buffer to zero and clear the edit text field
mOutStringBuffer.setLength(0);
}
}
写入功能。
代码:
public void write(byte[] out) {
// Create temporary object
ConnectedThread r;
// Synchronize a copy of the ConnectedThread
synchronized (this) {
if (mState != STATE_CONNECTED) return;
r = mConnectedThread;
}
// Perform the write unsynchronized
r.write(out);
}
ConnectedThread 中的 wite代码:
public void write(byte[] buffer) {
try {
mmOutStream.write(buffer);
// Share the sent message back to the UI Activity
mHandler.obtainMessage(com.example.hasani.bluetoothterminal.Constants.MESSAGE_WRITE, -1, -1, buffer)
.sendToTarget();
} catch (IOException e) {
Log.e(TAG, "Exception during write", e);
}
}
因为里面有Handler的作用。问题是在逐步调试时,每个字符都发送到另一个设备,并且该设备接收从“A”到“B”的每个字符串,因此没有问题。
但是当我运行我的android应用程序时,发送“M”后,调用 time() 函数并发送字符串,但发送字符串的前三个字符,即;设备未收到“Amm”。我仍然不明白是什么导致了这个问题。请帮忙!。将不胜感激。谢谢!
最佳答案
哦,等等!我得到了解决方案。万一有人遇到同样的情况。在我的 onClickListener 中,我使用第二个处理程序在 5 秒延迟后调用 time() 函数。
我的 onClickListener 代码是:
mManButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
man = 1;
linear = 0;
auto = 0;
cal = 0;
test = 0;
linear = 0;
clearScreen();
mManButton.setBackgroundResource(R.color.button_pressed);
mStartButton.setBackgroundResource(R.color.button_default);
mCalButton.setBackgroundResource(R.color.button_default);
mTestButton.setBackgroundResource(R.color.button_default);
mLinearButtton.setBackgroundResource(R.color.button_default);
mAutoButton.setBackgroundResource(R.color.button_default);
// Send a message using content of the edit text widget
sendMessage("M");
tv7.setText("Please wait....");
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
time();
}
},5000);
}
});
我的 time() 函数是:
private void time() {
int day = 0;
Date now = new Date();
String sdf = new SimpleDateFormat("EEEE", Locale.ENGLISH).format(now);
switch (sdf) {
case ("Monday"):
day = 1;
break;
case ("Tuesday"):
day = 2;
break;
case ("Wednesday"):
day = 3;
break;
case ("Thursday"):
day = 4;
break;
case ("Friday"):
day = 5;
break;
case ("Saturday"):
day = 6;
break;
case ("Sunday"):
day = 7;
break;
}
int mm = Calendar.getInstance().get(Calendar.MINUTE);
int HH = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
int dd = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
int MM = Calendar.getInstance().get(Calendar.MONTH)+1;
int yy = Calendar.getInstance().get(Calendar.YEAR)%100;
String A = "A";
String min = String.format("%02d",mm);
String hour = String.format("%02d",HH);
String d = String.format("%02d",day);
String date = String.format("%02d",dd);
String month = String.format("%02d",MM);
String year = String.format("%02d",yy);
String B = "B";
String time2 = A+min+hour+d+date+month+year+B;
sendMessage(time2);
}
现在我可以根据需要接收正确的数据。我的应用程序运行得非常顺利。
关于java - 通过蓝牙从 Android 发送时,字符串中缺少前三个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41255530/
是否可以识别我周围启用了蓝牙的设备?我不需要与他们交流,只知道他们就在那里。 我正在寻找类似于 android 的 BluetouthDevice.startDiscovery() 的东西 这样的事情
蓝牙的 HTTP 代理服务是否允许我将 BLE 设备视为 HTTP 服务器,例如以便与设备对话的应用可以向其发送 GET/POST/PUT 请求? 或者这个操作是相反的方向,BLE 设备通过应用程序向
我正在与BlueZ库一起在Linux下管理蓝牙堆栈。我正在尝试打开一个套接字,该套接字应与已知UUID的特定服务连接。我已成功尝试按照以下示例在服务器和客户端之间打开套接字: http://peopl
有谁知道蓝牙设备如何获取范围内可发现设备的设备 ID? 理想情况下,我正在寻找涉及蓝牙协议(protocol)最小实现的最简单解决方案。 一个起点会很好,我只是想创建一个设备,它可以以最小的功耗存储附
蓝牙双模设备是否可以在与 BT LE 设备配对的同时被经典蓝牙发现?如果设备不能同时运行这两种模式也没关系,但我真的应该在这些模式之间切换芯片吗?我只是在 BT 4 Core 规范中找不到答案 最佳答
我目前正在开展一个涉及乐高 Mindstorms 套件的项目。砖 block 是 NXT,我对蓝牙 ping 速率很好奇。 我对其进行了 100 次 ping 测试,得到了一些有趣的结果。延迟似乎分为
我正在启动一个通过蓝牙进行无线 MIDI 连接的项目。据我所知,BT规范中没有定义MIDI配置文件。 我想知道你们中的一些人是否有兴趣分享有关通过 BT 使用 MIDI 的最佳方式的经验,特别是关于延
Closed. This question is off-topic。它当前不接受答案。
我想通过蓝牙将我的摩托罗拉机器人连接到 OBDKey。我以 BluetoothChat 为例连接蓝牙,使用 KWP 作为协议(protocol) 然后我写byte[]命令 command[0]=ra
几个月前,我用 C# 编写了一个 Messenger 程序,可以让许多客户端连接到服务器并进行聊天。 现在,我想为 android 编写相同的程序。在阅读了 Android Developers 中的
我目前正在制作一个与蓝牙相关的 Android 实用程序,我需要更改我的设备的设备发现范围.. 我有办法这样做吗?我目前正在考虑使用 TPL 来执行此操作,但我不太确定.. Android 应用程序或
我正在为两个玩家构建 tic tac,需要蓝牙连接来交换一些数据,我可以启用蓝牙,启用发现能力,但我不知道“BluetoothServerSocket”和客户端“BluetoothSocket”中的问
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况相关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visi
我正在 Microsoft visual studio express 2012,C++ 中制作一个程序,以便与具有此 mac 地址的设备建立简单的蓝牙连接:“00:12:08:24:15:50”,
我正在为 python import bluetooth 使用蓝牙模块,我相信它是 PyBluez 包。我能够从 bluetooth.BluetoothSocket 类进行连接、发送和接收,但我的应用
我正在为 python import bluetooth 使用蓝牙模块,我相信它是 PyBluez 包。我能够从 bluetooth.BluetoothSocket 类进行连接、发送和接收,但我的应用
我尝试通过以下命令来做到这一点: ./configure -developer-build -opensource -nomake examples -nomake tests make module
我有一个服务,理论上可以在没有关联 Activity 的情况下工作(因为“服务”适用于 Android 平台)。 此服务使用蓝牙,特别是注册一个具有给定名称的蓝牙服务来监听通信。当然,它必须启用蓝牙才
谁知道是否可以制作一个应用程序通过蓝牙模拟触摸屏鼠标或触控板? 如何让 PC(或 MAC)知道我是鼠标设备? 问候, 胡安 最佳答案 您应该看看蓝牙 HID 规范。这可能是可能的,具体取决于您用来模拟
我的问题很简单。我想知道什么是我的应用程序的最佳实践,以便它可以“防打瞌睡”。随着 Android N 将在更多情况下应用 Doze,这变得更加相关。 阅读时Doze Documentation有一部
我是一名优秀的程序员,十分优秀!