完成 user epc读取

master
wangh 6 months ago
parent d0d8042a70
commit ff06994098

@ -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">
<activity
android:name=".BlueToothServerActivity"
android:exported="true"/>
android:exported="true" />
<receiver
android:name=".broadcast.ScanERCodeReceiver"
@ -36,13 +37,15 @@
</activity>
<activity
android:name=".ReadActivity"
android:exported="true" />
android:exported="true">
<!-- <intent-filter>-->
<!-- <action android:name="android.intent.action.MAIN" />-->
<!-- <category android:name="android.intent.category.LAUNCHER" />-->
<!-- </intent-filter>-->
</activity>
<activity
android:name=".ReadBlueToothActivity"
android:exported="true" />
<activity
android:name=".WriteUserActivity"
android:exported="true" />
@ -52,10 +55,10 @@
<activity
android:name=".MainActivity"
android:exported="true">
<!-- <intent-filter> -->
<!-- <action android:name="android.intent.action.MAIN" /> -->
<!-- <category android:name="android.intent.category.LAUNCHER" /> -->
<!-- </intent-filter> -->
<!-- <intent-filter>-->
<!-- <action android:name="android.intent.action.MAIN" />-->
<!-- <category android:name="android.intent.category.LAUNCHER" />-->
<!-- </intent-filter>-->
</activity>
</application>

@ -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]));

@ -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<String, String> map = new HashMap<>();
map.put("EpcCode", info);
map.put("token", "123456");
map.put("Language", SharedPreferencesUtils.getstring("languageIndex", "0"));
OkGo.<MyResult>post(url + "/readEPCCode").upRequestBody(RequestBody.create(JSON, gson.toJson(map))).execute(new MyRecultCall(dialog, this) {
@Override
public void onSuccess(Response<MyResult> response) {
super.onSuccess(response);
var body = response.body();
if (body.getResultFlag().equals("1")) {
list.addAll(gson.fromJson(body.getJson(), new TypeToken<List<ReadTyreNoResult>>() {
}.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<String, String> map = new HashMap<>();
map.put("EpcCode", info);
map.put("token", "123456");
map.put("Language", SharedPreferencesUtils.getstring("languageIndex", "0"));
OkGo.<MyResult>post(url + "/readEPCCode")
.upRequestBody(RequestBody.create(JSON, gson.toJson(map)))
.execute(new MyRecultCall(dialog, this) {
@Override
public void onSuccess(Response<MyResult> response) {
super.onSuccess(response);
var body = response.body();
if (body.getResultFlag().equals("1")) {
list.addAll(gson.fromJson(body.getJson(), new TypeToken<List<ReadTyreNoResult>>() {
}.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) {
}
}

@ -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<String, String> map = new HashMap<>();

@ -41,7 +41,7 @@
android:layout_height="match_parent"
android:text="@string/scanning_area" />
<EditText
<TextView
android:id="@+id/read_text"
style="@style/text_name"
android:layout_width="match_parent"

@ -36,4 +36,5 @@
<string name="scanning_area">scanning area</string>
<string name="dialog_button_text">I see</string>
<string name="bluetooth_open_state">Bluetooth not connected, program exits</string>
<string name="bluetooth_open_secess">Connection successful</string>
</resources>

@ -37,4 +37,5 @@
<string name="scanning_area">Khu vực quét</string>
<string name="dialog_button_text">Tôi biết rồi</string>
<string name="bluetooth_open_state">Bluetooth không kết nối, chương trình thoát</string>
<string name="bluetooth_open_secess">Kết nối thành công</string>
</resources>

@ -35,5 +35,6 @@
<string name="standalone_mode">单机模式</string>
<string name="scanning_area">扫描区域</string>
<string name="dialog_button_text">我知道了</string>
<string name="bluetooth_open_state">蓝牙未连接,程序退出</string>>
<string name="bluetooth_open_state">蓝牙未连接,程序退出</string>
<string name="bluetooth_open_secess">蓝牙连接成功</string>>
</resources>
Loading…
Cancel
Save