|
|
@ -60,6 +60,7 @@ public class ReadBlueToothActivity extends BaseActivity {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
binding = DataBindingUtil.setContentView(this, R.layout.activity_bluetooth_read);
|
|
|
|
binding = DataBindingUtil.setContentView(this, R.layout.activity_bluetooth_read);
|
|
|
|
binding.setTitle(getString(R.string.go_read));
|
|
|
|
binding.setTitle(getString(R.string.go_read));
|
|
|
|
|
|
|
|
dialog.show();
|
|
|
|
adapter = new ResultAdapter(this);
|
|
|
|
adapter = new ResultAdapter(this);
|
|
|
|
list = new ArrayList<>(11);
|
|
|
|
list = new ArrayList<>(11);
|
|
|
|
adapter.setList(list);
|
|
|
|
adapter.setList(list);
|
|
|
@ -90,37 +91,54 @@ public class ReadBlueToothActivity extends BaseActivity {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
bluetoothSocket = device.createRfcommSocketToServiceRecord(device.getUuids()[0].getUuid());
|
|
|
|
bluetoothSocket = device.createRfcommSocketToServiceRecord(device.getUuids()[0].getUuid());
|
|
|
|
bluetoothSocket.connect();
|
|
|
|
runOnUiThread(new Runnable() {
|
|
|
|
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() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
public void run() {
|
|
|
|
byte[] buffer = new byte[1024];
|
|
|
|
new Handler().postDelayed(new Runnable() {
|
|
|
|
int bytesRead;
|
|
|
|
@Override
|
|
|
|
try {
|
|
|
|
public void run() {
|
|
|
|
while ((bytesRead = inputStream.read(buffer)) != -1) {
|
|
|
|
if (ActivityCompat.checkSelfPermission(context, android.Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
|
|
|
|
String receivedData = new String(buffer, 0, bytesRead);
|
|
|
|
// return;
|
|
|
|
Message message = handler.obtainMessage();
|
|
|
|
}
|
|
|
|
message.obj = receivedData;
|
|
|
|
try {
|
|
|
|
handler.sendMessage(message);
|
|
|
|
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) {
|
|
|
|
}, 500);
|
|
|
|
Log.e("TAG", "Error reading from input stream", e);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
myThread.start();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
e.printStackTrace();
|
|
|
@ -129,19 +147,48 @@ public class ReadBlueToothActivity extends BaseActivity {
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private String user = null;
|
|
|
|
|
|
|
|
@SuppressLint("HandlerLeak")
|
|
|
|
private Handler handler = new Handler() {
|
|
|
|
private Handler handler = new Handler() {
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void handleMessage(@NonNull Message msg) {
|
|
|
|
public void handleMessage(@NonNull Message msg) {
|
|
|
|
super.handleMessage(msg);
|
|
|
|
super.handleMessage(msg);
|
|
|
|
String receivedData = (String) msg.obj;
|
|
|
|
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) {
|
|
|
|
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 {
|
|
|
|
} else {
|
|
|
|
|
|
|
|
Log.e("TAG", "读取失败:" + receivedData);
|
|
|
|
|
|
|
|
user = null;
|
|
|
|
binding.readText.setText(null);
|
|
|
|
binding.readText.setText(null);
|
|
|
|
|
|
|
|
if (!list.isEmpty()) {
|
|
|
|
|
|
|
|
list.clear();
|
|
|
|
|
|
|
|
adapter.notifyDataSetChanged();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -149,51 +196,56 @@ public class ReadBlueToothActivity extends BaseActivity {
|
|
|
|
|
|
|
|
|
|
|
|
// 读取user区
|
|
|
|
// 读取user区
|
|
|
|
public void readInfo(View view) {
|
|
|
|
public void readInfo(View view) {
|
|
|
|
String data = binding.readText.getText().toString() + "\r";
|
|
|
|
user = null;
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
Log.e("TAG", "发送:" + data);
|
|
|
|
outputStream.write("GR300218\r".getBytes());
|
|
|
|
outputStream.write(data.getBytes());
|
|
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
} 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) {
|
|
|
|
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")
|
|
|
|
@SuppressLint("NotifyDataSetChanged")
|
|
|
|
public void readEpcCodeInfo(String info, boolean state, String stateInfo) {
|
|
|
|
public void readEpcCodeInfo(String info) {
|
|
|
|
if (state) {
|
|
|
|
music.start();
|
|
|
|
music.start();
|
|
|
|
list.clear();
|
|
|
|
list.clear();
|
|
|
|
binding.readText.setText(info);
|
|
|
|
binding.readText.setText(info);
|
|
|
|
Map<String, String> map = new HashMap<>();
|
|
|
|
Map<String, String> map = new HashMap<>();
|
|
|
|
map.put("EpcCode", info);
|
|
|
|
map.put("EpcCode", info);
|
|
|
|
map.put("token", "123456");
|
|
|
|
map.put("token", "123456");
|
|
|
|
map.put("Language", SharedPreferencesUtils.getstring("languageIndex", "0"));
|
|
|
|
map.put("Language", SharedPreferencesUtils.getstring("languageIndex", "0"));
|
|
|
|
OkGo.<MyResult>post(url + "/readEPCCode")
|
|
|
|
OkGo.<MyResult>post(url + "/readEPCCode").upRequestBody(RequestBody.create(JSON, gson.toJson(map))).execute(new MyRecultCall(dialog, this) {
|
|
|
|
.upRequestBody(RequestBody.create(JSON, gson.toJson(map)))
|
|
|
|
@Override
|
|
|
|
.execute(new MyRecultCall(dialog, this) {
|
|
|
|
public void onSuccess(Response<MyResult> response) {
|
|
|
|
@Override
|
|
|
|
super.onSuccess(response);
|
|
|
|
public void onSuccess(Response<MyResult> response) {
|
|
|
|
var body = response.body();
|
|
|
|
super.onSuccess(response);
|
|
|
|
if (body.getResultFlag().equals("1")) {
|
|
|
|
var body = response.body();
|
|
|
|
list.addAll(gson.fromJson(body.getJson(), new TypeToken<List<ReadTyreNoResult>>() {
|
|
|
|
if (body.getResultFlag().equals("1")) {
|
|
|
|
}.getType()));
|
|
|
|
list.addAll(gson.fromJson(body.getJson(), new TypeToken<List<ReadTyreNoResult>>() {
|
|
|
|
adapter.notifyDataSetChanged();
|
|
|
|
}.getType()));
|
|
|
|
} else {
|
|
|
|
adapter.notifyDataSetChanged();
|
|
|
|
Toast.makeText(ReadBlueToothActivity.this, body.getJson(), Toast.LENGTH_SHORT).show();
|
|
|
|
} else {
|
|
|
|
adapter.notifyDataSetChanged();
|
|
|
|
Toast.makeText(ReadBlueToothActivity.this, body.getJson(), Toast.LENGTH_SHORT).show();
|
|
|
|
}
|
|
|
|
adapter.notifyDataSetChanged();
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
}
|
|
|
|
binding.readText.setText(null);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@SuppressLint("NotifyDataSetChanged")
|
|
|
|
@SuppressLint("NotifyDataSetChanged")
|
|
|
@ -207,7 +259,7 @@ public class ReadBlueToothActivity extends BaseActivity {
|
|
|
|
for (int i = 0; i < 12; i++) {
|
|
|
|
for (int i = 0; i < 12; i++) {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
list.add(new ReadTyreNoResult(i, stringArray[i], infos[i]));
|
|
|
|
list.add(new ReadTyreNoResult(i, stringArray[i], infos[i]));
|
|
|
|
}catch (Exception er){
|
|
|
|
} catch (Exception er) {
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|