From ff069940985bfb445a7c444cc61103bb8cb7850e Mon Sep 17 00:00:00 2001 From: wangh <123456> Date: Thu, 6 Jun 2024 17:08:19 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=20user=20epc=E8=AF=BB?= =?UTF-8?q?=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/AndroidManifest.xml | 21 +- .../com/example/jinyu_rfid/ReadActivity.java | 2 +- .../jinyu_rfid/ReadBlueToothActivity.java | 180 +++++++++++------- .../example/jinyu_rfid/WriteUserActivity.java | 1 - .../res/layout/activity_bluetooth_read.xml | 2 +- app/src/main/res/values-en-rUS/strings.xml | 1 + app/src/main/res/values-vi-rVN/strings.xml | 1 + app/src/main/res/values/strings.xml | 3 +- 8 files changed, 134 insertions(+), 77 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 5bb3005..0668cd2 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -15,11 +15,12 @@ android:label="@string/app_name" android:roundIcon="@mipmap/logo" android:supportsRtl="true" + android:usesCleartextTraffic="true" android:theme="@style/Theme.JinYuRFID" tools:targetApi="31"> + android:exported="true" /> - + android:exported="true"> + + + + + - - @@ -52,10 +55,10 @@ - - - - + + + + diff --git a/app/src/main/java/com/example/jinyu_rfid/ReadActivity.java b/app/src/main/java/com/example/jinyu_rfid/ReadActivity.java index 81c5886..1a689ee 100644 --- a/app/src/main/java/com/example/jinyu_rfid/ReadActivity.java +++ b/app/src/main/java/com/example/jinyu_rfid/ReadActivity.java @@ -91,7 +91,7 @@ public class ReadActivity extends BaseActivity implements DataReturnCall { binding.readText.setText(user); music.start(); String info = ASCIIUtil.hex2Str(user); - Log.e("TAG", "readUserInfo:" + info); + Log.e("TAG", "readUserInfo:" + user); var infos = info.split("~"); for (int i = 0; i < 12; i++) { list.add(new ReadTyreNoResult(i, stringArray[i], infos[i])); diff --git a/app/src/main/java/com/example/jinyu_rfid/ReadBlueToothActivity.java b/app/src/main/java/com/example/jinyu_rfid/ReadBlueToothActivity.java index 3da3b2e..ebeaecf 100644 --- a/app/src/main/java/com/example/jinyu_rfid/ReadBlueToothActivity.java +++ b/app/src/main/java/com/example/jinyu_rfid/ReadBlueToothActivity.java @@ -60,6 +60,7 @@ public class ReadBlueToothActivity extends BaseActivity { super.onCreate(savedInstanceState); binding = DataBindingUtil.setContentView(this, R.layout.activity_bluetooth_read); binding.setTitle(getString(R.string.go_read)); + dialog.show(); adapter = new ResultAdapter(this); list = new ArrayList<>(11); adapter.setList(list); @@ -90,37 +91,54 @@ public class ReadBlueToothActivity extends BaseActivity { } try { bluetoothSocket = device.createRfcommSocketToServiceRecord(device.getUuids()[0].getUuid()); - bluetoothSocket.connect(); - Log.e("TAG", "连接成功?:" + bluetoothSocket.isConnected()); - if (!bluetoothSocket.isConnected()) { - // 设备未连接 - Toast.makeText(context, getString(R.string.bluetooth_open_state), Toast.LENGTH_LONG).show(); - finish(); - return; - } - - inputStream = bluetoothSocket.getInputStream(); - outputStream = bluetoothSocket.getOutputStream(); - // 启动后台线程接收数据 - Thread myThread = new Thread(new Runnable() { + runOnUiThread(new Runnable() { @Override public void run() { - byte[] buffer = new byte[1024]; - int bytesRead; - try { - while ((bytesRead = inputStream.read(buffer)) != -1) { - String receivedData = new String(buffer, 0, bytesRead); - Message message = handler.obtainMessage(); - message.obj = receivedData; - handler.sendMessage(message); + new Handler().postDelayed(new Runnable() { + @Override + public void run() { + if (ActivityCompat.checkSelfPermission(context, android.Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) { + // return; + } + try { + Log.e("TAG", "开始连接"); + bluetoothSocket.connect(); + dialog.dismiss(); + Toast.makeText(context, getString(R.string.bluetooth_open_secess), Toast.LENGTH_SHORT).show(); + inputStream = bluetoothSocket.getInputStream(); + outputStream = bluetoothSocket.getOutputStream(); + outputStream.write("GA1\r".getBytes()); + // outputStream.write("RFIDPWR2700\r".getBytes()); + // 启动后台线程接收数据 + Thread myThread = new Thread(new Runnable() { + @Override + public void run() { + byte[] buffer = new byte[1024]; + int bytesRead; + try { + while ((bytesRead = inputStream.read(buffer)) != -1) { + String receivedData = new String(buffer, 0, bytesRead); + Message message = handler.obtainMessage(); + message.obj = receivedData; + handler.sendMessage(message); + } + } catch (Exception e) { + Log.e("TAG", "Error reading from input stream", e); + dialog.dismiss(); + } + } + }); + myThread.start(); + } catch (IOException e) { + // throw new RuntimeException(e); + Toast.makeText(context, getString(R.string.bluetooth_open_state), Toast.LENGTH_SHORT).show(); + finish(); + } } - } catch (IOException e) { - Log.e("TAG", "Error reading from input stream", e); - - } + }, 500); } }); - myThread.start(); + } catch (IOException e) { e.printStackTrace(); @@ -129,19 +147,48 @@ public class ReadBlueToothActivity extends BaseActivity { } - + private String user = null; + @SuppressLint("HandlerLeak") private Handler handler = new Handler() { + @Override public void handleMessage(@NonNull Message msg) { super.handleMessage(msg); String receivedData = (String) msg.obj; - Log.e("TAG", "handleMessage:" + receivedData); - if (receivedData.substring(0, 2).equals("GR")){ + if (receivedData.substring(0, 2).equals("GR")) { + var trim = receivedData.substring(2, receivedData.length()).trim(); if (activityType) { - readUserInfo(receivedData.substring(2,receivedData.length())); + if (user == null) { + user = trim; + // Log.e("TAG", "handleMessage1:" + user); + try { + outputStream.write("GR301F3E\r".getBytes()); + } catch (IOException e) { + // e.printStackTrace(); + Toast.makeText(context, getString(R.string.bluetooth_open_state), Toast.LENGTH_SHORT).show(); + finish(); + } + return; + } + user += receivedData.substring(2, receivedData.length()).trim(); + // Log.e("TAG", "handleMessage-:" + "4532383036463132303030303030303234314632323541337E33323433323034303634307E4A494E59557E3338352F36355232322E357E4A593539387E323450527E3338352F36355232322E35203136344B2032345052204A5935393820424C34454A597E4B7E3136347E3535352E307E5F7E5F7E"); + // Log.e("TAG", "handleMessage2:" + user); + readUserInfo(user); + user = null; + } else { + // 读取EPC + readEpcCodeInfo(trim); } } else { + Log.e("TAG", "读取失败:" + receivedData); + user = null; binding.readText.setText(null); + if (!list.isEmpty()) { + list.clear(); + adapter.notifyDataSetChanged(); + } + + } } @@ -149,51 +196,56 @@ public class ReadBlueToothActivity extends BaseActivity { // 读取user区 public void readInfo(View view) { - String data = binding.readText.getText().toString() + "\r"; + user = null; try { - Log.e("TAG", "发送:" + data); - outputStream.write(data.getBytes()); + outputStream.write("GR300218\r".getBytes()); } catch (IOException e) { - e.printStackTrace(); + // e.printStackTrace(); + Toast.makeText(context, getString(R.string.bluetooth_open_state), Toast.LENGTH_SHORT).show(); + finish(); } } - + // 读取EPC区 public void readEPCInfo(View view) { - + try { + outputStream.write("GR10020C\r".getBytes()); + } catch (IOException e) { + Toast.makeText(context, getString(R.string.bluetooth_open_state), Toast.LENGTH_SHORT).show(); + finish(); + } } @SuppressLint("NotifyDataSetChanged") - public void readEpcCodeInfo(String info, boolean state, String stateInfo) { - if (state) { - music.start(); - list.clear(); - binding.readText.setText(info); - Map map = new HashMap<>(); - map.put("EpcCode", info); - map.put("token", "123456"); - map.put("Language", SharedPreferencesUtils.getstring("languageIndex", "0")); - OkGo.post(url + "/readEPCCode").upRequestBody(RequestBody.create(JSON, gson.toJson(map))).execute(new MyRecultCall(dialog, this) { - @Override - public void onSuccess(Response response) { - super.onSuccess(response); - var body = response.body(); - if (body.getResultFlag().equals("1")) { - list.addAll(gson.fromJson(body.getJson(), new TypeToken>() { - }.getType())); - adapter.notifyDataSetChanged(); - } else { - Toast.makeText(ReadBlueToothActivity.this, body.getJson(), Toast.LENGTH_SHORT).show(); - adapter.notifyDataSetChanged(); - } - + public void readEpcCodeInfo(String info) { + music.start(); + list.clear(); + binding.readText.setText(info); + Map map = new HashMap<>(); + map.put("EpcCode", info); + map.put("token", "123456"); + map.put("Language", SharedPreferencesUtils.getstring("languageIndex", "0")); + OkGo.post(url + "/readEPCCode") + .upRequestBody(RequestBody.create(JSON, gson.toJson(map))) + .execute(new MyRecultCall(dialog, this) { + @Override + public void onSuccess(Response response) { + super.onSuccess(response); + var body = response.body(); + if (body.getResultFlag().equals("1")) { + list.addAll(gson.fromJson(body.getJson(), new TypeToken>() { + }.getType())); + adapter.notifyDataSetChanged(); + } else { + Toast.makeText(ReadBlueToothActivity.this, body.getJson(), Toast.LENGTH_SHORT).show(); + adapter.notifyDataSetChanged(); } - }); - } else { - binding.readText.setText(null); - } + } + }); + + } @SuppressLint("NotifyDataSetChanged") @@ -207,7 +259,7 @@ public class ReadBlueToothActivity extends BaseActivity { for (int i = 0; i < 12; i++) { try { list.add(new ReadTyreNoResult(i, stringArray[i], infos[i])); - }catch (Exception er){ + } catch (Exception er) { } } diff --git a/app/src/main/java/com/example/jinyu_rfid/WriteUserActivity.java b/app/src/main/java/com/example/jinyu_rfid/WriteUserActivity.java index c619dc7..5a7d0fa 100644 --- a/app/src/main/java/com/example/jinyu_rfid/WriteUserActivity.java +++ b/app/src/main/java/com/example/jinyu_rfid/WriteUserActivity.java @@ -104,7 +104,6 @@ public class WriteUserActivity extends BaseActivity implements DataReturnCall, P tagUserStr.append("~"); } String user = ASCIIUtil.str2Hex(tagUserStr.toString()); - if (!writeUserState) { Log.e("TAG", "请求"); Map map = new HashMap<>(); diff --git a/app/src/main/res/layout/activity_bluetooth_read.xml b/app/src/main/res/layout/activity_bluetooth_read.xml index a1fcb72..027201c 100644 --- a/app/src/main/res/layout/activity_bluetooth_read.xml +++ b/app/src/main/res/layout/activity_bluetooth_read.xml @@ -41,7 +41,7 @@ android:layout_height="match_parent" android:text="@string/scanning_area" /> - scanning area I see Bluetooth not connected, program exits + Connection successful \ No newline at end of file diff --git a/app/src/main/res/values-vi-rVN/strings.xml b/app/src/main/res/values-vi-rVN/strings.xml index ea2e751..d4075bf 100644 --- a/app/src/main/res/values-vi-rVN/strings.xml +++ b/app/src/main/res/values-vi-rVN/strings.xml @@ -37,4 +37,5 @@ Khu vực quét Tôi biết rồi Bluetooth không kết nối, chương trình thoát + Kết nối thành công \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 6c630a5..d52e2d3 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -35,5 +35,6 @@ 单机模式 扫描区域 我知道了 - 蓝牙未连接,程序退出> + 蓝牙未连接,程序退出 + 蓝牙连接成功> \ No newline at end of file