diff --git a/app/src/main/java/com/rehome/dywoa/Contans.java b/app/src/main/java/com/rehome/dywoa/Contans.java index 038f0be..807e4d3 100755 --- a/app/src/main/java/com/rehome/dywoa/Contans.java +++ b/app/src/main/java/com/rehome/dywoa/Contans.java @@ -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"; diff --git a/app/src/main/java/com/rehome/dywoa/bleUtil/BlueToothDeviceActivity.java b/app/src/main/java/com/rehome/dywoa/bleUtil/BlueToothDeviceActivity.java index 3cd8161..c639eed 100644 --- a/app/src/main/java/com/rehome/dywoa/bleUtil/BlueToothDeviceActivity.java +++ b/app/src/main/java/com/rehome/dywoa/bleUtil/BlueToothDeviceActivity.java @@ -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 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("当前设备不支持低功耗蓝牙,无法连接传感器采集数据"); } @@ -119,12 +140,16 @@ public class BlueToothDeviceActivity extends BaseActivity { // 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)); + //showLog("扫描发现设备" + device.getName() + "," + device.getAddress()); + //String deviceName = device.getName(); + 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("连接"); @@ -132,47 +157,69 @@ public class BlueToothDeviceActivity extends BaseActivity { 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 listDianLiang = stringToListBlank(resultReceive); + if(!listDianLiang.isEmpty()){ + showLog(listDianLiang.get(0)); + showLog(listDianLiang.get(1)); + List 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 listDianLiang = stringToListBlank(resultReceive); + if(!listDianLiang.isEmpty()){ + showLog(listDianLiang.get(0)); + showLog(listDianLiang.get(1)); + List 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(); - } + blueScan(); } }); //BleManager.getInstance().init(getApplication()); @@ -199,11 +246,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 +256,7 @@ public class BlueToothDeviceActivity extends BaseActivity { //ble.write("BV", false); //温度测量 ble.write("0B0B6300", true); + cmd="0B0B6300"; //距离测量 //ble.write("4453", true); //转速测量 @@ -231,16 +274,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.disconnect(); + ble.free(); // if(BleManager.getInstance().getScanSate()== BleScanState.STATE_SCANNING){ // BleManager.getInstance().cancelScan(); // } @@ -432,23 +485,20 @@ public class BlueToothDeviceActivity extends BaseActivity { TextView deviceName; TextView deviceStatus; TextView deviceBind; - TextView deviceDeBind; TextView sendData; } private ArrayList 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(); mInflator = BlueToothDeviceActivity.this.getLayoutInflater(); this.callBackBlueToothBind=callBackBlueToothBind; - this.callBackBlueToothUnBind=callBackBlueToothUnBind; this.callBackBlueToothSendData=callBackBlueToothSendData; } @@ -456,10 +506,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 +548,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 +591,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 +611,12 @@ public class BlueToothDeviceActivity extends BaseActivity { private int selectItem = -1; } + private List stringToList(String strs) { + String str[] = strs.split(":"); + return Arrays.asList(str); + } + private List stringToListBlank(String strs) { + String str[] = strs.split("\n"); + return Arrays.asList(str); + } } \ No newline at end of file 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 index 2f587c1..d89c31a 100644 --- 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 @@ -79,7 +79,7 @@ public class BlueToothDeviceSdkActivity extends BaseActivity { @Override public int getContentViewID() { - return R.layout.activity_blue_tooth_device; + return R.layout.activity_blue_tooth_device_sdk; } @Override @@ -437,7 +437,6 @@ public class BlueToothDeviceSdkActivity extends BaseActivity { private ArrayList mLeDevices; private LayoutInflater mInflator; private LeDeviceListAdapter.CallBackBlueToothBind callBackBlueToothBind; - private LeDeviceListAdapter.CallBackBlueToothUnBind callBackBlueToothUnBind; private LeDeviceListAdapter.CallBackBlueToothSendData callBackBlueToothSendData; public LeDeviceListAdapter(LeDeviceListAdapter.CallBackBlueToothBind callBackBlueToothBind, @@ -453,10 +452,6 @@ public class BlueToothDeviceSdkActivity extends BaseActivity { void Click(int position); } - public interface CallBackBlueToothUnBind { - void Click(int position); - } - public interface CallBackBlueToothSendData { void Click(int position); } @@ -499,7 +494,6 @@ public class BlueToothDeviceSdkActivity 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 { diff --git a/app/src/main/java/com/rehome/dywoa/ui/activity/sbxj/SxgzActivity.java b/app/src/main/java/com/rehome/dywoa/ui/activity/sbxj/SxgzActivity.java index 06cfb36..dae4aa2 100755 --- a/app/src/main/java/com/rehome/dywoa/ui/activity/sbxj/SxgzActivity.java +++ b/app/src/main/java/com/rehome/dywoa/ui/activity/sbxj/SxgzActivity.java @@ -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(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; diff --git a/app/src/main/res/layout/activity_blue_tooth_device.xml b/app/src/main/res/layout/activity_blue_tooth_device.xml index 018f0da..2df3725 100644 --- a/app/src/main/res/layout/activity_blue_tooth_device.xml +++ b/app/src/main/res/layout/activity_blue_tooth_device.xml @@ -11,46 +11,80 @@ + android:gravity="center_vertical" + android:orientation="horizontal"> + android:gravity="center|start" + android:text="未连接" + android:layout_marginStart="10dp" + android:textColor="#ff0000" + android:textSize="15sp" /> + + + + - + android:layout_height="0dp" + android:layout_weight="9"> + + + + + diff --git a/app/src/main/res/layout/listitem_device.xml b/app/src/main/res/layout/listitem_device.xml index 97806ce..f19a911 100644 --- a/app/src/main/res/layout/listitem_device.xml +++ b/app/src/main/res/layout/listitem_device.xml @@ -100,32 +100,18 @@ android:textColor="#ffffff" android:text="蓝牙绑定" android:textSize="16sp" /> - - - -