移除蓝牙解绑

master
wenfei 2 months ago
parent 5c4d1b18d5
commit 834d9d8042

@ -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";

@ -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("当前设备不支持低功耗蓝牙,无法连接传感器采集数据");
}
@ -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<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("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)+"℃");
}
}
}
if("0F3101".equals(cmd)){
ble.write("VR", false);
cmd="VR";
}
}
}
});
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<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 +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<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);
}
}

@ -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<BluetoothDevice> 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 {

@ -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;

@ -11,41 +11,63 @@
<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: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:layout_weight="1"
android:gravity="center|left"
android:text="点巡检仪上蓝灯亮起则为连接成功"
android:textColor="#000080"
android:textSize="12sp" />
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>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="9">
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
@ -53,4 +75,16 @@
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>

@ -100,32 +100,18 @@
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" />
</FrameLayout>
<TextView
android:id="@+id/send_data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
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>

Loading…
Cancel
Save