Compare commits

...

14 Commits

@ -10,8 +10,8 @@ android {
applicationId "com.rehome.dywoa"
minSdk 24
targetSdk 36
versionCode 27
versionName "1.2.6"
versionCode 29
versionName "1.2.8"
multiDexEnabled true
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
//authorities
@ -91,6 +91,7 @@ dependencies {
implementation libs.circleimageview
implementation libs.photoviewer
implementation libs.appupdate
//APP
implementation libs.crashreport
implementation libs.xui
implementation libs.eventbus
@ -118,9 +119,6 @@ dependencies {
implementation files('libs/SangforSDK.aar')
//W3SDK
implementation files('libs/blemanager.aar')
implementation libs.circleimageview
//APP
implementation libs.crashreport
//

Binary file not shown.

@ -2,23 +2,27 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-feature android:name="android.hardware.bluetooth" android:required="false"/>
<uses-feature
android:name="android.hardware.bluetooth"
android:required="false" />
<uses-feature
android:name="android.hardware.bluetooth_le"
android:required="true" /> <!-- Android 12 中的新蓝牙权限开始 -->
<!-- Request legacy Bluetooth permissions on older devices. -->
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission
android:name="android.permission.BLUETOOTH_ADMIN"
android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.BLUETOOTH_SCAN"
<uses-permission
android:name="android.permission.BLUETOOTH_SCAN"
android:usesPermissionFlags="neverForLocation"
tools:targetApi="31" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"
<uses-permission
android:name="android.permission.ACCESS_FINE_LOCATION"
android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />
<uses-permission android:name="android.permission.NFC" />
@ -345,6 +349,11 @@
android:name=".bleUtil.BlueToothDeviceActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:screenOrientation="portrait" />
<activity
android:name=".bleUtil.BlueToothDeviceSdkActivity"
android:exported="false"
android:configChanges="keyboardHidden|orientation|screenSize"
android:screenOrientation="portrait"/>
<service
android:name="com.sangfor.atrust.sdp_tunnel.SdpVpnService"

@ -273,6 +273,8 @@ public class Contans {
public final static String NFCBM = "NFCBM";
public final static String KEY_BLUE_TOOTH = "KEY_BLUE_TOOTH";
public final static String KEY_BLUE_TOOTH_ADDRESS = "KEY_BLUE_TOOTH_ADDRESS";
public final static String KEY_BLUE_TOOTH_NAME = "KEY_BLUE_TOOTH_NAME";
public final static String USERNAME = "USERNAME";
public final static String SCID = "scid";

@ -76,8 +76,8 @@ public class BlueTestInfoActivity extends BaseActivity {
}
});
lv = findViewById(R.id.lv);
txData = findViewById(R.id.tx_data);
ztData = findViewById(R.id.zt_data);
// txData = findViewById(R.id.tx_data);
// ztData = findViewById(R.id.zt_data);
}
@Override

@ -15,6 +15,7 @@ import android.graphics.Color;
import android.os.Build;
import com.bjzc.blemanager.BluetoothLeService;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
@ -44,9 +45,11 @@ 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.GsonUtils;
import com.rehome.dywoa.utils.SPUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
@ -56,7 +59,9 @@ import java.util.List;
public class BlueToothDeviceActivity extends BaseActivity {
ListView lv;
TextView tx_data;
TextView ble_status;
TextView ble_dianlang;
TextView tvNodata;
private BluetoothAdapter mBluetoothAdapter;
private ArrayList<String> mPermissionList = new ArrayList();
private BluetoothLeScanner bluetoothLeScanner;
@ -72,6 +77,10 @@ public class BlueToothDeviceActivity extends BaseActivity {
private BleManager ble;
private int selectItem = -1;
private String cmd = "";
private String str="";
private String deviceNameSave;
private String deviceAddressSave;
@Override
public int getContentViewID() {
@ -81,11 +90,23 @@ public class BlueToothDeviceActivity extends BaseActivity {
@Override
public void initView() {
lv = findViewById(R.id.lv);
tx_data = findViewById(R.id.tx_data);
ble_status = findViewById(R.id.ble_status);
ble_dianlang = findViewById(R.id.ble_dianlang);
tvNodata = findViewById(R.id.tv_nodata);
ble= BleManager.getInstance();
String str=ble.init(BlueToothDeviceActivity.this);
str=ble.init(BlueToothDeviceActivity.this);
showLog(str);
deviceNameSave = (String) SPUtils.get(context, Contans.KEY_BLUE_TOOTH_NAME,"");
showLog("-------deviceNameSave-------");
showLog(deviceNameSave);
deviceAddressSave = (String) SPUtils.get(context, Contans.KEY_BLUE_TOOTH_ADDRESS,"");
showLog("-------deviceAddressSave-------");
showLog(deviceAddressSave);
if("BLE is not supported".equals(str)){
showToast("当前设备不支持低功耗蓝牙,无法连接传感器采集数据");
}
@ -110,69 +131,85 @@ public class BlueToothDeviceActivity extends BaseActivity {
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (ActivityCompat.checkSelfPermission(BlueToothDeviceActivity.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()){
//showLog(new Gson().toJson(device));
if (device !=null && device.getName() != null && device.getName().startsWith("W3")){
showLog(new Gson().toJson(device));
mLeDeviceListAdapter.addDevice(device);
mLeDeviceListAdapter.notifyDataSetChanged();
if(!TextUtils.isEmpty(deviceNameSave) && !TextUtils.isEmpty(deviceAddressSave)){
selectItem=0;
ble.connect(deviceAddressSave,deviceNameSave);
}
}
} else if (BluetoothLeService.ACTION_GATT_CONNECTED.equals(action)) {
showLog("连接");
mLeDeviceListAdapter.setSelectItem(selectItem);
mLeDeviceListAdapter.notifyDataSetChanged();
BluetoothDevice device = mLeDeviceListAdapter.getDevice(selectItem);
SPUtils.put(context, Contans.KEY_BLUE_TOOTH,new Gson().toJson(device));
String deviceName = device.getName();
String deviceAddress = device.getAddress();
if (deviceName != null && !deviceName.isEmpty()){
SPUtils.put(context, Contans.KEY_BLUE_TOOTH,device.getAddress());
showLog(deviceName);
showLog(deviceAddress);
SPUtils.put(context, Contans.KEY_BLUE_TOOTH_ADDRESS,deviceAddress);
SPUtils.put(context, Contans.KEY_BLUE_TOOTH_NAME,device.getName());
}
} else if (BluetoothLeService.ACTION_GATT_DISCONNECTED.equals(action)) {
showLog("断开");
} else if (BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED.equals(action)) {
showLog("数据传输通道准备就绪,可以发送数据了。");
ble_status.setText("已连接");
ble_status.setTextColor(Color.parseColor("#00FF7F"));//连接之后的颜色
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
ble.write("BV", false);
cmd="BV";
}
},2000);
} else if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)) {
byte[] data= intent.getByteArrayExtra(BluetoothLeService.EXTRA_DATA);
String resultReceive = new String(data);
showLog("收到传感器数据" + resultReceive);
if("VR".equals(cmd)){
if("BV".equals(cmd)){
showLog(resultReceive);
}else{
tx_data.setText("接收数值:"+resultReceive);
if(!TextUtils.isEmpty(resultReceive)){
List<String> listDianLiang = stringToListBlank(resultReceive);
if(!listDianLiang.isEmpty()){
showLog(listDianLiang.get(0));
showLog(listDianLiang.get(1));
List<String> strDianLiang = stringToList(listDianLiang.get(0));
if(!strDianLiang.isEmpty()){
ble_dianlang.setText(strDianLiang.get(1)+strDianLiang.get(0));
}
}
}
}
if("0F3101".equals(cmd)){
ble.write("VR", false);
cmd="VR";
if("0B0B6300".equals(cmd)){
showLog(resultReceive);
if(!TextUtils.isEmpty(resultReceive)){
List<String> listDianLiang = stringToListBlank(resultReceive);
if(!listDianLiang.isEmpty()){
showLog(listDianLiang.get(0));
showLog(listDianLiang.get(1));
List<String> strDianLiang = stringToList(listDianLiang.get(0));
if(!strDianLiang.isEmpty()){
ble_dianlang.setText(strDianLiang.get(1)+"℃");
}
}
}
}
}
}
});
initToolbar("蓝牙数据管理", "扫描并确认", new View.OnClickListener() {
initToolbar("蓝牙传感器管理", "扫描蓝牙", new View.OnClickListener() {
@Override
public void onClick(View v) {
showLog("蓝牙数据管理");
mLeDeviceListAdapter.clear();
mLeDeviceListAdapter.notifyDataSetChanged();
//initBlueTooth();
// if(BleManager.getInstance().isSupportBle()){
// initBlueToothFastBle();
// }else{
// showToast("当前设备不支持低功耗蓝牙,无法连接传感器采集数据");
// }
if("OK".equals(str)){
ble.scan();
}
showLog("扫描蓝牙设备");
blueScan();
}
});
//BleManager.getInstance().init(getApplication());
@ -199,11 +236,6 @@ public class BlueToothDeviceActivity extends BaseActivity {
selectItem=position;
ble.connect(device.getAddress(),device.getName());
}
}, new LeDeviceListAdapter.CallBackBlueToothUnBind() {
@Override
public void Click(int position) {
}
}, new LeDeviceListAdapter.CallBackBlueToothSendData() {
@Override
public void Click(int position) {
@ -214,6 +246,7 @@ public class BlueToothDeviceActivity extends BaseActivity {
//ble.write("BV", false);
//温度测量
ble.write("0B0B6300", true);
cmd="0B0B6300";
//距离测量
//ble.write("4453", true);
//转速测量
@ -231,16 +264,26 @@ public class BlueToothDeviceActivity extends BaseActivity {
lv.setAdapter(mLeDeviceListAdapter);
}
private void blueScan(){
mLeDeviceListAdapter.clear();
mLeDeviceListAdapter.notifyDataSetChanged();
ble.disconnect();
selectItem = -1;
if("OK".equals(str)){
ble.scan();
}
}
@Override
public void initData() {
blueScan();
}
@Override
protected void onDestroy() {
super.onDestroy();
ble.scan();
ble.disconnect();
ble.free();
// if(BleManager.getInstance().getScanSate()== BleScanState.STATE_SCANNING){
// BleManager.getInstance().cancelScan();
// }
@ -432,23 +475,20 @@ public class BlueToothDeviceActivity extends BaseActivity {
TextView deviceName;
TextView deviceStatus;
TextView deviceBind;
TextView deviceDeBind;
TextView sendData;
}
private ArrayList<BluetoothDevice> mLeDevices;
private LayoutInflater mInflator;
private CallBackBlueToothBind callBackBlueToothBind;
private CallBackBlueToothUnBind callBackBlueToothUnBind;
private CallBackBlueToothSendData callBackBlueToothSendData;
public LeDeviceListAdapter(CallBackBlueToothBind callBackBlueToothBind,CallBackBlueToothUnBind callBackBlueToothUnBind,
public LeDeviceListAdapter(CallBackBlueToothBind callBackBlueToothBind,
CallBackBlueToothSendData callBackBlueToothSendData) {
super();
mLeDevices = new ArrayList<BluetoothDevice>();
mInflator = BlueToothDeviceActivity.this.getLayoutInflater();
this.callBackBlueToothBind=callBackBlueToothBind;
this.callBackBlueToothUnBind=callBackBlueToothUnBind;
this.callBackBlueToothSendData=callBackBlueToothSendData;
}
@ -456,10 +496,6 @@ public class BlueToothDeviceActivity extends BaseActivity {
void Click(int position);
}
public interface CallBackBlueToothUnBind {
void Click(int position);
}
public interface CallBackBlueToothSendData {
void Click(int position);
}
@ -502,7 +538,6 @@ public class BlueToothDeviceActivity extends BaseActivity {
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 {
@ -546,14 +581,6 @@ public class BlueToothDeviceActivity extends BaseActivity {
}
}
});
viewHolder.deviceDeBind.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(callBackBlueToothUnBind!=null){
callBackBlueToothUnBind.Click(i);
}
}
});
viewHolder.sendData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
@ -574,4 +601,12 @@ public class BlueToothDeviceActivity extends BaseActivity {
private int selectItem = -1;
}
private List<String> stringToList(String strs) {
String str[] = strs.split(":");
return Arrays.asList(str);
}
private List<String> stringToListBlank(String strs) {
String str[] = strs.split("\n");
return Arrays.asList(str);
}
}

@ -0,0 +1,565 @@
package com.rehome.dywoa.bleUtil;
import android.Manifest;
import android.app.Activity;
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.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
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 com.google.gson.Gson;
import com.rehome.dywoa.Contans;
import com.rehome.dywoa.R;
import com.rehome.dywoa.base.BaseActivity;
import com.rehome.dywoa.utils.SPUtils;
import com.rehome.dywoa.weiget.AuditDialog;
import com.rehome.dywoa.weiget.ConfirmDialog;
import com.rehome.dywoa.weiget.WaitDialog;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
public class BlueToothDeviceSdkActivity extends BaseActivity {
ListView lv;
TextView tvNodata;
TextView ble_status;
TextView ble_dianlang;
// TextView txData;
// TextView ztData;
private String cmd = "";
private LeDeviceListAdapter mLeDeviceListAdapter;
private BluetoothAdapter mBluetoothAdapter;
private BluetoothLeScanner bluetoothLeScanner;
private boolean mScanning;
private Handler mHandler;
private static final int REQUEST_ENABLE_BT = 1;
private static final long SCAN_PERIOD = 10000;
private static final int REQUEST_CODE_ACCESS_COARSE_LOCATION = 123;
private boolean initconn = false;
private String mDeviceAddress;
private String mBleName;
private String deviceNameSave;
private String deviceAddressSave;
private BluetoothLeService mBluetoothLeService;
private boolean mConnected = false;
private ArrayList<String> mPermissionList = new ArrayList();
private WaitDialog dialog;
private Timer timer;
static int bleSendType=0;
@Override
public int getContentViewID() {
return R.layout.activity_bluetoothlist;
}
@Override
public void initView() {
initToolbar("蓝牙传感器管理", "扫描蓝牙", new View.OnClickListener() {
@Override
public void onClick(View v) {
showLog("扫描蓝牙设备");
reConnectBle();
}
});
lv = findViewById(R.id.lv);
tvNodata = findViewById(R.id.tv_nodata);
ble_status = findViewById(R.id.ble_status);
ble_dianlang = findViewById(R.id.ble_dianlang);
dialog = new WaitDialog(context, "正在扫描蓝牙传感器...");
dialog.setCancelable(true);
tvNodata.setVisibility(View.GONE);
lv.setVisibility(View.VISIBLE);
mHandler = new Handler();
}
@Override
public void initData() {
deviceAddressSave = (String) SPUtils.get(context, Contans.KEY_BLUE_TOOTH_ADDRESS,"");
showLog("-------deviceAddressSave-------");
showLog(deviceAddressSave);
deviceNameSave = (String) SPUtils.get(context, Contans.KEY_BLUE_TOOTH_NAME,"");
showLog("-------deviceNameSave-------");
showLog(mBleName);
if(!TextUtils.isEmpty(deviceNameSave) && !TextUtils.isEmpty(deviceAddressSave)){
mHandler.postDelayed(this::reConnectBle, 1000);
}else{
//initPermission();
}
if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_SHORT).show();
//finish();
return;
}
setAdapter();
LocalBroadcastManager.getInstance(this).registerReceiver(mGattUpdateReceiver, makeGattUpdateIntentFilter());
}
private void reConnectBle(){
mLeDeviceListAdapter.clear();
mLeDeviceListAdapter.notifyDataSetChanged();
mLeDeviceListAdapter.setSelectItem(-1);
// 注销广播接收器
LocalBroadcastManager.getInstance(this).unregisterReceiver(mGattUpdateReceiver);
if(mBluetoothLeService!=null){
mBluetoothLeService.disconnect();
mBluetoothLeService.close();
unbindService(mServiceConnection);
}
Intent intent = new Intent(this, BluetoothLeService.class);
stopService(intent);
initPermission();
if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
Toast.makeText(this, R.string.ble_not_supported, Toast.LENGTH_SHORT).show();
//finish();
return;
}
setAdapter();
LocalBroadcastManager.getInstance(this).registerReceiver(mGattUpdateReceiver, makeGattUpdateIntentFilter());
}
// todo 蓝牙动态申请权限
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) {
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();
return;
}
bluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner();
// setAdapter();
Intent gattServiceIntent = new Intent(BlueToothDeviceSdkActivity.this, BluetoothLeService.class);
bindService(gattServiceIntent, mServiceConnection, BIND_AUTO_CREATE);
initconn = true;
//已授权
scanLeDevice(true);
} else {
showToast("您未授权开启蓝牙连接到Blue设备请先开启权限");
}
}
@Override
protected void onResume() {
super.onResume();
if (mBluetoothAdapter != null && (!mBluetoothAdapter.isEnabled())) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
return;
}
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
if(timer==null){
try {
timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
// if(mBluetoothLeService!=null){
// bleSendType++;
// if(bleSendType%2==0){
// mBluetoothLeService.writeHex("0B0B6300");
// }
// if(bleSendType%3==0){
// mBluetoothLeService.writeString("BV");
// }
// }
}
}, 6000, 6000);
// 设定指定的时间time,此处为10000毫秒
} catch (Exception e) {
showLog("timer is nnull");
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_ENABLE_BT && resultCode == Activity.RESULT_CANCELED) {
finish();
return;
}
super.onActivityResult(requestCode, resultCode, data);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
private void scanLeDevice(final boolean enable) {
if (enable) {
dialog.show();
mHandler.postDelayed(() -> {
mScanning = false;
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED) {
return;
}
//mBluetoothAdapter.stopLeScan(mLeScanCallback);
bluetoothLeScanner.stopScan(leScanCallback);
}, SCAN_PERIOD);
mScanning = true;
//mBluetoothAdapter.startLeScan(mLeScanCallback);
bluetoothLeScanner.startScan(leScanCallback);
} else {
mScanning = false;
//mBluetoothAdapter.stopLeScan(mLeScanCallback);
if(bluetoothLeScanner!=null){
bluetoothLeScanner.stopScan(leScanCallback);
}
}
}
//扫描到每台设备后调用回调函数callback.
// private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {
// @Override
// public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
// runOnUiThread(() -> {
// mLeDeviceListAdapter.addDevice(device);
// mLeDeviceListAdapter.notifyDataSetChanged();
// });
// }
// };
private ScanCallback leScanCallback = new ScanCallback() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
super.onScanResult(callbackType, result);
BluetoothDevice device = result.getDevice();
showLog("发现新设备啦");
showLog(new Gson().toJson(device));
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
return;
}
String bleName = device.getName();
String bleAddress = device.getAddress();
showLog(bleName);
showLog(bleAddress);
if (bleName != null && bleName.startsWith("W3")) {
dialog.dismiss();
mLeDeviceListAdapter.addDevice(device);
mLeDeviceListAdapter.notifyDataSetChanged();
if(!TextUtils.isEmpty(deviceNameSave) && !TextUtils.isEmpty(deviceAddressSave) && deviceAddressSave.equals(bleAddress) && deviceNameSave.equals(bleName)){
int count = mLeDeviceListAdapter.getCount();
confirmConnect(count-1);
//mBluetoothLeService.connect(deviceAddressSave);
}
}
}
};
private void setAdapter() {
mLeDeviceListAdapter = new LeDeviceListAdapter();
lv.setAdapter(mLeDeviceListAdapter);
lv.setOnItemClickListener((parent, view, position, id) -> {
AuditDialog confirmDialog = new AuditDialog(context, "您确定要绑定当前蓝牙传感器吗", new AuditDialog.AuditDialogListener() {
@Override
public void confirm() {
confirmConnect(position);
}
@Override
public void cancel() {
}
});
confirmDialog.setCancelable(true);
confirmDialog.show();
});
}
private void confirmConnect(int position) {
final BluetoothDevice device = mLeDeviceListAdapter.getDevice(position);
if (device == null) return;
if (mScanning) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED) {
return;
}
//mBluetoothAdapter.stopLeScan(mLeScanCallback);
bluetoothLeScanner.stopScan(leScanCallback);
mScanning = false;
}
mDeviceAddress = device.getAddress();
mBleName = device.getName();
mLeDeviceListAdapter.setSelectItem(position);//记载当前点击选中的节点position为了变色
mLeDeviceListAdapter.notifyDataSetChanged();
if (initconn) {
if(mBluetoothLeService!=null){
if (!mConnected) mBluetoothLeService.connect(mDeviceAddress);
}
} else {
Intent gattServiceIntent = new Intent(BlueToothDeviceSdkActivity.this, BluetoothLeService.class);
bindService(gattServiceIntent, mServiceConnection, BIND_AUTO_CREATE);
initconn = true;
}
}
private class LeDeviceListAdapter extends BaseAdapter {
private class ViewHolder {
TextView deviceName;
TextView deviceStatus;
}
private ArrayList<BluetoothDevice> mLeDevices;
private LayoutInflater mInflator;
public LeDeviceListAdapter() {
super();
mLeDevices = new ArrayList<BluetoothDevice>();
mInflator = BlueToothDeviceSdkActivity.this.getLayoutInflater();
}
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) {
ViewHolder viewHolder;
if (view == null) {
view = mInflator.inflate(R.layout.listitem_device, null);
viewHolder = new ViewHolder();
viewHolder.deviceName = view.findViewById(R.id.device_name);
viewHolder.deviceStatus = view.findViewById(R.id.device_status);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
BluetoothDevice device = mLeDevices.get(i);
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
return null;
}
final String deviceName = device.getName();
if (deviceName != null && deviceName.length() > 0)
viewHolder.deviceName.setText(deviceName);
else
viewHolder.deviceName.setText(R.string.unknown_device);
//当前点击选中的变色
if (i == selectItem) {
viewHolder.deviceStatus.getPaint().setFakeBoldText(true);
viewHolder.deviceStatus.setTextColor(Color.parseColor("#00FF7F"));//连接之后的颜色
viewHolder.deviceStatus.setText("已绑定");
} else {
viewHolder.deviceStatus.getPaint().setFakeBoldText(false);
viewHolder.deviceStatus.setTextColor(Color.parseColor("#000000"));
viewHolder.deviceStatus.setText("未绑定");
}
return view;
}
public void setSelectItem(int selectItem) {
this.selectItem = selectItem;
}
private int selectItem = -1;
}
private final ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder service) {
mBluetoothLeService = ((BluetoothLeService.LocalBinder) service).getService();
if (!mBluetoothLeService.initialize()) {
finish();
}
mBluetoothLeService.connect(mDeviceAddress);
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
mBluetoothLeService = null;
}
};
private final BroadcastReceiver mGattUpdateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (BluetoothLeService.ACTION_GATT_CONNECTED.equals(action)) {
mConnected = true;
//ztData.setText("蓝牙状态:已连接");
ble_status.setText("已连接");
ble_status.setTextColor(Color.parseColor("#00FF7F"));//连接之后的颜色
SPUtils.put(context, Contans.KEY_BLUE_TOOTH_ADDRESS, mDeviceAddress);
SPUtils.put(context, Contans.KEY_BLUE_TOOTH_NAME, mBleName);
} else if (BluetoothLeService.ACTION_GATT_DISCONNECTED.equals(action)) {
mConnected = false;
//ztData.setText("蓝牙状态:已断开");
ble_status.setText("已断开");
ble_status.setTextColor(Color.parseColor("#ff0000"));//连接之后的颜色
} else if (BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED.equals(action)) {
//ztData.setText("蓝牙状态:已连接,仪器准备就绪");
showLog("数据传输通道准备就绪,可以发送数据了。");
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
mBluetoothLeService.writeString("BV");
}
},2000);
} else if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)) {
//String Data = "";
String resultReceive = intent.getStringExtra(BluetoothLeService.EXTRA_DATA);
// String type = BleData.substring(0, 1);
// if (type.equals("T")) {
// Data = "温度:" + BleData.substring(2, 6);
// } else {
// Data = "震动:" + BleData.substring(2, 6);
// }
if(!TextUtils.isEmpty(resultReceive)){
List<String> listDianLiang = stringToListBlank(resultReceive);
if(!listDianLiang.isEmpty()){
showLog(listDianLiang.get(0));
showLog(listDianLiang.get(1));
List<String> strDianLiang = stringToList(listDianLiang.get(0));
if(!strDianLiang.isEmpty()){
ble_dianlang.setText(strDianLiang.get(1)+strDianLiang.get(0));
}
}
}
showLog(resultReceive);
//txData.setText(Data);
}
}
};
static IntentFilter makeGattUpdateIntentFilter() {
final IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(BluetoothLeService.ACTION_GATT_CONNECTED);
intentFilter.addAction(BluetoothLeService.ACTION_GATT_DISCONNECTED);
intentFilter.addAction(BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED);
intentFilter.addAction(BluetoothLeService.ACTION_DATA_AVAILABLE);
return intentFilter;
}
private List<String> stringToList(String strs) {
String str[] = strs.split(":");
return Arrays.asList(str);
}
private List<String> stringToListBlank(String strs) {
String str[] = strs.split("\n");
return Arrays.asList(str);
}
@Override
protected void onDestroy() {
scanLeDevice(false);
mLeDeviceListAdapter.clear();
if(mBluetoothLeService!=null){
unbindService(mServiceConnection);
}
// 注销广播接收器
LocalBroadcastManager.getInstance(this).unregisterReceiver(mGattUpdateReceiver);
if(timer!=null){
timer.cancel();
}
super.onDestroy();
}
}

@ -7,6 +7,7 @@ import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCallback;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattDescriptor;
import android.bluetooth.BluetoothGattService;
import android.bluetooth.BluetoothManager;
import android.bluetooth.BluetoothProfile;
@ -14,19 +15,22 @@ import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Binder;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;
import androidx.core.app.ActivityCompat;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.UUID;
public class BluetoothLeService extends Service {
private final static String TAG = BluetoothLeService.class.getSimpleName();
private final static String TAG = "app";
private BluetoothManager mBluetoothManager;
private BluetoothAdapter mBluetoothAdapter;
@ -49,6 +53,14 @@ public class BluetoothLeService extends Service {
public final static String EXTRA_DATA =
"com.example.bluetooth.le.EXTRA_DATA";
public BluetoothGattCharacteristic characteristicWrite = null;
public BluetoothGattCharacteristic characteristicRead = null;
// 替换为你的服务和特征UUID
private static final UUID SERVICE_UUID = UUID.fromString("6e400001-b5a3-f393-e0a9-e50e24dcca9e");//服务uuid
private static final UUID CHARACTERISTIC_UUID_READ = UUID.fromString("6e400003-b5a3-f393-e0a9-e50e24dcca9e");//读取蓝牙uuid
private static final UUID CHARACTERISTIC_UUID_WRITE = UUID.fromString("6e400002-b5a3-f393-e0a9-e50e24dcca9e");//蓝牙写入数据uuid
private static final UUID CLIENT_CHARACTERISTIC_CONFIG = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb");//开启蓝牙通知配置,固定写死
// Implements callback methods for GATT events that the app cares about. For example,
@ -58,25 +70,20 @@ public class BluetoothLeService extends Service {
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
String intentAction;
if (newState == BluetoothProfile.STATE_CONNECTED) {
// 连接成功,开始发现服务
intentAction = ACTION_GATT_CONNECTED;
mConnectionState = STATE_CONNECTED;
broadcastUpdate(intentAction);
Log.i(TAG, "Connected to GATT server.");
// Attempts to discover services after successful connection.
if (ActivityCompat.checkSelfPermission(BluetoothLeService.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;
}
Log.i(TAG, "Attempting to start service discovery:" +
mBluetoothGatt.discoverServices());
boolean isDiscovery = mBluetoothGatt.discoverServices();
Log.i(TAG, "Attempting to start service discovery:" + isDiscovery);
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
mBluetoothGatt.close();
intentAction = ACTION_GATT_DISCONNECTED;
mConnectionState = STATE_DISCONNECTED;
Log.i(TAG, "Disconnected from GATT server.");
@ -86,34 +93,68 @@ public class BluetoothLeService extends Service {
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
super.onServicesDiscovered(gatt, status);
if (status == BluetoothGatt.GATT_SUCCESS) {
broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);
////tsp默认选择一个地址
BluetoothGattCharacteristic characteristic1=
gatt.getService(UUID.fromString("0000ffe0-0000-1000-8000-00805f9b34fb")).getCharacteristic(UUID.fromString("0000ffe4-0000-1000-8000-00805f9b34fb"));
setCharacteristicNotification(characteristic1,true);
BluetoothGattService service = gatt.getService(SERVICE_UUID);
if (service != null) {
BluetoothGattCharacteristic characteristic = service.getCharacteristic(CHARACTERISTIC_UUID_READ);
if (characteristic != null) {
// 启用通知
enableCharacteristicNotification(gatt, characteristic);
broadcastUpdate(ACTION_GATT_SERVICES_DISCOVERED);
}
}
} else {
Log.w(TAG, "onServicesDiscovered received: " + status);
Log.i(TAG, "onServicesDiscovered received: " + status);
}
}
@Override
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicRead(gatt, characteristic, status);
if (status == BluetoothGatt.GATT_SUCCESS) {
//普通读取蓝牙设备数据,有些设备会用到,当前蓝牙传感器没用这个
byte[] data = characteristic.getValue();
String receivedData = new String(data);
Log.i(TAG, "读取数据: " + receivedData);
broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
}
}
@Override
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
super.onCharacteristicChanged(gatt, characteristic);
// 收到通知数据
byte[] data = characteristic.getValue();
String receivedData = new String(data);
Log.i(TAG, "收到通知数据: " + receivedData);
broadcastUpdate(ACTION_DATA_AVAILABLE, characteristic);
}
@Override
public void onCharacteristicWrite (BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status){
@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicWrite(gatt, characteristic, status);
if (status == BluetoothGatt.GATT_SUCCESS) {
Log.i(TAG, "Data written successfully");
} else {
Log.e(TAG, "Failed to write data: " + status);
}
}
};
private void enableCharacteristicNotification(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
return;
}
gatt.setCharacteristicNotification(characteristic, true);
BluetoothGattDescriptor descriptor = characteristic.getDescriptor(CLIENT_CHARACTERISTIC_CONFIG);
if (descriptor != null) {
descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
gatt.writeDescriptor(descriptor);
}
}
private void broadcastUpdate(final String action) {
final Intent intent = new Intent(action);
LocalBroadcastManager.getInstance(BluetoothLeService.this).sendBroadcast(intent);
@ -123,13 +164,18 @@ public class BluetoothLeService extends Service {
private void broadcastUpdate(final String action, final BluetoothGattCharacteristic characteristic) {
final Intent intent = new Intent(action);
// For all other profiles, writes the data formatted in HEX.
final byte[] data = characteristic.getValue();
if (data != null && data.length > 0) {
final StringBuilder stringBuilder = new StringBuilder(data.length);
for(byte byteChar : data)
stringBuilder.append(String.format("%02X ", byteChar));
intent.putExtra(EXTRA_DATA, new String(data) + "\n" + stringBuilder.toString());
}
// final byte[] data = characteristic.getValue();
// 收到通知数据
byte[] data = characteristic.getValue();
String receivedData = new String(data, StandardCharsets.UTF_8);
//Log.i(TAG, "收到通知数据: " + receivedData);
// if (data != null && data.length > 0) {
// final StringBuilder stringBuilder = new StringBuilder(data.length);
// for (byte byteChar : data)
// stringBuilder.append(String.format("%02X ", byteChar));
// intent.putExtra(EXTRA_DATA, new String(data) + "\n" + stringBuilder.toString());
// }
intent.putExtra(EXTRA_DATA, receivedData);
LocalBroadcastManager.getInstance(BluetoothLeService.this).sendBroadcast(intent);
}
@ -184,11 +230,10 @@ public class BluetoothLeService extends Service {
* Connects to the GATT server hosted on the Bluetooth LE device.
*
* @param address The device address of the destination device.
*
* @return Return true if the connection is initiated successfully. The connection result
* is reported asynchronously through the
* {@code BluetoothGattCallback#onConnectionStateChange(android.bluetooth.BluetoothGatt, int, int)}
* callback.
* is reported asynchronously through the
* {@code BluetoothGattCallback#onConnectionStateChange(android.bluetooth.BluetoothGatt, int, int)}
* callback.
*/
public boolean connect(final String address) {//连接设备
if (mBluetoothAdapter == null || address == null) {
@ -197,16 +242,9 @@ public class BluetoothLeService extends Service {
}
// Previously connected device. Try to reconnect.
if (mBluetoothDeviceAddress != null && address.equals(mBluetoothDeviceAddress)&& mBluetoothGatt != null) {
if (address.equals(mBluetoothDeviceAddress) && mBluetoothGatt != null) {
Log.d(TAG, "Trying to use an existing mBluetoothGatt for connection.");
if (ActivityCompat.checkSelfPermission(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 false;
}
if (mBluetoothGatt.connect()) {
@ -224,13 +262,36 @@ public class BluetoothLeService extends Service {
}
// We want to directly connect to the device, so we are setting the autoConnect
// parameter to false.
mBluetoothGatt = device.connectGatt(this, false, mGattCallback);
//mBluetoothGatt = device.connectGatt(this, false, mGattCallback);
mBluetoothGatt = device.connectGatt(this, false, mGattCallback, BluetoothDevice.TRANSPORT_LE);
Log.d(TAG, "Trying to create a new connection.");
mBluetoothDeviceAddress = address;
mConnectionState = STATE_CONNECTING;
return true;
}
public boolean checkBleConnect(final String address) {
if (mBluetoothAdapter == null || address == null) {
return false;
}
if (address.equals(mBluetoothDeviceAddress) && mBluetoothGatt != null) {
Log.d(TAG, "Trying to use an existing mBluetoothGatt for connection.");
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT) != PackageManager.PERMISSION_GRANTED) {
return false;
}
if (mBluetoothGatt.connect()) {
return true;
} else {
return false;
}
}
final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
if (device == null) {
return false;
}
return false;
}
/**
* Disconnects an existing connection or cancel a pending connection. The disconnection result
* is reported asynchronously through the
@ -243,16 +304,11 @@ public class BluetoothLeService extends Service {
return;
}
if (ActivityCompat.checkSelfPermission(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;
}
mBluetoothGatt.disconnect();
mBluetoothGatt.close();
mBluetoothGatt = null;
}
/**
@ -277,70 +333,54 @@ public class BluetoothLeService extends Service {
mBluetoothGatt = null;
}
/**
* Request a read on a given {@code BluetoothGattCharacteristic}. The read result is reported
* asynchronously through the {@code BluetoothGattCallback#onCharacteristicRead(android.bluetooth.BluetoothGatt, android.bluetooth.BluetoothGattCharacteristic, int)}
* callback.
*
* @param characteristic The characteristic to read from.
*/
public void readCharacteristic(BluetoothGattCharacteristic characteristic) {
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
Log.w(TAG, "BluetoothAdapter not initialized");
return;
public boolean writeData(byte[] data) {
if (mBluetoothGatt == null) {
Log.e("TAG", "BluetoothGatt is null");
return false;
}
if (ActivityCompat.checkSelfPermission(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;
BluetoothGattService service = mBluetoothGatt.getService(SERVICE_UUID);
if (service == null) {
Log.e("TAG", "Service not found");
return false;
}
mBluetoothGatt.readCharacteristic(characteristic);
}
public void writeCharacteristic(BluetoothGattCharacteristic characteristic) {
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
Log.w(TAG, "BluetoothAdapter not initialized");
return;
BluetoothGattCharacteristic characteristic = service.getCharacteristic(CHARACTERISTIC_UUID_WRITE);
if (characteristic == null) {
Log.e("TAG", "Characteristic not found");
return false;
}
// 设置写入类型
int properties = characteristic.getProperties();
if ((properties & BluetoothGattCharacteristic.PROPERTY_WRITE) > 0) {
characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
} else if ((properties & BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE) > 0) {
characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
}
characteristic.setValue(data);
if (ActivityCompat.checkSelfPermission(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;
return false;
}
mBluetoothGatt.writeCharacteristic(characteristic);
return mBluetoothGatt.writeCharacteristic(characteristic);
}
/**
* Enables or disables notification on a give characteristic.
*
* @param characteristic Characteristic to act on.
* @param enabled If true, enable notification. False otherwise.
*/
//选择地址
public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic, boolean enabled) {
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
Log.w(TAG, "BluetoothAdapter not initialized");
return;
}
if (ActivityCompat.checkSelfPermission(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;
// 写入字符串数据
public boolean writeString(String message) {
byte[] data = message.getBytes(StandardCharsets.UTF_8);
return writeData(data);
}
// 写入16进制数据
public boolean writeHex(String hexString) {
int len = hexString.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4)
+ Character.digit(hexString.charAt(i + 1), 16));
}
mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);
return writeData(data);
}
public List<BluetoothGattService> getSupportedGattServices() {

@ -2,13 +2,16 @@ package com.rehome.dywoa.ui.activity
import android.content.ComponentName
import android.content.ContentValues
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.graphics.Color
import android.os.Build
import android.os.IBinder
import android.text.TextUtils
import android.util.Log
import android.view.Gravity
@ -59,6 +62,7 @@ import com.rehome.dywoa.bean.ScxjjhBean
import com.rehome.dywoa.bean.StatusInfo
import com.rehome.dywoa.bean.StatusInfoSingleTask
import com.rehome.dywoa.bean.WaitForBean
import com.rehome.dywoa.bleUtil.BluetoothLeService
import com.rehome.dywoa.databinding.ActivityHomeBinding
import com.rehome.dywoa.entity.ScDjjhInfo
import com.rehome.dywoa.ui.fragment.HomeFragment
@ -81,6 +85,7 @@ import com.yolanda.nohttp.rest.Response
import org.litepal.crud.DataSupport
import java.io.File
import java.text.SimpleDateFormat
import java.util.Arrays
import java.util.Calendar
import java.util.Timer
import java.util.TimerTask
@ -118,6 +123,15 @@ class HomeActivity : BaseActivityOaToolbarViewBinding<ActivityHomeBinding>() {
private var manager: DownloadManager? = null
// private lateinit var ble: BleManager
//
// private var cmd: String = ""
// private var str: String = ""
// private var deviceNameSave: String = ""
// private var deviceAddressSave: String = ""
private var mBluetoothLeService: BluetoothLeService?=null
override fun getViewBinding() = ActivityHomeBinding.inflate(layoutInflater)
override fun getToolbar() = binding.toolbarView.toolbar
@ -137,6 +151,8 @@ class HomeActivity : BaseActivityOaToolbarViewBinding<ActivityHomeBinding>() {
initMqtt()
}
initToolbar("首页")
binding.toolbarView.topLl.visibility = View.GONE
StatusBarUtil.setColor(this, ContextCompat.getColor(context, R.color.app_status_bar))
@ -248,8 +264,25 @@ class HomeActivity : BaseActivityOaToolbarViewBinding<ActivityHomeBinding>() {
getXjZhTjDropDown("zy");
getXjZhTjDropDown("zc");
getXjZhTjDropDown("lx");
val gattServiceIntent = Intent(this, BluetoothLeService::class.java)
bindService(gattServiceIntent, mServiceConnection, BIND_AUTO_CREATE)
}
private val mServiceConnection: ServiceConnection = object : ServiceConnection {
override fun onServiceConnected(componentName: ComponentName, service: IBinder) {
// 服务连接成功时调用
val binder = service as BluetoothLeService.LocalBinder
mBluetoothLeService = binder.service
}
override fun onServiceDisconnected(componentName: ComponentName) {
mBluetoothLeService=null
}
}
private fun initMqtt() {
////开启MQTT推送服务
mqttPublishServer = MqttSSLPublishServer(context)
@ -460,19 +493,7 @@ class HomeActivity : BaseActivityOaToolbarViewBinding<ActivityHomeBinding>() {
checkConnectServer()
}
override fun onDestroy() {
super.onDestroy()
if (timerCheckServerConnectStatus != null) {
timerCheckServerConnectStatus?.cancel()
}
if (mqttPublishServer != null) {
mqttPublishServer!!.destroyAll()
mqttPublishServer = null
}
if (timer != null) {
timer!!.cancel()
}
}
override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
if (keyCode == KeyEvent.KEYCODE_BACK && event.action == KeyEvent.ACTION_DOWN) {
@ -1225,4 +1246,48 @@ class HomeActivity : BaseActivityOaToolbarViewBinding<ActivityHomeBinding>() {
}
})
}
private fun stringToList(strs: String): List<String> {
val str = strs.split(":".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
return Arrays.asList(*str)
}
private fun stringToListBlank(strs: String): List<String> {
val str = strs.split("\n".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
return Arrays.asList(*str)
}
override fun onStart() {
super.onStart()
// val gattServiceIntent = Intent(this, BluetoothLeService::class.java)
// bindService(gattServiceIntent, mServiceConnection, BIND_AUTO_CREATE)
}
override fun onStop() {
super.onStop()
// if(mBluetoothLeService!=null){
// unbindService(mServiceConnection)
// }
}
override fun onDestroy() {
super.onDestroy()
if (timerCheckServerConnectStatus != null) {
timerCheckServerConnectStatus?.cancel()
}
if (mqttPublishServer != null) {
mqttPublishServer!!.destroyAll()
mqttPublishServer = null
}
if (timer != null) {
timer!!.cancel()
}
mBluetoothLeService?.disconnect()
mBluetoothLeService?.close()
if(mBluetoothLeService!=null){
unbindService(mServiceConnection)
}
val intent = Intent(this, BluetoothLeService::class.java)
stopService(intent)
}
}

@ -210,8 +210,23 @@ class LoginActivity : BaseActivityOaToolbarViewBinding<ActivityLoginBinding>() {
// binding.etPassword.setText("A000000a.")
//ceshi1
binding.etUsername.setText("ceshi1")
binding.etPassword.setText("A000000a.")
// binding.etUsername.setText("ceshi1")
// binding.etPassword.setText("CS20251119.")
//黄文飞
// binding.etUsername.setText("RH00002")
// binding.etPassword.setText("RH20251029.")
// binding.etUsername.setText("972526")
// binding.etPassword.setText("Wq20001221.")
// binding.etUsername.setText("TD00046")
// binding.etPassword.setText("A000000a.")
// binding.etUsername.setText("371523")
// binding.etPassword.setText("A000000a.")
//瑞洪 RH00002/王总 RH00002/chao工 RH00003/范红波
@ -298,7 +313,6 @@ class LoginActivity : BaseActivityOaToolbarViewBinding<ActivityLoginBinding>() {
//checkVpnLogin()
// appLogin() //登录
})
}
override fun onRestart() {
@ -938,6 +952,14 @@ class LoginActivity : BaseActivityOaToolbarViewBinding<ActivityLoginBinding>() {
startActivity(intent)
finish()
}
if((!TextUtils.isEmpty(userInfo.firstDeparment))&&(userInfo.firstDeparment.equals("生产科技部"))){
showToast("生产科技部仓管为了支持点检功能,支持离线登录,有效期一天。")
App.getInstance().userInfo = userInfo
val intent = Intent(context, HomeActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP)
startActivity(intent)
finish()
}
}
} else {
Log.i("login", userInfo.statusMsg)

@ -1,7 +1,14 @@
package com.rehome.dywoa.ui.activity.sbxdjgl;
import static android.content.Context.BIND_AUTO_CREATE;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
@ -22,9 +29,11 @@ import com.rehome.dywoa.DBModel.DjjhRwQy;
import com.rehome.dywoa.R;
import com.rehome.dywoa.base.BaseFragment;
import com.rehome.dywoa.bean.SisReaultBean;
import com.rehome.dywoa.bleUtil.BluetoothLeService;
import com.rehome.dywoa.utils.GsonUtils;
import com.rehome.dywoa.utils.HttpListener;
import com.rehome.dywoa.utils.NoProgresshttpUtils;
import com.rehome.dywoa.utils.SPUtils;
import com.rehome.dywoa.utils.UiUtlis;
import com.rehome.dywoa.weiget.ListDialog;
import com.yolanda.nohttp.NoHttp;
@ -69,6 +78,7 @@ public class CJFragment extends BaseFragment {
TextView tv_sis_value;
//Button btn_next;
Button btn_selectCheckResult;
Button btn_cj;
LinearLayout ll_bz;
LinearLayout ll_et_jg;
@ -86,6 +96,11 @@ public class CJFragment extends BaseFragment {
private double bjsx = 0;//报警上限
private double bjxx = 0;//报警下限
private String deviceNameSave;
private String deviceAddressSave;
private BluetoothLeService mBluetoothLeService;
public CJFragment() {
}
@ -113,6 +128,8 @@ public class CJFragment extends BaseFragment {
findView();
et_jg.setEnabled(isEdit);
btn_selectCheckResult.setEnabled(isEdit);
deviceAddressSave = (String) SPUtils.get(context, Contans.KEY_BLUE_TOOTH_ADDRESS,"");
deviceNameSave = (String) SPUtils.get(context, Contans.KEY_BLUE_TOOTH_NAME,"");
updata(info, index, zj);
}
@ -145,6 +162,7 @@ public class CJFragment extends BaseFragment {
ll_bz = view.findViewById(R.id.ll_bz);
ll_et_jg = view.findViewById(R.id.ll_et_jg);
sv = view.findViewById(R.id.sv);
btn_cj = view.findViewById(R.id.btn_cj);
// btn_next.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View v) {
@ -191,6 +209,70 @@ public class CJFragment extends BaseFragment {
dialog.show();
}
});
btn_cj.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mBluetoothLeService!=null){
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if(isEdit){
if(!TextUtils.isEmpty(info.getPOINTNAME())){
if(info.getPOINTNAME().contains("温度")){
mBluetoothLeService.writeHex("0B0B6301");
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
mBluetoothLeService.writeString("BV");
}
}, 3000);
}
if(info.getPOINTNAME().contains("测温")){
mBluetoothLeService.writeHex("0B0B6301");
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
mBluetoothLeService.writeString("BV");
}
}, 3000);
}
if(info.getPOINTNAME().contains("环境温度")){
mBluetoothLeService.writeHex("0B0B6300");
}
if(info.getPOINTNAME().contains("环境测温")){
mBluetoothLeService.writeHex("0B0B6300");
}
if(info.getPOINTNAME().contains("振动")){
mBluetoothLeService.writeHex("1F12");
}
if(info.getPOINTNAME().contains("振动加速度")){
mBluetoothLeService.writeHex("1F11");
}
if(info.getPOINTNAME().contains("振动速度")){
mBluetoothLeService.writeHex("1F12");
}
if(info.getPOINTNAME().contains("振动位移")){
mBluetoothLeService.writeHex("1F13");
}
if(info.getPOINTNAME().contains("测振")){
mBluetoothLeService.writeHex("1F12");
}
if(info.getPOINTNAME().contains("测振加速度")){
mBluetoothLeService.writeHex("1F11");
}
if(info.getPOINTNAME().contains("测振速度")){
mBluetoothLeService.writeHex("1F12");
}
if(info.getPOINTNAME().contains("测振位移")){
mBluetoothLeService.writeHex("1F13");
}
}
}
}
},1000);
}
}
});
}
@Override
@ -232,6 +314,11 @@ public class CJFragment extends BaseFragment {
public void afterTextChanged(Editable s) {
}
});
if(isEdit){
Intent gattServiceIntent = new Intent(requireActivity(), BluetoothLeService.class);
requireActivity().bindService(gattServiceIntent, mServiceConnection, BIND_AUTO_CREATE);
}
}
private void getSisDate(String sisNo){
@ -333,7 +420,6 @@ public class CJFragment extends BaseFragment {
tv_kks.setText(info.getASSETNUM());
}
if(!TextUtils.isEmpty(info.getUNITOFMEASURE())){
tv_dw.setVisibility(View.VISIBLE);
}else{
@ -347,17 +433,61 @@ public class CJFragment extends BaseFragment {
if(!TextUtils.isEmpty(info.getMEAMETHOD())){
if(info.getMEAMETHOD().equals("抄表")){
//android:digits="0123456789.-"
btn_cj.setVisibility(View.GONE);
ll_bz.setVisibility(View.GONE);
tv_dw.setVisibility(View.VISIBLE);
ll_et_jg.setVisibility(View.VISIBLE);
btn_selectCheckResult.setVisibility(View.GONE);
//et_jg.setInputType(InputType.TYPE_CLASS_NUMBER|InputType.TYPE_NUMBER_FLAG_DECIMAL);
//et_jg.setText("正常");
if(isEdit){
if(!TextUtils.isEmpty(info.getPOINTNAME())){
if(!TextUtils.isEmpty(deviceNameSave) && !TextUtils.isEmpty(deviceAddressSave)){
if(info.getPOINTNAME().contains("温度")){
btn_cj.setVisibility(View.VISIBLE);
}
if(info.getPOINTNAME().contains("测温")){
btn_cj.setVisibility(View.VISIBLE);
}
if(info.getPOINTNAME().contains("环境温度")){
btn_cj.setVisibility(View.VISIBLE);
}
if(info.getPOINTNAME().contains("环境测温")){
btn_cj.setVisibility(View.VISIBLE);
}
if(info.getPOINTNAME().contains("振动")){
btn_cj.setVisibility(View.VISIBLE);
}
if(info.getPOINTNAME().contains("振动加速度")){
btn_cj.setVisibility(View.VISIBLE);
}
if(info.getPOINTNAME().contains("振动速度")){
btn_cj.setVisibility(View.VISIBLE);
}
if(info.getPOINTNAME().contains("振动位移")){
btn_cj.setVisibility(View.VISIBLE);
}
if(info.getPOINTNAME().contains("测振")){
btn_cj.setVisibility(View.VISIBLE);
}
if(info.getPOINTNAME().contains("测振加速度")){
btn_cj.setVisibility(View.VISIBLE);
}
if(info.getPOINTNAME().contains("测振速度")){
btn_cj.setVisibility(View.VISIBLE);
}
if(info.getPOINTNAME().contains("测振位移")){
btn_cj.setVisibility(View.VISIBLE);
}
}
}
}
}else{
ll_bz.setVisibility(View.GONE);
tv_dw.setVisibility(View.GONE);
ll_et_jg.setVisibility(View.GONE);
btn_selectCheckResult.setVisibility(View.VISIBLE);
btn_cj.setVisibility(View.GONE);
//et_jg.setInputType(InputType.TYPE_CLASS_TEXT);
}
}
@ -506,4 +636,29 @@ public class CJFragment extends BaseFragment {
public String getEditTextFxContent() {
return etFxnr.getText().toString().trim();
}
public void getBlueToothData(String bleData) {
et_jg.setText(bleData);
}
private final ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder service) {
mBluetoothLeService = ((BluetoothLeService.LocalBinder) service).getService();
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
mBluetoothLeService = null;
}
};
@Override
public void onDestroy() {
super.onDestroy();
if(isEdit){
if(mBluetoothLeService!=null){
requireActivity().unbindService(mServiceConnection);
}
}
}
}

@ -19,6 +19,8 @@ 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.bleUtil.BlueToothDeviceSdkActivity;
import com.rehome.dywoa.ui.activity.sbxj.NFCInfoActivity;
import com.rehome.dywoa.utils.GsonUtils;
import com.rehome.dywoa.utils.HttpListener;
@ -43,9 +45,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 +123,12 @@ 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 = new Intent(SbxdjglActivity.this, BlueToothDeviceSdkActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
break;
}
}
});
@ -161,11 +169,15 @@ public class SbxdjglActivity extends BaseActivity2 {
param.put("YHID", username);
if(!TextUtils.isEmpty(firstDeparment)){
if(firstDeparment.equals("设备部")||firstDeparment.equals("承包商")||firstDeparment.equals("粤电服大亚湾项目部")){
param.put("firstDeparment", firstDeparment);
param.put("secondDeparment", secondDeparment);
param.put("firstDeparmentId", firstDeparmentId);
param.put("secondDeparmentId", secondDeparmentId);
if(firstDeparment.equals("设备部")||firstDeparment.equals("承包商")||firstDeparment.equals("生产科技部")||firstDeparment.equals("粤电服大亚湾项目部")){
if(username!=null&&username.equals("RH00002")&&firstDeparment.equals("承包商")){
showLog("RH00002 test account");
}else{
param.put("firstDeparment", firstDeparment);
param.put("secondDeparment", secondDeparment);
param.put("firstDeparmentId", firstDeparmentId);
param.put("secondDeparmentId", secondDeparmentId);
}
}
}

@ -516,5 +516,4 @@ public class SdjgzActivity extends BaseActivity {
showLog("-------已检未上传点数------");
}
}
}
// 6217003090002510800
}

@ -1,14 +1,18 @@
package com.rehome.dywoa.ui.activity.sbxdjgl;
import android.content.BroadcastReceiver;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.appcompat.app.AlertDialog;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import android.text.TextUtils;
import android.view.View;
@ -21,6 +25,7 @@ import com.rehome.dywoa.DBModel.DjjhRwQy;
import com.rehome.dywoa.R;
import com.rehome.dywoa.adapter.MyFragmentYXAdapter;
import com.rehome.dywoa.base.BaseFragment;
import com.rehome.dywoa.bleUtil.BluetoothLeService;
import com.rehome.dywoa.ui.activity.sbxj.SbxjcjsbActivity;
import com.rehome.dywoa.utils.ControllerActivity;
import com.rehome.dywoa.utils.GsonUtils;
@ -31,6 +36,7 @@ import org.litepal.crud.DataSupport;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
@ -90,6 +96,16 @@ public class SjcjFragment extends BaseFragment {
}
}
@Override
public int getLayoutId() {
return R.layout.fragment_scjsj;
}
@Override
protected void initView() {
findView();
}
private void findView() {
vp = view.findViewById(R.id.vp);
rb1 = view.findViewById(R.id.rb1);
@ -231,20 +247,12 @@ public class SjcjFragment extends BaseFragment {
});
}
@Override
protected void initView() {
findView();
}
@Override
public int getLayoutId() {
return R.layout.fragment_scjsj;
}
public void initData() {
if (!isEdit) {
btn_save_next.setVisibility(View.GONE);
}else{
LocalBroadcastManager.getInstance(requireActivity()).registerReceiver(mGattUpdateReceiver, makeGattUpdateIntentFilter());
}
list = new ArrayList<>();
if (!lists.isEmpty()) {
@ -382,4 +390,76 @@ public class SjcjFragment extends BaseFragment {
bz.updata(lists.get(0).getMEASTANDARD());
ff.update(lists.get(0).getMEAMETHOD());
}
private final BroadcastReceiver mGattUpdateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
String bleData = "";
if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)) {
String resultReceive = intent.getStringExtra(BluetoothLeService.EXTRA_DATA);
String type = resultReceive.substring(0, 1);
showLog("data:"+resultReceive);
if (type.equals("T")) {
if(!TextUtils.isEmpty(resultReceive)){
List<String> listWenDu = stringToListBlank(resultReceive);
if(!listWenDu.isEmpty()){
showLog(listWenDu.get(0));
showLog(listWenDu.get(1));
List<String> strWenDu = stringToList(listWenDu.get(0));
if(!strWenDu.isEmpty()){
bleData = strWenDu.get(1);
}
}
}
//bleData = "温度:" + data.substring(2, 6);
} else if("a".equals(type)) {
List<String> strWenDu = stringToList(resultReceive);
if(!strWenDu.isEmpty()){
bleData = strWenDu.get(1);
}
}else if("v".equals(type)) {
List<String> strWenDu = stringToList(resultReceive);
if(!strWenDu.isEmpty()){
bleData = strWenDu.get(1);
}
}else if("d".equals(type)) {
List<String> strWenDu = stringToList(resultReceive);
if(!strWenDu.isEmpty()){
bleData = strWenDu.get(1);
}
}
if(!TextUtils.isEmpty(bleData)){
cj.getBlueToothData(bleData);
}
}
}
};
static IntentFilter makeGattUpdateIntentFilter() {
final IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(BluetoothLeService.ACTION_GATT_CONNECTED);
intentFilter.addAction(BluetoothLeService.ACTION_GATT_DISCONNECTED);
intentFilter.addAction(BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED);
intentFilter.addAction(BluetoothLeService.ACTION_DATA_AVAILABLE);
return intentFilter;
}
@Override
public void onDestroy() {
super.onDestroy();
if (isEdit) {
// 注销广播接收器
LocalBroadcastManager.getInstance(requireActivity()).unregisterReceiver(mGattUpdateReceiver);
}
}
private List<String> stringToList(String strs) {
String str[] = strs.split(":");
return Arrays.asList(str);
}
private List<String> stringToListBlank(String strs) {
String str[] = strs.split("\n");
return Arrays.asList(str);
}
}

@ -289,11 +289,15 @@ public class XzjhFragment extends BaseFragment {
param.put("YHID", username);
if(!TextUtils.isEmpty(firstDeparment)){
if(firstDeparment.equals("设备部")||firstDeparment.equals("承包商")||firstDeparment.equals("粤电服大亚湾项目部")){
param.put("firstDeparment", firstDeparment);
param.put("secondDeparment", secondDeparment);
param.put("firstDeparmentId", firstDeparmentId);
param.put("secondDeparmentId", secondDeparmentId);
if(firstDeparment.equals("设备部")||firstDeparment.equals("承包商")||firstDeparment.equals("生产科技部")||firstDeparment.equals("粤电服大亚湾项目部")){
if(username!=null&&username.equals("RH00002")&&firstDeparment.equals("承包商")){
showLog("RH00002 test account");
}else{
param.put("firstDeparment", firstDeparment);
param.put("secondDeparment", secondDeparment);
param.put("firstDeparmentId", firstDeparmentId);
param.put("secondDeparmentId", secondDeparmentId);
}
}
}
@ -669,11 +673,15 @@ public class XzjhFragment extends BaseFragment {
param.put("YHID", username);
if(!TextUtils.isEmpty(firstDeparment)){
if(firstDeparment.equals("设备部")||firstDeparment.equals("承包商")||firstDeparment.equals("粤电服大亚湾项目部")){
param.put("firstDeparment", firstDeparment);
param.put("secondDeparment", secondDeparment);
param.put("firstDeparmentId", firstDeparmentId);
param.put("secondDeparmentId", secondDeparmentId);
if(firstDeparment.equals("设备部")||firstDeparment.equals("承包商")||firstDeparment.equals("生产科技部")||firstDeparment.equals("粤电服大亚湾项目部")){
if(username!=null&&username.equals("RH00002")&&firstDeparment.equals("承包商")){
showLog("RH00002 test account");
}else{
param.put("firstDeparment", firstDeparment);
param.put("secondDeparment", secondDeparment);
param.put("firstDeparmentId", firstDeparmentId);
param.put("secondDeparmentId", secondDeparmentId);
}
}
}

@ -20,6 +20,7 @@ import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.google.gson.Gson;
import com.luck.picture.lib.basic.PictureSelector;
import com.luck.picture.lib.entity.LocalMedia;
import com.rehome.dywoa.App;
@ -457,6 +458,10 @@ public class SxgzActivity extends BaseActivity {
if (adapter == null) {
for(XSJJHXZDataBean item : xsjjhxzDataBeanList){
showLog(new Gson().toJson(item));
}
adapter = new CommonAdapter<XSJJHXZDataBean>(context, R.layout.xsgz_item, xsjjhxzDataBeanList) {
@Override
protected void convert(ViewHolder viewHolder, XSJJHXZDataBean item, int position) {
@ -588,7 +593,7 @@ public class SxgzActivity extends BaseActivity {
// result = "048B94EAC21B91";
// result = "041894EAC21B91";
// result = "04261EEA211991";
// result = "0442C2EA211991";
if(TextUtils.isEmpty(result)){
return;

@ -38,6 +38,7 @@ import com.rehome.dywoa.bean.GridItem;
import com.rehome.dywoa.bean.WaitForBean;
import com.rehome.dywoa.bleUtil.BlueTestInfoActivity;
import com.rehome.dywoa.bleUtil.BlueToothDeviceActivity;
import com.rehome.dywoa.bleUtil.BlueToothDeviceSdkActivity;
import com.rehome.dywoa.ui.activity.BiShowActivity;
import com.rehome.dywoa.ui.activity.EveryDateRiskControllerListActivity;
import com.rehome.dywoa.ui.activity.FaceRecognitionAppActivity;
@ -111,7 +112,6 @@ public class HomeFragment extends BaseFragment {
SimpleDateFormat sp = new SimpleDateFormat("yyyy-MM-dd");
private BleManager ble;
//人脸识别
ActivityResultLauncher<Intent> launcherResultFaceRecognition;
@ -133,45 +133,6 @@ public class HomeFragment extends BaseFragment {
username = App.getInstance().getUserInfo().getManid();
firstDeparment = App.getInstance().getUserInfo().getFirstDeparment();
ble= BleManager.getInstance();
String str=ble.init(requireActivity());
if("OK".equals(str)){
}
ble.setOnReceiveListener(new BleManager.OnReceiveListener() {
@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(requireActivity(), 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()){
showLog(new Gson().toJson(device));
}
} else if (BluetoothLeService.ACTION_GATT_CONNECTED.equals(action)) {
showLog("连接");
} else if (BluetoothLeService.ACTION_GATT_DISCONNECTED.equals(action)) {
showLog("断开");
} else if (BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED.equals(action)) {
showLog("数据传输通道准备就绪,可以发送数据了。");
} else if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action)) {
byte[] data= intent.getByteArrayExtra(BluetoothLeService.EXTRA_DATA);
showLog("收到传感器数据" + new String(data));
}
}
});
//android 10 以上
if (context.getExternalFilesDir(null) != null) {
@ -333,7 +294,7 @@ public class HomeFragment extends BaseFragment {
break;
case 4:
//巡检
if((firstDeparment!=null&&firstDeparment.equals("运行部"))||(firstDeparment!=null&&firstDeparment.equals("承包商"))||(username!=null&&username.equals("371522"))||(username!=null&&username.equals("ceshi1"))||(username!=null&&username.equals("RH00001"))){
if((firstDeparment!=null&&firstDeparment.equals("运行部"))||(firstDeparment!=null&&firstDeparment.equals("承包商"))||(username!=null&&username.equals("371522"))||(username!=null&&username.equals("ceshi1"))||(username!=null&&username.equals("RH00002"))||(username!=null&&username.equals("RH00020"))){
Intent intentXj= new Intent(mActivity, XscbglActivity.class);
intentXj.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intentXj);
@ -343,7 +304,7 @@ public class HomeFragment extends BaseFragment {
break;
case 5:
//点检
if((firstDeparment!=null&&firstDeparment.equals("设备部"))||(firstDeparment!=null&&firstDeparment.equals("粤电服大亚湾项目部"))||(firstDeparment!=null&&firstDeparment.equals("承包商"))||(username!=null&&username.equals("371522"))||(username!=null&&username.equals("ceshi1"))||(username!=null&&username.equals("RH00001"))){
if((firstDeparment!=null&&firstDeparment.equals("设备部"))||(firstDeparment!=null&&firstDeparment.equals("粤电服大亚湾项目部"))||(firstDeparment!=null&&firstDeparment.equals("生产科技部"))||(firstDeparment!=null&&firstDeparment.equals("承包商"))||(username!=null&&username.equals("371522"))||(username!=null&&username.equals("ceshi1"))||(username!=null&&username.equals("RH00002"))||(username!=null&&username.equals("RH00020"))){
Intent intentDj= new Intent(mActivity, SbxdjglActivity.class);
intentDj.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
intentDj.putExtra("edit",false);
@ -477,7 +438,11 @@ public class HomeFragment extends BaseFragment {
// intentBlueTestInfo.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
// startActivity(intentBlueTestInfo);
Intent intentBlueToothDevice = new Intent(mActivity, BlueToothDeviceActivity.class);
// Intent intentBlueToothDevice = new Intent(mActivity, BlueToothDeviceActivity.class);
// intentBlueToothDevice.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
// startActivity(intentBlueToothDevice);
Intent intentBlueToothDevice = new Intent(mActivity, BlueToothDeviceSdkActivity.class);
intentBlueToothDevice.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intentBlueToothDevice);
@ -539,12 +504,12 @@ public class HomeFragment extends BaseFragment {
// String[] titles = {"管控一体化","两票系统", "SIS系统", "机组参数","巡检","点检","运行日志","kks码查询","应急预案","用车","用印","BI"};
// int[] imgIds = {R.drawable.icon_runlog_home,R.drawable.icon_liangpiao, R.drawable.icon_sis_new, R.drawable.icon_gcjd_new,R.drawable.xjgz,R.drawable.icon_dianjian,R.drawable.icon_runlog_home,R.drawable.icon_kks,R.drawable.icon_yjya,R.drawable.icon_use_car,R.drawable.icon_use_seal,R.drawable.icon_bi};
String[] titles = {"管控一体化","两票系统", "SIS系统", "机组参数","巡检","点检","kks码查询","应急预案","BI","高风险","人脸识别","隐患随手拍","每日作业管控风险清单","蓝牙传感器"};
String[] titles = {"管控一体化","两票系统", "SIS系统", "机组参数","巡检","点检","kks码查询","应急预案","BI","高风险","人脸识别","隐患随手拍","每日作业管控风险清单"};
int[] imgIds = {R.drawable.icon_gkyth,R.drawable.icon_lpqt, R.drawable.icon_sis_new_first,
R.drawable.icon_jzcs_new,R.drawable.icon_qj_new,R.drawable.icon_dj_new,
R.drawable.icon_kks_search,R.drawable.icon_yjya,R.drawable.icon_bi,
R.drawable.icon_high_risk,R.drawable.icon_face_re,R.drawable.icon_rinhuan_take,
R.drawable.icon_zyfx,R.drawable.icon_rinhuan_take};
R.drawable.icon_zyfx};
@ -701,6 +666,8 @@ public class HomeFragment extends BaseFragment {
//String url = "http://192.168.2.115:8601/"+Contans.FACE_RECOGNITION_APP_URL;
String url = Contans.IP+Contans.FACE_RECOGNITION_APP_URL;
showLog("人脸识别:");
showLog(url);
Request<String> request = NoHttp.createStringRequest(url, RequestMethod.POST);
// showLog(json);
// request.setDefineRequestBodyForJson(json);

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.7 KiB

@ -11,46 +11,80 @@
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="80dp"
android:minHeight="30dp"
android:layout_margin="10dp"
android:orientation="vertical">
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:id="@+id/zt_data"
android:layout_width="match_parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center|left"
android:text="蓝牙状态:"
android:gravity="center|start"
android:text="蓝牙传感器状态:"
android:textColor="#000000"
android:textSize="18sp" />
<TextView
android:layout_width="match_parent"
android:id="@+id/ble_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center|left"
android:text="点巡检仪上蓝灯亮起则为连接成功"
android:textColor="#000080"
android:textSize="12sp" />
android:gravity="center|start"
android:text="未连接"
android:layout_marginStart="10dp"
android:textColor="#ff0000"
android:textSize="15sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="30dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:id="@+id/tx_data"
android:layout_width="match_parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_weight="1"
android:gravity="center|left"
android:text="接收数值:"
android:gravity="center|start"
android:textColor="#000000"
android:text="蓝牙传感器电量:"
android:textSize="18sp" />
<TextView
android:id="@+id/ble_dianlang"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center|start"
android:text="0 %"
android:layout_marginStart="10dp"
android:textColor="#000000"
android:textSize="18sp" />
</LinearLayout>
<ListView
android:id="@+id/lv"
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:divider="@null"
android:dividerHeight="10dp" />
android:layout_height="0dp"
android:layout_weight="9">
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:divider="@null"
android:dividerHeight="10dp" />
<TextView
android:id="@+id/tv_nodata"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="50dp"
android:layout_marginEnd="50dp"
android:layout_gravity="center_vertical"
android:gravity="center"
android:text="没有发现蓝牙传感器设备,请重新扫描" />
</FrameLayout>
</LinearLayout>

@ -0,0 +1,73 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/layout_base" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="30dp"
android:layout_margin="10dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center|start"
android:text="蓝牙传感器状态:"
android:textColor="#000000"
android:textSize="18sp" />
<TextView
android:id="@+id/ble_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center|start"
android:text="未连接"
android:layout_marginStart="10dp"
android:textColor="#ff0000"
android:textSize="15sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="30dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center|start"
android:textColor="#000000"
android:text="蓝牙传感器电量:"
android:textSize="18sp" />
<TextView
android:id="@+id/ble_dianlang"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center|start"
android:text="0 %"
android:layout_marginStart="10dp"
android:textColor="#000000"
android:textSize="18sp" />
</LinearLayout>
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:divider="@null"
android:dividerHeight="10dp" />
</LinearLayout>

@ -8,46 +8,80 @@
<LinearLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_height="wrap_content"
android:minHeight="30dp"
android:layout_margin="10dp"
android:orientation="vertical">
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:id="@+id/zt_data"
android:layout_weight="1"
android:textSize="18sp"
android:gravity="center|left"
android:layout_width="match_parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="蓝牙状态:" />
android:gravity="center|start"
android:text="蓝牙传感器状态:"
android:textColor="#000000"
android:textSize="18sp" />
<TextView
android:layout_weight="1"
android:textSize="12sp"
android:gravity="center|left"
android:layout_width="match_parent"
android:id="@+id/ble_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000080"
android:text="点巡检仪上蓝灯亮起则为连接成功" />
android:gravity="center|start"
android:text="未连接"
android:layout_marginStart="10dp"
android:textColor="#ff0000"
android:textSize="15sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="30dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:layout_marginTop="5dp"
android:id="@+id/tx_data"
android:layout_weight="1"
android:textSize="18sp"
android:gravity="center|left"
android:layout_width="match_parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="接收数值:" />
android:gravity="center|start"
android:textColor="#000000"
android:text="蓝牙传感器电量:"
android:textSize="18sp" />
<TextView
android:id="@+id/ble_dianlang"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center|start"
android:text="0 %"
android:layout_marginStart="10dp"
android:textColor="#000000"
android:textSize="18sp" />
</LinearLayout>
<ListView
android:id="@+id/lv"
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:divider="@null"
android:dividerHeight="10dp" />
android:layout_height="match_parent">
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:divider="@null"
android:dividerHeight="10dp" />
<TextView
android:id="@+id/tv_nodata"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="50dp"
android:layout_marginEnd="50dp"
android:layout_gravity="center_vertical"
android:gravity="center"
android:text="没有发现蓝牙传感器设备,请重新扫描" />
</FrameLayout>
</LinearLayout>

@ -12,7 +12,7 @@
android:layout_height="200px"
android:gravity="center"
android:text="是否审核"
android:textColor="#000"
android:textColor="#000000"
android:textSize="20sp"
android:textStyle="bold" />

@ -307,25 +307,39 @@
android:id="@+id/ll_et_jg"
android:layout_width="match_parent"
android:layout_height="80px"
android:visibility="gone"
android:visibility="visible"
android:orientation="horizontal">
<EditText
android:id="@+id/et_jg"
style="@style/editTextTheme"
android:layout_height="70px"
android:layout_margin="0dp"
android:layout_width="250px"
android:layout_width="180px"
android:layout_gravity="center_vertical"
android:inputType="numberSigned|numberDecimal"/>
<TextView
android:id="@+id/tv_dw"
android:layout_width="150px"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:layout_marginStart="5dp"
android:text="MM/S"
android:textSize="@dimen/sbtextSize" />
android:textSize="13sp" />
<Button
android:id="@+id/btn_cj"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_gravity="center_vertical"
android:layout_marginStart="5dp"
android:background="@drawable/border_line_while"
android:minWidth="80dp"
android:minHeight="70px"
android:text="采集"
android:textColor="@color/colorPrimary"
android:textSize="14sp"
android:visibility="visible" />
</LinearLayout>
<Button
android:id="@+id/btn_selectCheckResult"
@ -339,7 +353,7 @@
android:text="点击选择采集结果"
android:textColor="@color/colorPrimary"
android:textSize="15sp"
android:visibility="visible"/>
android:visibility="gone"/>
</FrameLayout>
</LinearLayout>

@ -84,7 +84,7 @@
android:layout_height="wrap_content"
android:layout_weight="1"
android:baselineAligned="false"
android:gravity="center_vertical"
android:gravity="center_vertical|end"
android:minHeight="30dp"
android:orientation="horizontal">
<FrameLayout
@ -95,37 +95,24 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center_horizontal"
android:padding="3dp"
android:background="@drawable/btn_selector_bt"
android:textColor="#ffffff"
android:text="蓝牙绑定"
android:textSize="16sp" />
<TextView
android:id="@+id/device_de_bind"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="3dp"
android:text="蓝牙解绑"
android:background="@drawable/btn_selector"
android:textColor="#ffffff"
android:visibility="gone"
android:textSize="16sp" />
android:textSize="15sp" />
</FrameLayout>
<TextView
android:id="@+id/send_data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginStart="10dp"
android:padding="3dp"
android:gravity="center|left"
android:gravity="center|end"
android:background="@drawable/btn_selector"
android:textColor="#ffffff"
android:visibility="gone"
android:text="数据采集"
android:textSize="15sp" />
</LinearLayout>
</LinearLayout>

@ -12,8 +12,8 @@
<string name="last">上一条</string>
<string name="next">下一条</string>
<string name="data_error">数据出错</string>
<string name="ble_not_supported">BLE is not supported</string>
<string name="error_bluetooth_not_supported">Bluetooth not supported.</string>
<string name="ble_not_supported">当前设备不支持低功耗蓝牙</string>
<string name="error_bluetooth_not_supported">当前设备不支持低功耗蓝牙或未开启蓝牙功能</string>
<string name="unknown_device">不能识别的设备</string>
<string name="check_content_temp">检查内容[测温]:</string>
<string name="check_content_vibration">检查内容[测振]:</string>

@ -1,6 +1,6 @@
[versions]
agentwebCore = "v5.1.1-androidx"
agp = "8.10.0"
agp = "8.10.1"
androidxLocalbroadcastmanager = "1.1.0"
annotationVersion = "1.9.1"
appupdate = "4.2.6"

@ -4,4 +4,4 @@ distributionPath=wrapper/dists
#distributionUrl=https\://services.gradle.org/distributions/gradle-8.11.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.14-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.14.3-bin.zip
Loading…
Cancel
Save