diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 932c14d..3750330 100755 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -2,24 +2,27 @@ - + - + android:required="true" /> - - + - - - @@ -346,6 +349,11 @@ android:name=".bleUtil.BlueToothDeviceActivity" android:configChanges="keyboardHidden|orientation|screenSize" android:screenOrientation="portrait" /> + () { binding.etUsername.setText("RH00002") binding.etPassword.setText("RH20251029.") +// binding.etUsername.setText("972526") +// binding.etPassword.setText("Wq20001221.") + //瑞洪 RH00002/王总 RH00002/chao工 RH00003/范红波 diff --git a/app/src/main/java/com/rehome/dywoa/ui/activity/sbxdjgl/BlueToothDeviceSdkActivity.java b/app/src/main/java/com/rehome/dywoa/ui/activity/sbxdjgl/BlueToothDeviceSdkActivity.java new file mode 100644 index 0000000..2f587c1 --- /dev/null +++ b/app/src/main/java/com/rehome/dywoa/ui/activity/sbxdjgl/BlueToothDeviceSdkActivity.java @@ -0,0 +1,566 @@ +package com.rehome.dywoa.ui.activity.sbxdjgl; + + +import android.Manifest; +import android.annotation.SuppressLint; +import android.bluetooth.BluetoothAdapter; +import android.bluetooth.BluetoothDevice; +import android.bluetooth.BluetoothManager; +import android.bluetooth.le.BluetoothLeScanner; +import android.bluetooth.le.ScanCallback; +import android.bluetooth.le.ScanResult; +import android.content.Context; +import android.content.Intent; +import android.content.pm.PackageManager; +import android.graphics.Color; +import android.os.Build; +import com.bjzc.blemanager.BluetoothLeService; +import android.os.Handler; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.BaseAdapter; +import android.widget.ListView; +import android.widget.TextView; +import android.widget.Toast; + +import androidx.activity.EdgeToEdge; +import androidx.annotation.NonNull; +import androidx.appcompat.app.AppCompatActivity; +import androidx.core.app.ActivityCompat; +import androidx.core.graphics.Insets; +import androidx.core.view.ViewCompat; +import androidx.core.view.WindowInsetsCompat; +import androidx.localbroadcastmanager.content.LocalBroadcastManager; + +//import com.clj.fastble.BleManager; +import com.bjzc.blemanager.BleManager; +import com.clj.fastble.callback.BleScanCallback; +import com.clj.fastble.data.BleDevice; +import com.clj.fastble.data.BleScanState; +import com.clj.fastble.scan.BleScanRuleConfig; +import com.google.gson.Gson; +import com.rehome.dywoa.Contans; +import com.rehome.dywoa.R; +import com.rehome.dywoa.adapter.XzjhAdapter; +import com.rehome.dywoa.base.BaseActivity; +import com.rehome.dywoa.utils.SPUtils; + +import java.util.ArrayList; +import java.util.List; + +/** + * 蓝牙扫描 + * https://developer.android.google.cn/develop/connectivity/bluetooth/ble/find-ble-devices?hl=zh-cn + */ +public class BlueToothDeviceSdkActivity extends BaseActivity { + + ListView lv; + TextView ble_status; + TextView ble_dianlang; + + private BluetoothAdapter mBluetoothAdapter; + private ArrayList mPermissionList = new ArrayList(); + private BluetoothLeScanner bluetoothLeScanner; + private boolean scanning=false; + private Handler handler = new Handler(); + + // Stops scanning after 10 seconds. + private static final long SCAN_PERIOD = 10000; + + //private LeDeviceListAdapter leDeviceListAdapter = new LeDeviceListAdapter(); + private LeDeviceListAdapter mLeDeviceListAdapter; + + private BleManager ble; + private int selectItem = -1; + private String cmd = ""; + private String str = ""; + + @Override + public int getContentViewID() { + return R.layout.activity_blue_tooth_device; + } + + @Override + public void initView() { + lv = findViewById(R.id.lv); + ble_status = findViewById(R.id.ble_status); + ble_dianlang = findViewById(R.id.ble_dianlang); + ble = BleManager.getInstance(); + str = ble.init(BlueToothDeviceSdkActivity.this); + showLog(str); + + ble.setOnReceiveListener(new BleManager.OnReceiveListener() { + @SuppressLint("SetTextI18n") + @Override + public void onShowMessage(Context context, Intent intent) { + final String action = intent.getAction(); + if (BluetoothDevice.ACTION_FOUND.equals(action)) { + BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); + if (ActivityCompat.checkSelfPermission(BlueToothDeviceSdkActivity.this, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) { + // TODO: Consider calling + // ActivityCompat#requestPermissions + // here to request the missing permissions, and then overriding + // public void onRequestPermissionsResult(int requestCode, String[] permissions, + // int[] grantResults) + // to handle the case where the user grants the permission. See the documentation + // for ActivityCompat#requestPermissions for more details. + return; + } + showLog("扫描发现设备" + device.getName() + "," + device.getAddress()); + String deviceName = device.getName(); + if (deviceName != null && !deviceName.isEmpty()&&deviceName.startsWith("W3")){ + //showLog(new Gson().toJson(device)); + mLeDeviceListAdapter.addDevice(device); + mLeDeviceListAdapter.notifyDataSetChanged(); + } + } else if (BluetoothLeService.ACTION_GATT_CONNECTED.equals(action)) { + showLog("连接"); + mLeDeviceListAdapter.setSelectItem(selectItem); + mLeDeviceListAdapter.notifyDataSetChanged(); + + BluetoothDevice device = mLeDeviceListAdapter.getDevice(selectItem); + String deviceName = device.getName(); + if (deviceName != null && !deviceName.isEmpty()){ + SPUtils.put(context, Contans.KEY_BLUE_TOOTH,device.getAddress()); + } + } else if (BluetoothLeService.ACTION_GATT_DISCONNECTED.equals(action)) { + showLog("断开"); + } else if (BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED.equals(action)) { + showLog("数据传输通道准备就绪,可以发送数据了。"); + ble.write("BV", false); + cmd="BV"; + ble_status.setText("已连接"); + ble_status.setTextColor(Color.parseColor("#00FF7F"));//连接之后的颜色 + } else if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)) { + byte[] data= intent.getByteArrayExtra(BluetoothLeService.EXTRA_DATA); + String resultReceive = new String(data); + showLog("收到传感器数据" + resultReceive); + showLog(cmd); +// if("BV".equals(cmd)){ +// //showLog(resultReceive); +// ble_dianlang.setText(resultReceive+"%"); +// } + } + } + }); + + initToolbar("蓝牙传感器管理", "扫描蓝牙", new View.OnClickListener() { + @Override + public void onClick(View v) { + showLog("蓝牙数据管理"); + bleScan(); + } + }); + //BleManager.getInstance().init(getApplication()); + + + + + mLeDeviceListAdapter = new LeDeviceListAdapter(new LeDeviceListAdapter.CallBackBlueToothBind() { + + @Override + public void Click(int position) { + showLog(String.valueOf(position)); + BluetoothDevice device = mLeDeviceListAdapter.getDevice(position); + if (ActivityCompat.checkSelfPermission(BlueToothDeviceSdkActivity.this, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) { + // TODO: Consider calling + // ActivityCompat#requestPermissions + // here to request the missing permissions, and then overriding + // public void onRequestPermissionsResult(int requestCode, String[] permissions, + // int[] grantResults) + // to handle the case where the user grants the permission. See the documentation + // for ActivityCompat#requestPermissions for more details. + return; + } + selectItem=position; + ble.connect(device.getAddress(),device.getName()); + } + }, new LeDeviceListAdapter.CallBackBlueToothSendData() { + @Override + public void Click(int position) { + //ble.write(String cmd, Boolean ishex) + //读固件版本号 + //ble.write("VR", false); + //读电量 + ble.write("BV", false); + cmd="BV"; + //温度测量 + //ble.write("0B0B6300", true); + //距离测量 + //ble.write("4453", true); + //转速测量 + //ble.write("0F3101", true); + //cmd="0F3101"; + //加速度测量 + //ble.write("1F11", true); + //速度测量。 + //ble.write("1F12", true); + //位移测量 + //ble.write("1F13", true); + + } + }); + lv.setAdapter(mLeDeviceListAdapter); + } + + private void bleScan(){ + mLeDeviceListAdapter.clear(); + mLeDeviceListAdapter.notifyDataSetChanged(); + selectItem = -1; + ble.disconnect(); + if("BLE is not supported".equals(str)){ + showToast("当前设备不支持低功耗蓝牙,无法连接传感器采集数据"); + } + if("Bluetooth not supported".equals(str)){ + showToast("当前设备不支持蓝牙功能"); + } + if("Bluetooth not enabled".equals(str)){ + showToast("蓝牙功能没有开启"); + } + if("ACCESS_COARSE_LOCATION permission denied".equals(str)){ + showToast("您未授权开启蓝牙扫描附近的设备,请先开启蓝牙权限"); + } + if("context is no Activity".equals(str)){ + showToast("Init(context)方法的参数必须是 Activity"); + } + if("OK".equals(str)){ + ble.scan(); + } + } + + @Override + public void initData() { + bleScan(); + } + + @Override + protected void onDestroy() { + super.onDestroy(); + ble.disconnect(); +// if(BleManager.getInstance().getScanSate()== BleScanState.STATE_SCANNING){ +// BleManager.getInstance().cancelScan(); +// } +// BleManager.getInstance().disconnectAllDevice(); +// BleManager.getInstance().destroy(); + } + + + private void initPermission(){ + if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.S){ + // Android 版本大于等于 Android12 时 + // 只包括蓝牙这部分的权限,其余的需要什么权限自己添加 + mPermissionList.add(Manifest.permission.BLUETOOTH_SCAN); + mPermissionList.add(Manifest.permission.BLUETOOTH_ADVERTISE); + mPermissionList.add(Manifest.permission.BLUETOOTH_CONNECT); + } else { + // Android 版本小于 Android12 及以下版本 + mPermissionList.add(Manifest.permission.ACCESS_COARSE_LOCATION); + mPermissionList.add(Manifest.permission.ACCESS_FINE_LOCATION); + } + showLog(new Gson().toJson(mPermissionList.toArray(new String[0]))); + ActivityCompat.requestPermissions(this,mPermissionList.toArray(new String[0]),1001); + } + + @Override + public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { + super.onRequestPermissionsResult(requestCode, permissions, grantResults); + Log.i("app","onRequestPermissionsResult"); + Log.i("app",String.valueOf(grantResults.length)); + Log.i("app",new Gson().toJson(grantResults)); + boolean hasGrant = true; + for (int grantResult : grantResults) { + if (grantResult != 0) { + //未授权 + hasGrant = false; + break; + } + } + if(hasGrant){ + //已授权 + showToast("已授权低功耗蓝牙"); + BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE); + mBluetoothAdapter = bluetoothManager.getAdapter(); + if (mBluetoothAdapter == null) { + Toast.makeText(this, R.string.error_bluetooth_not_supported, Toast.LENGTH_SHORT).show(); + }else{ + //可以开始扫描啦 + showLog("可以开始扫描啦"); + bluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner(); + scanLeDevice(); + } + //scanLeDevice(true); + }else{ + showToast("您未授权开启蓝牙连接到Blue设备,请先开启权限"); + } + } + + + public void initBlueTooth() { +// boolean bluetoothAvailable = getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH); +// if(bluetoothAvailable){ +// showLog("传统蓝牙可用"); +// }else{ +// showLog("传统蓝牙不可用"); +// } + boolean bluetoothLEAvailable = getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE); + + if(bluetoothLEAvailable){ + showLog("低功耗蓝牙可用"); + initPermission(); + }else{ + showLog("低功耗蓝牙不可用"); + showToast("传感器必须使用低功耗蓝牙,当前设备不支持低功耗蓝牙"); + } + } + +// public void initBlueToothFastBle() { +// if(BleManager.getInstance().getScanSate()== BleScanState.STATE_SCANNING){ +// BleManager.getInstance().cancelScan(); +// } +// BleManager.getInstance() +// .enableLog(true) +// .setReConnectCount(1, 5000) +// .setSplitWriteNum(20) +// .setConnectOverTime(10000) +// .setOperateTimeout(5000); +// +// BleScanRuleConfig scanRuleConfig = new BleScanRuleConfig.Builder() +// .setAutoConnect(true) +// .setScanTimeOut(10000) +// .build(); +// BleManager.getInstance().initScanRule(scanRuleConfig); +// +// BleManager.getInstance().scan(new BleScanCallback() { +// @Override +// public void onScanStarted(boolean success) { +// showToast("开始蓝牙扫描"); +// } +// +// @Override +// public void onScanning(BleDevice bleDevice) { + //// if (context != null && !BlueToothDeviceActivity.this.isFinishing() && !BlueToothDeviceActivity.this.isDestroyed()) { + //// showToast("发现设备"); + //// } +// BluetoothDevice device = bleDevice.getDevice(); +// if(device!=null){ +// if (ActivityCompat.checkSelfPermission(context, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) { +// // TODO: Consider calling +// // ActivityCompat#requestPermissions +// // here to request the missing permissions, and then overriding +// // public void onRequestPermissionsResult(int requestCode, String[] permissions, +// // int[] grantResults) +// // to handle the case where the user grants the permission. See the documentation +// // for ActivityCompat#requestPermissions for more details. +// return; +// } +// String deviceName = device.getName(); +// if (deviceName != null && !deviceName.isEmpty()){ +// showLog(new Gson().toJson(device)); +// mLeDeviceListAdapter.addDevice(bleDevice.getDevice()); +// mLeDeviceListAdapter.notifyDataSetChanged(); +// } +// } +// } +// +// @Override +// public void onScanFinished(List scanResultList) { +// if (context != null && !BlueToothDeviceActivity.this.isFinishing() && !BlueToothDeviceActivity.this.isDestroyed()) { +// showToast("蓝牙扫描结束"); +// } +// showLog(String.valueOf(mLeDeviceListAdapter.getCount())); +// showLog(new Gson().toJson(scanResultList)); +// } +// }); +// } + + private void scanLeDevice() { + if (!scanning) { + // Stops scanning after a predefined scan period. + handler.postDelayed(new Runnable() { + @Override + public void run() { + scanning = false; + if (ActivityCompat.checkSelfPermission(BlueToothDeviceSdkActivity.this, Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED) { + // TODO: Consider calling + // ActivityCompat#requestPermissions + // here to request the missing permissions, and then overriding + // public void onRequestPermissionsResult(int requestCode, String[] permissions, + // int[] grantResults) + // to handle the case where the user grants the permission. See the documentation + // for ActivityCompat#requestPermissions for more details. + showToast("蓝牙扫描未授权"); + return; + } + bluetoothLeScanner.stopScan(leScanCallback); + } + }, SCAN_PERIOD); + scanning = true; + if (ActivityCompat.checkSelfPermission(BlueToothDeviceSdkActivity.this, Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED) { + // TODO: Consider calling + // ActivityCompat#requestPermissions + // here to request the missing permissions, and then overriding + // public void onRequestPermissionsResult(int requestCode, String[] permissions, + // int[] grantResults) + // to handle the case where the user grants the permission. See the documentation + // for ActivityCompat#requestPermissions for more details. + showToast("您未授权开启蓝牙连接到Blue设备,请先开启权限"); + return; + } + bluetoothLeScanner.startScan(leScanCallback); + } else { + scanning = false; + bluetoothLeScanner.stopScan(leScanCallback); + } + } + + private ScanCallback leScanCallback = new ScanCallback() { + @Override + public void onScanResult(int callbackType, ScanResult result) { + super.onScanResult(callbackType, result); + showToast("发现新设备啦"); + mLeDeviceListAdapter.addDevice(result.getDevice()); + mLeDeviceListAdapter.notifyDataSetChanged(); + } + }; + + private class LeDeviceListAdapter extends BaseAdapter { + private class ViewHolder { + TextView deviceName; + TextView deviceStatus; + TextView deviceBind; + TextView deviceDeBind; + TextView sendData; + } + + private ArrayList mLeDevices; + private LayoutInflater mInflator; + private LeDeviceListAdapter.CallBackBlueToothBind callBackBlueToothBind; + private LeDeviceListAdapter.CallBackBlueToothUnBind callBackBlueToothUnBind; + private LeDeviceListAdapter.CallBackBlueToothSendData callBackBlueToothSendData; + + public LeDeviceListAdapter(LeDeviceListAdapter.CallBackBlueToothBind callBackBlueToothBind, + LeDeviceListAdapter.CallBackBlueToothSendData callBackBlueToothSendData) { + super(); + mLeDevices = new ArrayList(); + mInflator = BlueToothDeviceSdkActivity.this.getLayoutInflater(); + this.callBackBlueToothBind=callBackBlueToothBind; + this.callBackBlueToothSendData=callBackBlueToothSendData; + } + + public interface CallBackBlueToothBind { + void Click(int position); + } + + public interface CallBackBlueToothUnBind { + void Click(int position); + } + + public interface CallBackBlueToothSendData { + void Click(int position); + } + + public void addDevice(BluetoothDevice device) { + if (!mLeDevices.contains(device)) { + mLeDevices.add(device); + } + } + + public BluetoothDevice getDevice(int position) { + return mLeDevices.get(position); + } + + public void clear() { + mLeDevices.clear(); + } + + @Override + public int getCount() { + return mLeDevices.size(); + } + + @Override + public Object getItem(int i) { + return mLeDevices.get(i); + } + + @Override + public long getItemId(int i) { + return i; + } + + @Override + public View getView(int i, View view, ViewGroup viewGroup) { + LeDeviceListAdapter.ViewHolder viewHolder; + if (view == null) { + view = mInflator.inflate(R.layout.listitem_device, null); + viewHolder = new LeDeviceListAdapter.ViewHolder(); + viewHolder.deviceName = view.findViewById(R.id.device_name); + viewHolder.deviceStatus = view.findViewById(R.id.device_status); + viewHolder.deviceBind = view.findViewById(R.id.device_bind); + viewHolder.deviceDeBind = view.findViewById(R.id.device_de_bind); + viewHolder.sendData = view.findViewById(R.id.send_data); + view.setTag(viewHolder); + } else { + viewHolder = (LeDeviceListAdapter.ViewHolder) view.getTag(); + } + + BluetoothDevice device = mLeDevices.get(i); + if (ActivityCompat.checkSelfPermission(context, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) { + // TODO: Consider calling + // ActivityCompat#requestPermissions + // here to request the missing permissions, and then overriding + // public void onRequestPermissionsResult(int requestCode, String[] permissions, + // int[] grantResults) + // to handle the case where the user grants the permission. See the documentation + // for ActivityCompat#requestPermissions for more details. + return null; + } + String deviceName = device.getName(); + if (deviceName != null && !deviceName.isEmpty()) + viewHolder.deviceName.setText(deviceName); + else{ + viewHolder.deviceName.setText(R.string.unknown_device); + } + //当前点击选中的变色 + if (i == selectItem) { + viewHolder.deviceName.getPaint().setFakeBoldText(true); + //viewHolder.deviceName.setTextColor(Color.parseColor("#00FF7F"));//连接之后的颜色 + viewHolder.deviceStatus.setTextColor(Color.parseColor("#00FF7F"));//连接之后的颜色 + viewHolder.deviceStatus.setText("已连接"); + } else { + viewHolder.deviceName.getPaint().setFakeBoldText(false); + //viewHolder.deviceName.setTextColor(Color.parseColor("#00FF7F"));//连接之后的颜色 + viewHolder.deviceStatus.setTextColor(Color.parseColor("#000000")); + viewHolder.deviceStatus.setText("未绑定"); + } + viewHolder.deviceBind.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + if(callBackBlueToothBind!=null){ + callBackBlueToothBind.Click(i); + } + } + }); + viewHolder.sendData.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + if(callBackBlueToothSendData!=null){ + callBackBlueToothSendData.Click(i); + } + } + }); + + + + return view; + } + + public void setSelectItem(int selectItem) { + this.selectItem = selectItem; + } + + private int selectItem = -1; + } +} \ No newline at end of file diff --git a/app/src/main/java/com/rehome/dywoa/ui/activity/sbxdjgl/SbxdjglActivity.java b/app/src/main/java/com/rehome/dywoa/ui/activity/sbxdjgl/SbxdjglActivity.java index f889150..6f7869c 100755 --- a/app/src/main/java/com/rehome/dywoa/ui/activity/sbxdjgl/SbxdjglActivity.java +++ b/app/src/main/java/com/rehome/dywoa/ui/activity/sbxdjgl/SbxdjglActivity.java @@ -19,6 +19,7 @@ import com.rehome.dywoa.R; import com.rehome.dywoa.adapter.GridViewYXAdapter; import com.rehome.dywoa.base.BaseActivity2; import com.rehome.dywoa.bean.GridViewBean; +import com.rehome.dywoa.bleUtil.BlueToothDeviceActivity; import com.rehome.dywoa.ui.activity.sbxj.NFCInfoActivity; import com.rehome.dywoa.utils.GsonUtils; import com.rehome.dywoa.utils.HttpListener; @@ -43,9 +44,9 @@ import java.util.Map; public class SbxdjglActivity extends BaseActivity2 { GridView gv; - private final String[] str = {"任务", "工作", "统计", "读取NFC标签", "缺陷工单录入", "免检申请"}; + private final String[] str = {"任务", "工作", "统计", "读取NFC标签", "缺陷工单录入", "免检申请", "蓝牙传感器管理"}; private final int[] imageId = {R.drawable.icon3, R.drawable.icon6, R.drawable.icon_task_count, R.drawable.icon11, - R.drawable.icon15, R.drawable.icon18}; + R.drawable.icon15, R.drawable.icon18, R.drawable.icon19}; private GridViewYXAdapter adapter; @@ -121,6 +122,11 @@ public class SbxdjglActivity extends BaseActivity2 { intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); startActivity(intent); break; + case 6: + intent = new Intent(SbxdjglActivity.this, BlueToothDeviceActivity.class); + intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); + startActivity(intent); + break; } } }); diff --git a/app/src/main/res/drawable-xhdpi/icon19.png b/app/src/main/res/drawable-xhdpi/icon19.png new file mode 100644 index 0000000..b938603 Binary files /dev/null and b/app/src/main/res/drawable-xhdpi/icon19.png differ diff --git a/app/src/main/res/layout/activity_blue_tooth_device_sdk.xml b/app/src/main/res/layout/activity_blue_tooth_device_sdk.xml new file mode 100644 index 0000000..e48ebbc --- /dev/null +++ b/app/src/main/res/layout/activity_blue_tooth_device_sdk.xml @@ -0,0 +1,73 @@ + + + + + + + + + + + + + + + + + + + + + +