diff --git a/app/build.gradle b/app/build.gradle index da4fcd4..a49a3f4 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -39,6 +39,7 @@ dependencies { implementation 'androidx.constraintlayout:constraintlayout:2.1.4' implementation files('libs\\SerialPort.jar') implementation files('libs\\uhfcom13_v15.jar') + implementation files('libs\\RflyBlue-1.0.0.aar') testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.5' androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' diff --git a/app/libs/RflyBlue-1.0.0.aar b/app/libs/RflyBlue-1.0.0.aar new file mode 100644 index 0000000..1112268 Binary files /dev/null and b/app/libs/RflyBlue-1.0.0.aar differ diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index f8965cf..9dbe96b 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,6 +1,14 @@ + + + + + + + + + + diff --git a/app/src/main/java/com/example/bgsrfidtrack/BindingActivity.java b/app/src/main/java/com/example/bgsrfidtrack/BindingActivity.java index 9e1667b..f0173a5 100644 --- a/app/src/main/java/com/example/bgsrfidtrack/BindingActivity.java +++ b/app/src/main/java/com/example/bgsrfidtrack/BindingActivity.java @@ -82,6 +82,8 @@ public class BindingActivity extends BaseActivity implements MyKeyInfoCall, Adap // 提交 public void bindingSubmit(View view) { model.setWatBills(codeList); + + model.setUser(SharedPreferencesUtils.getstring("user","")); OkGo.post("http://"+ SharedPreferencesUtils.getstring("ip","")+ "/api/bindingSubmit") .upRequestBody(RequestBody.create(JSON, gson.toJson(model))) diff --git a/app/src/main/java/com/example/bgsrfidtrack/BindingDBActivity.java b/app/src/main/java/com/example/bgsrfidtrack/BindingDBActivity.java new file mode 100644 index 0000000..eacb5aa --- /dev/null +++ b/app/src/main/java/com/example/bgsrfidtrack/BindingDBActivity.java @@ -0,0 +1,76 @@ +package com.example.bgsrfidtrack; + +import android.os.Bundle; +import android.view.View; +import android.widget.Toast; + +import androidx.databinding.DataBindingUtil; + +import com.example.bgsrfidtrack.base.BaseActivity; +import com.example.bgsrfidtrack.base.MyRecultCall; +import com.example.bgsrfidtrack.base.MyResult; +import com.example.bgsrfidtrack.base.SharedPreferencesUtils; +import com.example.bgsrfidtrack.databinding.ActivityBindingDbBinding; +import com.example.bgsrfidtrack.receiver.MyKeyInfoCall; +import com.lzy.okgo.OkGo; +import com.lzy.okgo.model.Response; + +public class BindingDBActivity extends BaseActivity implements MyKeyInfoCall { + private ActivityBindingDbBinding binding; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + binding = DataBindingUtil.setContentView(this, R.layout.activity_binding_db); + initRFID(); + initKeyRecriver(this); + } + + public void binddingDbClear1(View view) { + binding.hkEpc.setText(null); + } + + public void binddingDbClear2(View view) { + binding.dbEpc.setText(null); + } + + public void bindingDbSubmit(View view) { + var hkEpcCode = binding.hkEpc.getText().toString(); + var dbEpcCode = binding.dbEpc.getText().toString(); + if (hkEpcCode.isEmpty() || dbEpcCode.isEmpty()) { + return; + } + OkGo.post("http://" + + SharedPreferencesUtils.getstring("ip", "") + + "/api/blueToothSubmit") + .params("locationCode", dbEpcCode) + .params("hkCode", hkEpcCode) + .execute(new MyRecultCall(dialog, this) { + @Override + public void onSuccess(Response response) { + super.onSuccess(response); + if (response.body().getCode() == 0) { + binding.hkEpc.setText(null); + binding.dbEpc.setText(null); + } + Toast.makeText(context, response.body().getMsg(), Toast.LENGTH_SHORT).show(); + } + }); + + } + + @Override + public void onkeyDown() { + readEPC(); + } + + @Override + public void sanEpcCall(String epc) { + super.sanEpcCall(epc); + if (binding.hkEpc.getText().toString().isEmpty()) { + binding.hkEpc.setText(epc); + } else { + binding.dbEpc.setText(epc); + } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/bgsrfidtrack/HomePageActivity.java b/app/src/main/java/com/example/bgsrfidtrack/HomePageActivity.java index e1c25a3..33f2955 100644 --- a/app/src/main/java/com/example/bgsrfidtrack/HomePageActivity.java +++ b/app/src/main/java/com/example/bgsrfidtrack/HomePageActivity.java @@ -19,13 +19,20 @@ public class HomePageActivity extends BaseActivity { ActivityHomePageBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_home_page); Intent intent = new Intent(this, BindingActivity.class); Intent intent2 = new Intent(this, UnBindingActivity.class); - binding.binding.setOnClickListener(v -> { + Intent intent3 = new Intent(this, BindingDBActivity.class); + Intent intent4 = new Intent(this, UnBindingActivity.class); + binding.homeIn.setOnClickListener(v -> { startActivity(intent); }); binding.unBinding.setOnClickListener(v -> { startActivity(intent2); }); - + binding.binding.setOnClickListener(v -> { + startActivity(intent3); + }); + binding.homeJiaojie.setOnClickListener(v -> { + startActivity(intent4); + }); } diff --git a/app/src/main/java/com/example/bgsrfidtrack/MainActivity.java b/app/src/main/java/com/example/bgsrfidtrack/MainActivity.java index 07e571c..7b7a115 100644 --- a/app/src/main/java/com/example/bgsrfidtrack/MainActivity.java +++ b/app/src/main/java/com/example/bgsrfidtrack/MainActivity.java @@ -2,16 +2,21 @@ package com.example.bgsrfidtrack; import androidx.appcompat.app.AppCompatActivity; import androidx.databinding.DataBindingUtil; +import androidx.databinding.ObservableBoolean; +import android.content.Context; import android.content.Intent; import android.os.Bundle; +import android.util.DisplayMetrics; import android.util.Log; import android.view.View; +import android.view.WindowManager; import com.example.bgsrfidtrack.base.BaseActivity; import com.example.bgsrfidtrack.base.MyRecultCall; import com.example.bgsrfidtrack.base.MyResult; import com.example.bgsrfidtrack.base.SharedPreferencesUtils; +import com.example.bgsrfidtrack.databinding.ActivityMain1Binding; import com.example.bgsrfidtrack.databinding.ActivityMainBinding; import com.example.bgsrfidtrack.model.LoginModel; import com.lzy.okgo.OkGo; @@ -21,12 +26,18 @@ import okhttp3.RequestBody; public class MainActivity extends BaseActivity { private LoginModel login; + private ObservableBoolean configState; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main); + // ActivityMain1Binding binding = DataBindingUtil.setContentView(this, R.layout.activity_main1); login = new LoginModel(); + configState=new ObservableBoolean(); + configState.set(true); + binding.setConfigState(configState); boolean isRemember = SharedPreferencesUtils.getboolean("isRemember", false); if (isRemember) { login.setUsername(SharedPreferencesUtils.getstring("user", "")); @@ -58,11 +69,17 @@ public class MainActivity extends BaseActivity { SharedPreferencesUtils.putstring("pass", pass); SharedPreferencesUtils.putstring("ip", ip); } + Intent intent = new Intent(MainActivity.this, HomePageActivity.class); + // Intent intent = new Intent(MainActivity.this, RflyActivity.class); startActivity(intent); finish(); } } }); } + // 改变配置ip栏显示 + public void loginConfigChange(View view){ + configState.set(!configState.get()); + } } \ No newline at end of file diff --git a/app/src/main/java/com/example/bgsrfidtrack/RflyActivity.java b/app/src/main/java/com/example/bgsrfidtrack/RflyActivity.java new file mode 100644 index 0000000..2e119ec --- /dev/null +++ b/app/src/main/java/com/example/bgsrfidtrack/RflyActivity.java @@ -0,0 +1,231 @@ +package com.example.bgsrfidtrack; + +import androidx.annotation.NonNull; +import androidx.appcompat.app.AppCompatActivity; +import androidx.core.app.ActivityCompat; +import androidx.core.content.ContextCompat; +import androidx.databinding.DataBindingUtil; +import androidx.databinding.ObservableBoolean; + +import android.Manifest; +import android.annotation.SuppressLint; +import android.bluetooth.BluetoothAdapter; +import android.bluetooth.BluetoothDevice; +import android.content.pm.PackageManager; +import android.os.Bundle; +import android.os.Handler; +import android.os.Message; +import android.util.Log; +import android.view.View; +import android.widget.Toast; + +import com.Rfly.bluereader.ReaderDevice.Rfly; +import com.Rfly.bluereader.exception.ReaderException; +import com.Rfly.bluereader.type.EpcBean; +import com.example.bgsrfidtrack.adapter.AdapterClickCall; +import com.example.bgsrfidtrack.adapter.GoodsInfoAdapter; +import com.example.bgsrfidtrack.base.BaseActivity; +import com.example.bgsrfidtrack.base.MyRecultCall; +import com.example.bgsrfidtrack.base.MyResult; +import com.example.bgsrfidtrack.base.SharedPreferencesUtils; +import com.example.bgsrfidtrack.databinding.ActivityRflyBinding; +import com.example.bgsrfidtrack.uitls.ASCIIUtil; +import com.lzy.okgo.OkGo; +import com.lzy.okgo.model.Response; + +import java.util.ArrayList; +import java.util.List; +import java.util.Set; + +public class RflyActivity extends BaseActivity implements Rfly.ConnectCallback, Rfly.ReadTagDataCallback { + private Rfly hbtReader; + private ObservableBoolean state; + private ActivityRflyBinding binding; + private List tagList; + private GoodsInfoAdapter adapter1; + private List list1; + private List listVis; + private GoodsInfoAdapter adapter2; + private List list2; + + @SuppressLint("MissingPermission") + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + binding = DataBindingUtil.setContentView(this, R.layout.activity_rfly); + checkPermissions(); + state = new ObservableBoolean(); + state.set(true); + binding.setState(state); + // 初始化 + hbtReader = new Rfly(this); + tagList = new ArrayList<>(); + // 注册蓝牙扫描 + hbtReader.setDeviceCallback(new Rfly.DeviceCallback() { + @Override + public void DeviceCallback(BluetoothDevice bluetoothDevice, String s) { + if (bluetoothDevice != null) { + if (bluetoothDevice.getBondState() == BluetoothDevice.BOND_BONDED && bluetoothDevice.getName() != null && + bluetoothDevice.getName().equals("RFly-IV166-837248")) { + hbtReader.connect(bluetoothDevice, RflyActivity.this); + + }else { + Toast.makeText(context, "扫描不到设备", Toast.LENGTH_SHORT).show(); + } + }else { + Toast.makeText(context, "蓝牙扫描失败", Toast.LENGTH_SHORT).show(); + } + } + }); + hbtReader.setreadTagDataCallback(this); + try { + // 开始扫描 + dialog.show(); + hbtReader.searchDevice(); + } catch (ReaderException e) { + throw new RuntimeException(e); + } + adapter1 = new GoodsInfoAdapter(this, new AdapterClickCall() { + @Override + public void deleteItem(int index) { + list1.remove(index); + listVis.remove(index); + adapter1.notifyDataSetChanged(); + } + }); + list1 = new ArrayList<>(); + listVis = new ArrayList<>(); + adapter1.setList(listVis); + binding.setAdapter1(adapter1); + adapter2 = new GoodsInfoAdapter(this, new AdapterClickCall() { + @Override + public void deleteItem(int index) { + list2.remove(index); + adapter2.notifyDataSetChanged(); + } + }); + list2 = new ArrayList<>(); + adapter2.setList(list2); + binding.setAdapter2(adapter2); + } + + // 返回RFID + @Override + public void ReadTagDataCallback(EpcBean epcBean) { + var strepc = epcBean.strepc; + if (tagList.contains(strepc)) { + return; + } + tagList.add(strepc); + Log.e("TAG", "ReadTagDataCallback1:" + strepc); + strepc = ASCIIUtil.hex2Str(strepc); + Log.e("TAG", "ReadTagDataCallback2:" + strepc); + Message message = new Message(); + message.obj = strepc; + handler.sendMessage(message); + + } + + private Handler handler = new Handler() { + @Override + public void handleMessage(@NonNull Message msg) { + super.handleMessage(msg); + var str = msg.obj.toString(); + if (str.contains("DB")) { + // 地标 + list2.add(str); + adapter2.notifyDataSetChanged(); + } else if (str.contains("HK")) { + list1.add(str); + var length = str.length(); + listVis.add(str.substring(length -4,length)); + adapter1.notifyDataSetChanged(); + } + } + }; + + // 读取按钮 + public void blueToothSanRFID(View view) { + try { + if (state.get()) { + hbtReader.ScanTags(); + state.set(false); + } else { + hbtReader.StopScan(); + state.set(true); + } + } catch (ReaderException e) { + e.printStackTrace(); + } + } + + @Override + protected void onDestroy() { + super.onDestroy(); + try { + hbtReader.disconnect(); + } catch (ReaderException e) { + throw new RuntimeException(e); + } + hbtReader.destroy(); + } + + @Override + public void ConnectCallback(boolean b) { + Log.e("TAG", "连接状态:" + b); + dialog.dismiss(); + if (b) { + Toast.makeText(context, "连接成功", Toast.LENGTH_SHORT).show(); + return; + } + Toast.makeText(context, "蓝牙没有配对", Toast.LENGTH_SHORT).show(); + } + + + /** + * 权限检测,并请求权限 + */ + private void checkPermissions() { + String[] permissions = {android.Manifest.permission.BLUETOOTH_SCAN, android.Manifest.permission.BLUETOOTH_ADVERTISE, android.Manifest.permission.BLUETOOTH_CONNECT, android.Manifest.permission.ACCESS_FINE_LOCATION, android.Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.BLUETOOTH}; + + // List permissionsToRequest = new ArrayList<>(); + + // for (String permission : permissions) { + // if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) { + // permissionsToRequest.add(permission); + // } + // } + + // if (!permissionsToRequest.isEmpty()) { + ActivityCompat.requestPermissions(this, permissions, 1); + // } + } + + // 提交 + public void blueToothSubmit(View view) { + if (!state.get()){ + Toast.makeText(context, "先停止读取RFID", Toast.LENGTH_SHORT).show(); + return; + } + if (list1.isEmpty()||list2.isEmpty()) return; + OkGo.post("http://"+ SharedPreferencesUtils.getstring("ip","") +"/api/blueToothSubmit") + .params("locationCode", list2.get(0)) + .params("hkCode", list1.get(0)) + .execute(new MyRecultCall(dialog, this) { + @Override + public void onSuccess(Response response) { + super.onSuccess(response); + if (response.body().getCode()==0){ + tagList.clear(); + list1.clear(); + listVis.clear(); + list2.clear(); + adapter1.notifyDataSetChanged(); + adapter2.notifyDataSetChanged(); + } + Toast.makeText(RflyActivity.this, response.body().getMsg(), Toast.LENGTH_SHORT).show(); + } + }); + + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/bgsrfidtrack/UnBindingActivity.java b/app/src/main/java/com/example/bgsrfidtrack/UnBindingActivity.java index 12a4ec6..a3a25b1 100644 --- a/app/src/main/java/com/example/bgsrfidtrack/UnBindingActivity.java +++ b/app/src/main/java/com/example/bgsrfidtrack/UnBindingActivity.java @@ -6,29 +6,60 @@ import androidx.databinding.DataBindingUtil; import android.os.Bundle; import android.util.Log; import android.view.View; +import android.widget.Toast; +import com.example.bgsrfidtrack.adapter.AdapterClickCall; import com.example.bgsrfidtrack.adapter.UnBindingGoodsInfoAdapter; import com.example.bgsrfidtrack.base.BaseActivity; +import com.example.bgsrfidtrack.base.MyRecultCall; +import com.example.bgsrfidtrack.base.MyResult; +import com.example.bgsrfidtrack.base.SharedPreferencesUtils; import com.example.bgsrfidtrack.databinding.ActivityUnBindingBinding; import com.example.bgsrfidtrack.receiver.MyKeyInfoCall; import com.example.bgsrfidtrack.uitls.ASCIIUtil; +import com.google.gson.reflect.TypeToken; +import com.lzy.okgo.OkGo; +import com.lzy.okgo.model.Response; + +import java.util.List; import cn.pda.serialport.Tools; -public class UnBindingActivity extends BaseActivity implements MyKeyInfoCall { +public class UnBindingActivity extends BaseActivity implements MyKeyInfoCall, AdapterClickCall { private UnBindingGoodsInfoAdapter adapter; private ActivityUnBindingBinding binding; + private List list; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); binding = DataBindingUtil.setContentView(this, R.layout.activity_un_binding); initRFID(); initKeyRecriver(this); + adapter = new UnBindingGoodsInfoAdapter(this, this); + binding.setAdapter(adapter); } + // 删除全部 public void unBindingSubmit(View view) { + OkGo.post("http://" + SharedPreferencesUtils.getstring("ip", "") + + "/api/deleteBindingList") + .params("epc", binding.unbindingEpc.getText().toString()) + .execute(new MyRecultCall(dialog, this) { + @Override + public void onSuccess(Response response) { + super.onSuccess(response); + var body = response.body(); + if (body.getCode() == 0) { + list.clear(); + adapter.notifyDataSetChanged(); + } + Toast.makeText(UnBindingActivity.this, body.getMsg(), Toast.LENGTH_SHORT).show(); + } + }); } + // 扫描RFID @Override public void onkeyDown() { @@ -39,5 +70,52 @@ public class UnBindingActivity extends BaseActivity implements MyKeyInfoCall { public void sanEpcCall(String epc) { super.sanEpcCall(epc); binding.unbindingEpc.setText(epc); + OkGo.post("http://" + SharedPreferencesUtils.getstring("ip", "") + + "/api/findBindingList") + .params("epc", epc) + .execute(new MyRecultCall(dialog, this) { + @Override + public void onSuccess(Response response) { + super.onSuccess(response); + var body = response.body(); + if (body.getCode() == 0) { + + list = gson.fromJson(body.getData().toString(), new TypeToken>() { + }.getType()); + adapter.setList(list); + adapter.notifyDataSetChanged(); + }else { + if (list!=null){ + list.clear(); + adapter.notifyDataSetChanged(); + } + } + Toast.makeText(UnBindingActivity.this, body.getMsg(), Toast.LENGTH_SHORT).show(); + } + }); } + + @Override + public void deleteItem(int index) { + var code = list.get(index); + // 删除一条 + OkGo.post("http://" + SharedPreferencesUtils.getstring("ip", "") + + "/api/deleteBindingItem") + .params("epc", binding.unbindingEpc.getText().toString()) + .params("code", code) + .execute(new MyRecultCall(dialog, this) { + @Override + public void onSuccess(Response response) { + super.onSuccess(response); + var body = response.body(); + if (body.getCode() == 0) { + list.remove(index); + adapter.notifyDataSetChanged(); + } + Toast.makeText(UnBindingActivity.this, body.getMsg(), Toast.LENGTH_SHORT).show(); + } + }); + } + + } \ No newline at end of file diff --git a/app/src/main/java/com/example/bgsrfidtrack/adapter/UnBindingGoodsInfoAdapter.java b/app/src/main/java/com/example/bgsrfidtrack/adapter/UnBindingGoodsInfoAdapter.java index d732f2b..e64107e 100644 --- a/app/src/main/java/com/example/bgsrfidtrack/adapter/UnBindingGoodsInfoAdapter.java +++ b/app/src/main/java/com/example/bgsrfidtrack/adapter/UnBindingGoodsInfoAdapter.java @@ -52,7 +52,7 @@ public class UnBindingGoodsInfoAdapter extends RecyclerView.Adapter watBills; + private String user; + + + public String getUser() { + return user; + } + + public void setUser(String user) { + this.user = user; + } public List getWatBills() { return watBills; diff --git a/app/src/main/res/layout/activity_binding_db.xml b/app/src/main/res/layout/activity_binding_db.xml new file mode 100644 index 0000000..3e8e97c --- /dev/null +++ b/app/src/main/res/layout/activity_binding_db.xml @@ -0,0 +1,75 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + +