|
@@ -18,6 +18,7 @@ import android.view.MenuItem;
|
|
|
import android.view.View;
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
import com.joanzapata.iconify.widget.IconTextView;
|
|
|
|
|
|
import java.io.UnsupportedEncodingException;
|
|
@@ -33,6 +34,7 @@ import cn.minbb.producttester.R;
|
|
|
import cn.minbb.producttester.adapter.BasicDeviceAdapter;
|
|
|
import cn.minbb.producttester.ctrl.App;
|
|
|
import cn.minbb.producttester.ctrl.BluetoothChatService;
|
|
|
+import cn.minbb.producttester.utils.DataTranslation;
|
|
|
|
|
|
public class BasicDeviceActivity extends AppCompatActivity {
|
|
|
|
|
@@ -46,6 +48,11 @@ public class BasicDeviceActivity extends AppCompatActivity {
|
|
|
private BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
|
|
|
private BluetoothChatService chatService = null;
|
|
|
|
|
|
+ private BasicDeviceAdapter basicDeviceAdapter;
|
|
|
+ private List<BasicDeviceAdapter.ItemBean> itemBeanList = new ArrayList<>();
|
|
|
+ // 输出流缓冲区
|
|
|
+ private StringBuffer outStringBuffer;
|
|
|
+
|
|
|
static {
|
|
|
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
|
|
|
}
|
|
@@ -58,14 +65,8 @@ public class BasicDeviceActivity extends AppCompatActivity {
|
|
|
|
|
|
App.setupActionBar(this);
|
|
|
noDevice.setVisibility(View.VISIBLE);
|
|
|
- List<BasicDeviceAdapter.ItemBean> itemBeanList = new ArrayList<>();
|
|
|
- itemBeanList.add(new BasicDeviceAdapter.ItemBean("123", "456"));
|
|
|
- itemBeanList.add(new BasicDeviceAdapter.ItemBean("321", "654"));
|
|
|
- itemBeanList.add(new BasicDeviceAdapter.ItemBean("789", "987"));
|
|
|
- itemBeanList.add(new BasicDeviceAdapter.ItemBean("123", "456"));
|
|
|
- itemBeanList.add(new BasicDeviceAdapter.ItemBean("321", "654"));
|
|
|
- itemBeanList.add(new BasicDeviceAdapter.ItemBean("789", "987"));
|
|
|
- BasicDeviceAdapter basicDeviceAdapter = new BasicDeviceAdapter(this, itemBeanList);
|
|
|
+
|
|
|
+ basicDeviceAdapter = new BasicDeviceAdapter(this, itemBeanList);
|
|
|
contentRecyclerView.setAdapter(basicDeviceAdapter);
|
|
|
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
|
|
|
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
|
|
@@ -166,14 +167,19 @@ public class BasicDeviceActivity extends AppCompatActivity {
|
|
|
startActivityForResult(new Intent(BasicDeviceActivity.this, ScanDeviceActivity.class), 0);
|
|
|
break;
|
|
|
case R.id.opUp:
|
|
|
+ sendDataToDevice("UD\r\nUU\r\n", false);
|
|
|
break;
|
|
|
case R.id.opLeft:
|
|
|
+ sendDataToDevice("LD\r\nLU\r\n", false);
|
|
|
break;
|
|
|
case R.id.opCenter:
|
|
|
+ sendDataToDevice("SD\r\nSU\r\n", false);
|
|
|
break;
|
|
|
case R.id.opRight:
|
|
|
+ sendDataToDevice("RD\r\nRU\r\n", false);
|
|
|
break;
|
|
|
case R.id.opDown:
|
|
|
+ sendDataToDevice("DD\r\nDU\n", false);
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
@@ -193,6 +199,27 @@ public class BasicDeviceActivity extends AppCompatActivity {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private void sendDataToDevice(String data, boolean hex) {
|
|
|
+ // 确保已连接
|
|
|
+ if (null == chatService || chatService.getState() != BluetoothChatService.STATE_CONNECTED) {
|
|
|
+ Toast.makeText(this, "设备未连接", Toast.LENGTH_SHORT).show();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // 检测是否有字符串发送
|
|
|
+ if (data.length() > 0) {
|
|
|
+ // 获取字符串并告诉 BluetoothChatService 发送
|
|
|
+ if (hex) {
|
|
|
+ chatService.write(DataTranslation.hexString2Bytes(data));
|
|
|
+ } else {
|
|
|
+ chatService.write(data.getBytes());
|
|
|
+ }
|
|
|
+ // 清空输出缓冲区
|
|
|
+ outStringBuffer.setLength(0);
|
|
|
+ } else {
|
|
|
+ Toast.makeText(this, "发送内容不能为空", Toast.LENGTH_SHORT).show();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
@SuppressLint("HandlerLeak")
|
|
|
private final Handler mHandler = new Handler() {
|
|
|
@Override
|
|
@@ -227,16 +254,24 @@ public class BasicDeviceActivity extends AppCompatActivity {
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
- System.err.println("/" + readMessage);
|
|
|
-
|
|
|
- Pattern p = Pattern.compile("\\{.*?\\}");
|
|
|
- Matcher m = p.matcher(readMessage);
|
|
|
- // 判断正则表达式是否匹配到
|
|
|
- if (m.find()) {
|
|
|
- System.out.println(m.group());
|
|
|
- } else {
|
|
|
- System.out.println("未找到");
|
|
|
+ if (null != readMessage) {
|
|
|
+ System.err.println("/" + readMessage);
|
|
|
+ Pattern p = Pattern.compile("\\{.*?\\}");
|
|
|
+ Matcher m = p.matcher(readMessage);
|
|
|
+ // 判断正则表达式是否匹配到
|
|
|
+ if (m.find()) {
|
|
|
+ String data = m.group();
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(data);
|
|
|
+ itemBeanList.clear();
|
|
|
+ for (String key : jsonObject.keySet()) {
|
|
|
+ itemBeanList.add(new BasicDeviceAdapter.ItemBean(key, jsonObject.getString(key)));
|
|
|
+ }
|
|
|
+ basicDeviceAdapter.notifyDataSetChanged();
|
|
|
+ } else {
|
|
|
+ System.out.println("未匹配到数据");
|
|
|
+ }
|
|
|
}
|
|
|
+
|
|
|
//检错误码计算函数
|
|
|
// if (inhex == true) {
|
|
|
// String readMessage = " " + Data_syn.bytesToHexString(readBuf, msg.arg1);
|
|
@@ -255,6 +290,7 @@ public class BasicDeviceActivity extends AppCompatActivity {
|
|
|
case BluetoothChatService.MESSAGE_TOAST:
|
|
|
Toast.makeText(getApplicationContext(), msg.getData().getString(BluetoothChatService.TOAST), Toast.LENGTH_SHORT).show();
|
|
|
break;
|
|
|
+ default:
|
|
|
}
|
|
|
}
|
|
|
};
|