扫描二维码,连接配网1
This commit is contained in:
@@ -1,7 +1,12 @@
|
||||
package com.ckkj.water.index;
|
||||
|
||||
import android.Manifest;
|
||||
import android.bluetooth.BluetoothAdapter;
|
||||
import android.bluetooth.BluetoothManager;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.text.TextUtils;
|
||||
@@ -9,15 +14,19 @@ import android.view.View;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.core.content.ContextCompat;
|
||||
|
||||
import com.ckkj.water.AppConfig;
|
||||
import com.ckkj.water.R;
|
||||
import com.ckkj.water.base.BaseFragment;
|
||||
import com.ckkj.water.custom.view.CustomRefreshHeader;
|
||||
import com.ckkj.water.databinding.IndexFragmentLayoutBinding;
|
||||
import com.ckkj.water.ConstantUtil;
|
||||
import com.ckkj.water.device.AddDeviceActivity;
|
||||
import com.ckkj.water.device.AddDeviceActivity_copy;
|
||||
import com.ckkj.water.device.DeviceControlActivity;
|
||||
import com.ckkj.water.device.DeviceReNameActivity;
|
||||
import com.ckkj.water.device.SelectWifiActivity;
|
||||
import com.ckkj.water.history.entity.WateringRecord;
|
||||
import com.ckkj.water.index.adapter.DeviceAdapter;
|
||||
import com.ckkj.water.index.entity.WaterDeviceEntity;
|
||||
@@ -49,6 +58,15 @@ public class IndexFragment extends BaseFragment<IndexFragmentLayoutBinding, Inde
|
||||
private List<WaterDeviceEntity> devList = new ArrayList<>();
|
||||
private int mCurrentPage = 1;
|
||||
|
||||
// 扫码待配网设备
|
||||
private WaterDeviceEntity pendingDeviceEntity = null;
|
||||
|
||||
// 蓝牙相关
|
||||
private static final int REQUEST_EVENT_CODE_BLUETOOTH_PERMISSION = 10002;
|
||||
private static final int REQUEST_ENABLE_BT = 10003;
|
||||
private BluetoothAdapter bluetoothAdapter;
|
||||
private boolean bluetoothReady = false;
|
||||
|
||||
@Override
|
||||
protected void initArgs(Bundle bundle) {
|
||||
}
|
||||
@@ -242,14 +260,119 @@ public class IndexFragment extends BaseFragment<IndexFragmentLayoutBinding, Inde
|
||||
@Override
|
||||
public void queryDeviceBindStatusSuccess(WaterDeviceEntity entity) {
|
||||
switch (entity.getBindDeviceStatus()){
|
||||
case 302:
|
||||
//Todo 即这是一台没有在mqtt中注册的设备,App 需要通过蓝牙连接,并实现配网
|
||||
//Todo entity 中有Mac地址,和设备名称,根据代码中现有的扫描、连接、配网代码,实现功能
|
||||
|
||||
case 200:
|
||||
//该设备目前已经绑定了当前用户,直接跳转到设备控制页面
|
||||
Bundle bundle = new Bundle();
|
||||
bundle.putSerializable("device_info",entity.getBindDevice());
|
||||
goTo(mContext, DeviceControlActivity.class,bundle);
|
||||
break;
|
||||
|
||||
case 302:
|
||||
// 这是一台没有在mqtt中注册的设备,需要通过蓝牙连接配网,(可理解为新设备,走蓝牙,配网)
|
||||
// 先检查蓝牙权限和开启状态
|
||||
pendingDeviceEntity = entity;
|
||||
checkBluetoothAndProceed();
|
||||
break;
|
||||
|
||||
case 304:
|
||||
//该设备目前在线,已经在mqtt注册,但是没有绑定用户,需要跳转到绑定页面
|
||||
Bundle bundleNoBind = new Bundle();
|
||||
bundleNoBind.putSerializable("ble_device_entity", entity.getBindDevice());
|
||||
goTo(mContext, DeviceReNameActivity.class, bundleNoBind);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查蓝牙权限和开启状态,然后跳转配网
|
||||
*/
|
||||
private void checkBluetoothAndProceed() {
|
||||
// 1. 检查蓝牙权限
|
||||
if (!checkBluetoothPermissions()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 2. 检查蓝牙是否开启
|
||||
if (!initBluetooth()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 3. 权限和状态都正常,跳转配网页面
|
||||
goToDeviceProvisioning();
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查蓝牙权限
|
||||
*/
|
||||
private boolean checkBluetoothPermissions() {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
|
||||
String[] permissions = {
|
||||
Manifest.permission.BLUETOOTH_SCAN,
|
||||
Manifest.permission.BLUETOOTH_CONNECT,
|
||||
Manifest.permission.ACCESS_FINE_LOCATION
|
||||
};
|
||||
if (!hasRecordPermission(permissions)) {
|
||||
getMorePermissionRequst(permissions,
|
||||
"蓝牙权限申请",
|
||||
"智能浇水需要您的授权,获取蓝牙权限添加设备",
|
||||
REQUEST_EVENT_CODE_BLUETOOTH_PERMISSION);
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
// Android 12 以下需要位置权限
|
||||
if (ContextCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION)
|
||||
!= PackageManager.PERMISSION_GRANTED) {
|
||||
requestPermissions(new String[]{
|
||||
Manifest.permission.ACCESS_FINE_LOCATION
|
||||
}, REQUEST_EVENT_CODE_BLUETOOTH_PERMISSION);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化蓝牙适配器并检查蓝牙是否开启
|
||||
* @return true 表示蓝牙已开启,false 表示未开启或不支持
|
||||
*/
|
||||
private boolean initBluetooth() {
|
||||
BluetoothManager bluetoothManager = (BluetoothManager) mContext.getSystemService(Context.BLUETOOTH_SERVICE);
|
||||
if (bluetoothManager != null) {
|
||||
bluetoothAdapter = bluetoothManager.getAdapter();
|
||||
}
|
||||
|
||||
if (bluetoothAdapter == null) {
|
||||
ToastUtil.showShort(mContext, "设备不支持蓝牙");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!bluetoothAdapter.isEnabled()) {
|
||||
// 蓝牙未开启,请求开启
|
||||
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
|
||||
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
|
||||
return false;
|
||||
}
|
||||
|
||||
bluetoothReady = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 跳转设备配网页面
|
||||
*/
|
||||
private void goToDeviceProvisioning() {
|
||||
if (pendingDeviceEntity == null) {
|
||||
ToastUtil.showShort(mContext, "设备信息异常");
|
||||
return;
|
||||
}
|
||||
Bundle bundle = new Bundle();
|
||||
bundle.putInt(ConstantUtil.ACTIVITY_NAVIGATE_FLAG, 100); // 100 = 来自扫码添加
|
||||
bundle.putSerializable("ble_device_entity", pendingDeviceEntity);
|
||||
goTo(mContext, SelectWifiActivity.class, bundle);
|
||||
pendingDeviceEntity = null;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onDestroyView() {
|
||||
@@ -270,13 +393,69 @@ public class IndexFragment extends BaseFragment<IndexFragmentLayoutBinding, Inde
|
||||
public void logOut(BaseBean bean) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {
|
||||
hidePermissionDialog();
|
||||
if (requestCode == REQUEST_EVENT_CODE_10001) {
|
||||
scanQrcode();
|
||||
} else if (requestCode == REQUEST_EVENT_CODE_BLUETOOTH_PERMISSION) {
|
||||
// 蓝牙权限获取成功,检查蓝牙状态
|
||||
if (initBluetooth()) {
|
||||
goToDeviceProvisioning();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {
|
||||
hidePermissionDialog();
|
||||
if (requestCode == REQUEST_EVENT_CODE_BLUETOOTH_PERMISSION) {
|
||||
ToastUtil.showShort(mContext, "需要蓝牙权限才能添加设备");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
||||
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
||||
EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
|
||||
hidePermissionDialog();
|
||||
|
||||
if (requestCode == REQUEST_EVENT_CODE_10001) {
|
||||
// 相机权限回调
|
||||
scanQrcode();
|
||||
} else if (requestCode == REQUEST_EVENT_CODE_BLUETOOTH_PERMISSION) {
|
||||
// 蓝牙权限回调
|
||||
boolean allGranted = true;
|
||||
for (int result : grantResults) {
|
||||
if (result != PackageManager.PERMISSION_GRANTED) {
|
||||
allGranted = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (allGranted) {
|
||||
// 权限获取成功,检查蓝牙状态
|
||||
if (initBluetooth()) {
|
||||
goToDeviceProvisioning();
|
||||
}
|
||||
} else {
|
||||
ToastUtil.showShort(mContext, "需要蓝牙权限才能添加设备");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||
super.onActivityResult(requestCode, resultCode, data);
|
||||
if (requestCode == REQUEST_ENABLE_BT) {
|
||||
if (resultCode == -1) { // RESULT_OK
|
||||
ToastUtil.showShort(mContext, "蓝牙已开启");
|
||||
bluetoothReady = true;
|
||||
if (pendingDeviceEntity != null) {
|
||||
goToDeviceProvisioning();
|
||||
}
|
||||
} else {
|
||||
ToastUtil.showShort(mContext, "请开启蓝牙以添加设备");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user