Log.i(TAG, "onCreate: 发送数据长度" + String.);
时间: 2024-02-21 19:59:26 浏览: 26
这段代码中,String后面没有指定要转换的变量或表达式,因此代码会编译错误。需要将String后面的表达式补充完整,例如:
```
int dataLength = 100;
Log.i(TAG, "onCreate: 发送数据长度" + String.valueOf(dataLength));
```
这样就可以将dataLength的值转换为字符串,并与其他字符串一起拼接成完整的日志信息,最终输出到Log中。
相关问题
以下代码无法运行,请修改以下:public class MainActivity4 extends AppCompatActivity { public static final String TAG = "petgrowing"; TextView show; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main4); show = findViewById(R.id.tv_res); } public void onClick(View btn) { Log.i(TAG, "onClick"); // 使用 Runnable 接口来动态创建新线程 new Thread(() -> { Log.i(TAG, "线程已运行"); try { Log.i(TAG, "run:正在工作..."); Document doc = Jsoup.connect("https://chl.cn/?jinri").get(); Log.i(TAG, "title=" + doc.title()); Elements tables = doc.getElementsByTag("table"); Element table1 = tables.first(); Log.i(TAG, "run:table=" + table1); Elements tds = table1.getElementsByTag("td"); Element td1 = tds.first(); Element td2 = tds.get(4); final String retStr = ("1:" + td1.text() + " 2:" + td2.text()); // 根据获取的数据更新 UI 线程上 TextView 的内容 runOnUiThread(() -> show.setText(retStr)); } catch (MalformedURLException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } }).start(); // 启动新线程 } }
可以尝试将 `onClick` 方法中的参数 `View btn` 改为 `View view`,因为该方法在布局文件中对应的控件的 `onClick` 属性中被调用时,系统会传入该控件的实例作为参数,而不是一个名为 `btn` 的参数。修改后的代码如下:
```
public class MainActivity4 extends AppCompatActivity {
public static final String TAG = "petgrowing";
TextView show;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);
show = findViewById(R.id.tv_res);
}
public void onClick(View view) {
Log.i(TAG, "onClick");
// 使用 Runnable 接口来动态创建新线程
new Thread(() -> {
Log.i(TAG, "线程已运行");
try {
Log.i(TAG, "run:正在工作...");
Document doc = Jsoup.connect("https://chl.cn/?jinri").get();
Log.i(TAG, "title=" + doc.title());
Elements tables = doc.getElementsByTag("table");
Element table1 = tables.first();
Log.i(TAG, "run:table=" + table1);
Elements tds = table1.getElementsByTag("td");
Element td1 = tds.first();
Element td2 = tds.get(4);
final String retStr = ("1:" + td1.text() + " 2:" + td2.text());
// 根据获取的数据更新 UI 线程上 TextView 的内容
runOnUiThread(() -> show.setText(retStr));
} catch (MalformedURLException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}).start(); // 启动新线程
}
}
```
// 发送指令 public void sendData(String originData){ // 获取数据 // 判断数据是字符串还是十六进制 if (originData.length() < 1) { return; } Log.d(TAG, "onCreate: 发送的原始数据 [" + originData + ']'); byte[] data = Util.str2Hex(originData); // 发送数据 sendData(data); } public void sendData(byte[] data){ // 发送数据 if (data != null && data.length > 0) { Log.d(TAG, "onCreate: 发送数据长度" + data.length); if(ble.sendData(data)){ Util.threadToast(XbyActivity.this, "发送成功"); return ; } } Util.threadToast(XbyActivity.this, "发送失败", true); } // 设置进度条 private void setProgressValue(int v){ northProgress.setProgress(v); } // 开始搜索,做一些设备清除工作 private void startSearch() { // 清理设备信息 ble.clear(); updateData(); // 搜索和蓝牙清理 clear(); ble.startSearch(); } // gatt连接断开时做的清理工作 private void clearGatt(){ // 关闭连接的蓝牙 ble.gattClose(); // 操作区域隐藏 showOpView(false, false); } // 清理工作,不会清除设备信息 private void clear() { // 提醒文字清空 showMsg(""); // 停止搜索 ble.stopSearch(); // 清理gatt clearGatt(); }解释每一句代码
这段代码是一个包含多个方法的类的部分代码,具体解释如下:
1. sendData()方法:用于发送数据。首先对传入的数据进行判断,如果数据为空或长度为0,则直接返回。如果数据不为空,则调用sendData(byte[] data)方法进行发送。该方法还使用了Util类的str2Hex()方法将字符串转换为十六进制格式的byte数组。
2. sendData(byte[] data)方法:用于发送byte数组格式的数据。如果数据不为空且长度大于0,则调用ble.sendData(data)方法进行发送。如果发送成功,则使用Util类的threadToast()方法在UI线程中显示“发送成功”提示,否则显示“发送失败”。
3. setProgressValue(int v)方法:用于设置进度条的值。
4. startSearch()方法:用于开始搜索设备。首先调用ble.clear()方法清除设备信息,并调用updateData()方法更新设备列表的显示。然后调用clear()方法清理蓝牙和gatt相关的信息,最后调用ble.startSearch()方法开始搜索设备。
5. clearGatt()方法:当gatt连接断开时调用,用于清理相关操作。首先调用ble.gattClose()方法关闭连接的蓝牙,然后调用showOpView(false, false)方法隐藏操作区域。
6. clear()方法:用于清理蓝牙和gatt相关的信息。首先调用showMsg("")方法清空提示信息,然后调用ble.stopSearch()方法停止搜索设备,并调用clearGatt()方法清理gatt信息。
总的来说,这些方法都是与蓝牙和设备搜索相关的操作,包括发送数据、更新设备列表、清理连接和搜索相关信息等。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)