ble蓝牙连接优化
This commit is contained in:
parent
c0cd52b1e2
commit
2e693984bb
|
|
@ -191,17 +191,6 @@
|
|||
android:theme="@style/MyAppTheme"
|
||||
android:usesCleartextTraffic="true"
|
||||
tools:replace="android:icon,android:theme,android:allowBackup">
|
||||
<activity
|
||||
android:name=".BluetoothActivity"
|
||||
android:exported="false"
|
||||
android:launchMode="singleTop"
|
||||
android:theme="@style/Transparent" />
|
||||
|
||||
<activity
|
||||
android:name=".BlueTooth2Activity"
|
||||
android:exported="false"
|
||||
android:launchMode="singleTop"
|
||||
android:theme="@style/Transparent" />
|
||||
<activity
|
||||
android:name=".NewBindDeviceChoiceActivity"
|
||||
android:exported="false"
|
||||
|
|
@ -793,10 +782,7 @@
|
|||
android:name=".MainSix_4F_ControlActivity"
|
||||
android:launchMode="singleTop"
|
||||
android:theme="@style/Transparent" />
|
||||
<activity
|
||||
android:name=".MainSix_4F_BlueControlActivity"
|
||||
android:launchMode="singleTop"
|
||||
android:theme="@style/Transparent" />
|
||||
|
||||
<activity
|
||||
android:name=".MainFour_3F_ControlActivity"
|
||||
android:launchMode="singleTop"
|
||||
|
|
|
|||
|
|
@ -7,24 +7,16 @@
|
|||
*/
|
||||
package com.ifish.activity;
|
||||
|
||||
import android.Manifest;
|
||||
import android.app.AlertDialog;
|
||||
import android.bluetooth.BluetoothAdapter;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.widget.ImageView;
|
||||
|
||||
import androidx.core.app.ActivityCompat;
|
||||
|
||||
import com.bumptech.glide.Glide;
|
||||
import com.ifish.activity.newbind.NewBindDeviceActivity;
|
||||
import com.ifish.adapter.BlueToothController;
|
||||
import com.ifish.baseclass.BaseActivity;
|
||||
import com.ifish.permission.PermissionHelper;
|
||||
import com.ifish.utils.AnimationUtil;
|
||||
|
|
@ -34,7 +26,6 @@ import com.ifish.utils.ToastUtil;
|
|||
import com.ifish.utils.WifiAdmin;
|
||||
import com.ifish.view.DisplayUtil;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* @author Jacky dada
|
||||
|
|
@ -47,7 +38,6 @@ public class BindTwoDeviceActivity extends BaseActivity {
|
|||
private int width, height;
|
||||
private WifiAdmin wifiAdmin;
|
||||
private String wifissid = "";
|
||||
private BlueToothController blueToothController = null;
|
||||
private String[] perlist = null;
|
||||
private PermissionHelper permissionHelper;
|
||||
@Override
|
||||
|
|
@ -55,7 +45,6 @@ public class BindTwoDeviceActivity extends BaseActivity {
|
|||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.bindtwodevice_activity);
|
||||
initTitle("选择设备");
|
||||
blueToothController = new BlueToothController();
|
||||
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.S){
|
||||
perlist = new String[]{
|
||||
android.Manifest.permission.BLUETOOTH_SCAN,
|
||||
|
|
@ -281,10 +270,11 @@ public class BindTwoDeviceActivity extends BaseActivity {
|
|||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
dialog.dismiss();
|
||||
blueToothController.open(BindTwoDeviceActivity.this);
|
||||
AnimationUtil.finishAnimation(BindTwoDeviceActivity.this);
|
||||
}
|
||||
});
|
||||
dialog.show();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load Diff
|
|
@ -435,8 +435,8 @@ public class NewBindDeviceActivity extends AppCompatActivity {
|
|||
}else {
|
||||
AlertDialog.Builder dialog = new AlertDialog.Builder(NewBindDeviceActivity.this);
|
||||
dialog.setCancelable(false);
|
||||
dialog.setMessage("蓝牙没有打开,现在去申请打开蓝牙,请允许");
|
||||
dialog.setPositiveButton("我知道了", new DialogInterface.OnClickListener() {
|
||||
dialog.setMessage("蓝牙没有打开,现在去申请打开蓝牙,请允许后,在去确定连接");
|
||||
dialog.setPositiveButton("去申请", new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(
|
||||
DialogInterface dialog,
|
||||
|
|
@ -1371,4 +1371,10 @@ public class NewBindDeviceActivity extends AppCompatActivity {
|
|||
L.i("jjia--------没有连接的蓝牙---");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||
super.onActivityResult(requestCode, resultCode, data);
|
||||
L.i(requestCode+"jjia--------result---"+resultCode);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,49 +1,49 @@
|
|||
package com.ifish.adapter;
|
||||
|
||||
import android.Manifest;
|
||||
import android.annotation.SuppressLint;
|
||||
import android.app.Activity;
|
||||
import android.bluetooth.BluetoothAdapter;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
|
||||
import androidx.core.app.ActivityCompat;
|
||||
|
||||
import com.ifish.utils.ToastUtil;
|
||||
|
||||
public class BlueToothController {
|
||||
|
||||
BluetoothAdapter adapter;
|
||||
|
||||
public BlueToothController() {
|
||||
adapter = BluetoothAdapter.getDefaultAdapter();
|
||||
}
|
||||
|
||||
public boolean isOpen() {
|
||||
if (adapter != null) {
|
||||
return adapter.isEnabled();
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@SuppressLint("MissingPermission")
|
||||
public void open(Activity activity) {
|
||||
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
|
||||
// Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
|
||||
activity.startActivityForResult(intent, 11);
|
||||
}
|
||||
|
||||
@SuppressLint("MissingPermission")
|
||||
public boolean find(){
|
||||
if (adapter!=null){
|
||||
return adapter.startDiscovery();
|
||||
}else {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
//package com.ifish.adapter;
|
||||
//
|
||||
//import android.Manifest;
|
||||
//import android.annotation.SuppressLint;
|
||||
//import android.app.Activity;
|
||||
//import android.bluetooth.BluetoothAdapter;
|
||||
//import android.content.Intent;
|
||||
//import android.content.pm.PackageManager;
|
||||
//
|
||||
//import androidx.core.app.ActivityCompat;
|
||||
//
|
||||
//import com.ifish.utils.ToastUtil;
|
||||
//
|
||||
//public class BlueToothController {
|
||||
//
|
||||
// BluetoothAdapter adapter;
|
||||
//
|
||||
// public BlueToothController() {
|
||||
// adapter = BluetoothAdapter.getDefaultAdapter();
|
||||
// }
|
||||
//
|
||||
// public boolean isOpen() {
|
||||
// if (adapter != null) {
|
||||
// return adapter.isEnabled();
|
||||
// } else {
|
||||
// return false;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
//
|
||||
// @SuppressLint("MissingPermission")
|
||||
// public void open(Activity activity) {
|
||||
// Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
|
||||
//// Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
|
||||
// activity.startActivityForResult(intent, 11);
|
||||
// }
|
||||
//
|
||||
// @SuppressLint("MissingPermission")
|
||||
// public boolean find(){
|
||||
// if (adapter!=null){
|
||||
// return adapter.startDiscovery();
|
||||
// }else {
|
||||
// return false;
|
||||
// }
|
||||
//
|
||||
//
|
||||
// }
|
||||
//
|
||||
//}
|
||||
|
|
|
|||
|
|
@ -65,7 +65,6 @@ import com.ifish.activity.BindTwoDeviceActivity;
|
|||
import com.ifish.activity.BlackListActivity;
|
||||
import com.ifish.activity.DeviceInstructionActivity;
|
||||
import com.ifish.activity.InformationListActivity;
|
||||
import com.ifish.activity.LookFishMineActivity;
|
||||
import com.ifish.activity.MainEightControlActivity;
|
||||
import com.ifish.activity.MainFiveBDControlActivity;
|
||||
import com.ifish.activity.MainFiveControlActivity;
|
||||
|
|
@ -116,15 +115,9 @@ import com.ifish.geewe.GeeWeUser;
|
|||
import com.ifish.geewe.P2PListener;
|
||||
import com.ifish.geewe.SettingListener;
|
||||
import com.ifish.permission.PermissionHelper;
|
||||
import com.ifish.tcp.BackFunctionCode7_11;
|
||||
import com.ifish.tcp.ModelCodec;
|
||||
import com.ifish.tcp.OrderDeviceConnectModel;
|
||||
import com.ifish.tcp.OrderModel;
|
||||
import com.ifish.utils.ActivityManager;
|
||||
import com.ifish.utils.AnimationUtil;
|
||||
import com.ifish.utils.AppUtil;
|
||||
import com.ifish.utils.BLEManger;
|
||||
import com.ifish.utils.ByteUtil;
|
||||
import com.ifish.utils.Commons;
|
||||
import com.ifish.utils.Commons.LoginSPKey;
|
||||
import com.ifish.utils.Commons.NetWork;
|
||||
|
|
@ -137,7 +130,6 @@ import com.ifish.utils.SPUtil;
|
|||
import com.ifish.utils.ScreenUtil;
|
||||
import com.ifish.utils.ToastUtil;
|
||||
import com.ifish.utils.UnreadCount;
|
||||
import com.ifish.utils.WifiAdmin;
|
||||
import com.ifish.view.LevelDialog;
|
||||
import com.ifish.view.MyBGARefreshLayout;
|
||||
import com.ifish.view.MyListView;
|
||||
|
|
@ -219,10 +211,6 @@ public class DeviceFragment extends BaseV4Fragment implements ObservableScrollVi
|
|||
private PermissionHelper permissionHelper;
|
||||
|
||||
String[] permission;
|
||||
// String[] permissionM = new String[]{android.Manifest.permission.CAMERA, android.Manifest.permission.ACCESS_FINE_LOCATION, android.Manifest.permission.READ_EXTERNAL_STORAGE, WRITE_EXTERNAL_STORAGE, android.Manifest.permission.READ_PHONE_STATE};
|
||||
//
|
||||
// String[] permission13 = new String[]{android.Manifest.permission.CAMERA, android.Manifest.permission.ACCESS_FINE_LOCATION, android.Manifest.permission.READ_PHONE_STATE};
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
|
|
@ -1904,188 +1892,180 @@ public class DeviceFragment extends BaseV4Fragment implements ObservableScrollVi
|
|||
public static final String WRITE_UUID = "0000ff01-0000-1000-8000-00805f9b34fb"; //写特征
|
||||
String device_mac=null;
|
||||
|
||||
private void sendBleData(){
|
||||
// private void sendBleData(){
|
||||
//
|
||||
//// String ssid = tvWifiName.getText().toString();
|
||||
//// String pwd = tvWifiPwd.getText().toString();
|
||||
//
|
||||
// WifiAdmin wifiAdmin = new WifiAdmin(requireContext());
|
||||
// String wifissid = wifiAdmin.getSSID();
|
||||
// L.i("jjia 获取当前连接的wifi:" + wifissid);
|
||||
// wifissid = wifiAdmin.updateSsid(wifissid);
|
||||
// String wifiname = wifissid;
|
||||
// if (TextUtils.isEmpty(wifissid)) {
|
||||
// wifiname = "";
|
||||
// } else {
|
||||
// if ("0x".equals(wifissid)) {//上次连接过wifi,但是中途断开,则会出现0x
|
||||
// wifiname = "";
|
||||
// }
|
||||
// }
|
||||
//// tvWifiName.setText(wifiname);
|
||||
// L.i("jjia----------------wifiname="+wifiname);
|
||||
// String pwd = null;
|
||||
// if (!TextUtils.isEmpty(wifiname)) {
|
||||
//// String pwd = KVUtil.getString(wifiname, "");
|
||||
// pwd = sp.getString(wifiname, "");
|
||||
//
|
||||
// }
|
||||
// L.i(wifiname+"jjia---------name--1-"+pwd);
|
||||
//
|
||||
//
|
||||
//
|
||||
// OrderDeviceConnectModel model = OrderModel.OrderDeviceConnectModel(wifiname, pwd);//设置正式环境域名
|
||||
// byte[] data = ModelCodec.enCode(model);
|
||||
// BleManager.getInstance().write(currentDevice, SERVICE_UUID, WRITE_UUID, data,false, new BleWriteCallback() {
|
||||
// @Override
|
||||
// public void onWriteSuccess(int current, int total, byte[] justWrite) {
|
||||
// L.i("jjia-------------ssid 发送");
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public void onWriteFailure(BleException exception) {
|
||||
//
|
||||
// }
|
||||
// });
|
||||
// }
|
||||
|
||||
// String ssid = tvWifiName.getText().toString();
|
||||
// String pwd = tvWifiPwd.getText().toString();
|
||||
|
||||
WifiAdmin wifiAdmin = new WifiAdmin(requireContext());
|
||||
String wifissid = wifiAdmin.getSSID();
|
||||
L.i("jjia 获取当前连接的wifi:" + wifissid);
|
||||
wifissid = wifiAdmin.updateSsid(wifissid);
|
||||
String wifiname = wifissid;
|
||||
if (TextUtils.isEmpty(wifissid)) {
|
||||
wifiname = "";
|
||||
} else {
|
||||
if ("0x".equals(wifissid)) {//上次连接过wifi,但是中途断开,则会出现0x
|
||||
wifiname = "";
|
||||
}
|
||||
}
|
||||
// tvWifiName.setText(wifiname);
|
||||
L.i("jjia----------------wifiname="+wifiname);
|
||||
String pwd = null;
|
||||
if (!TextUtils.isEmpty(wifiname)) {
|
||||
// String pwd = KVUtil.getString(wifiname, "");
|
||||
pwd = sp.getString(wifiname, "");
|
||||
|
||||
}
|
||||
L.i(wifiname+"jjia---------name--1-"+pwd);
|
||||
|
||||
|
||||
|
||||
OrderDeviceConnectModel model = OrderModel.OrderDeviceConnectModel(wifiname, pwd);//设置正式环境域名
|
||||
byte[] data = ModelCodec.enCode(model);
|
||||
BleManager.getInstance().write(currentDevice, SERVICE_UUID, WRITE_UUID, data,false, new BleWriteCallback() {
|
||||
@Override
|
||||
public void onWriteSuccess(int current, int total, byte[] justWrite) {
|
||||
L.i("jjia-------------ssid 发送");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onWriteFailure(BleException exception) {
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void checkoutMac(String mac){
|
||||
|
||||
if (!TextUtils.isEmpty(mac)){
|
||||
boolean isSameName = false;
|
||||
devicePosition = 0;
|
||||
try {
|
||||
if (Commons.DEVICE != null) {
|
||||
if (Commons.DEVICE.size() != 0) {
|
||||
for (int i = 0; i < Commons.DEVICE.size(); i++) {
|
||||
if (mac.equals(Commons.DEVICE.get(i).getMacAddress())) {
|
||||
devicePosition = i;
|
||||
isSameName = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
if (!isSameName){
|
||||
L.i("jjia-------------不包含");
|
||||
requireActivity().runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
// 检测页面
|
||||
AlertDialog.Builder dialog = new AlertDialog.Builder(requireContext(), AlertDialog.THEME_HOLO_LIGHT);
|
||||
dialog.setCancelable(true);
|
||||
dialog.setTitle("提示");
|
||||
dialog.setMessage("发现一个新设备是否去添加");
|
||||
dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface arg0, int arg1) {
|
||||
arg0.dismiss();
|
||||
}
|
||||
});
|
||||
dialog.setPositiveButton("确定", (dialog1, which) -> {
|
||||
dialog1.dismiss();
|
||||
Intent intent = new Intent();
|
||||
intent.setClass(requireActivity(), NewBindDeviceActivity.class);
|
||||
intent.putExtra("title", "连接水族箱");
|
||||
intent.putExtra("type", "bluetooth");
|
||||
intent.putExtra("device", currentDevice);
|
||||
startActivity(intent);
|
||||
AnimationUtil.finishAnimation(requireActivity());
|
||||
});
|
||||
dialog.show();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
private void openNotify(){
|
||||
BleManager.getInstance().notify(currentDevice, SERVICE_UUID, WRITE_UUID, new BleNotifyCallback() {
|
||||
@Override
|
||||
public void onNotifySuccess() {
|
||||
sendBleData();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNotifyFailure(BleException exception) {
|
||||
dismissProgressDialogCancelble();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCharacteristicChanged(byte[] data) {
|
||||
|
||||
if (data!=null && data.length>0){
|
||||
int check_code = data[1];
|
||||
int length = data[14];
|
||||
byte[] dataByte = new byte[length];
|
||||
for (int i = 0; i < dataByte.length; i++) {
|
||||
dataByte[i] = data[i];
|
||||
}
|
||||
|
||||
Object object = ModelCodec.deCode(dataByte);
|
||||
if (object!=null && object instanceof BackFunctionCode7_11){
|
||||
BackFunctionCode7_11 model = (BackFunctionCode7_11) object;
|
||||
device_mac = ByteUtil.bytesToHexString(model.getSrc());
|
||||
checkoutMac(device_mac);
|
||||
|
||||
|
||||
} else {
|
||||
}
|
||||
L.i("jjia-------device_mac-"+device_mac);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
private void connectBle(){
|
||||
BleManager.getInstance().connect(currentDevice, new BleGattCallback() {
|
||||
@Override
|
||||
public void onStartConnect() {
|
||||
|
||||
L.i("jjia---------conne------");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConnectFail(BleDevice bleDevice, BleException exception) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConnectSuccess(BleDevice bleDevice, BluetoothGatt gatt, int status) {
|
||||
// ToastUtil.show(NewBindDeviceActivity.this,"蓝牙连接成功");
|
||||
BleManager.getInstance().setMtu(bleDevice, 512, new BleMtuChangedCallback() {
|
||||
@Override
|
||||
public void onSetMTUFailure(BleException exception) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMtuChanged(int mtu) {
|
||||
openNotify();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisConnected(boolean isActiveDisConnected, BleDevice device, BluetoothGatt gatt, int status) {
|
||||
}
|
||||
});
|
||||
}
|
||||
// private void checkoutMac(String mac){
|
||||
//
|
||||
// if (!TextUtils.isEmpty(mac)){
|
||||
// boolean isSameName = false;
|
||||
// devicePosition = 0;
|
||||
// try {
|
||||
// if (Commons.DEVICE != null) {
|
||||
// if (Commons.DEVICE.size() != 0) {
|
||||
// for (int i = 0; i < Commons.DEVICE.size(); i++) {
|
||||
// if (mac.equals(Commons.DEVICE.get(i).getMacAddress())) {
|
||||
// devicePosition = i;
|
||||
// isSameName = true;
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// } catch (Exception e) {
|
||||
// e.printStackTrace();
|
||||
// }
|
||||
//
|
||||
//
|
||||
// if (!isSameName){
|
||||
// L.i("jjia-------------不包含");
|
||||
// requireActivity().runOnUiThread(new Runnable() {
|
||||
// @Override
|
||||
// public void run() {
|
||||
// // 检测页面
|
||||
// AlertDialog.Builder dialog = new AlertDialog.Builder(requireContext(), AlertDialog.THEME_HOLO_LIGHT);
|
||||
// dialog.setCancelable(true);
|
||||
// dialog.setTitle("提示");
|
||||
// dialog.setMessage("发现一个新设备是否去添加");
|
||||
// dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
|
||||
// @Override
|
||||
// public void onClick(DialogInterface arg0, int arg1) {
|
||||
// arg0.dismiss();
|
||||
// }
|
||||
// });
|
||||
// dialog.setPositiveButton("确定", (dialog1, which) -> {
|
||||
// dialog1.dismiss();
|
||||
// Intent intent = new Intent();
|
||||
// intent.setClass(requireActivity(), NewBindDeviceActivity.class);
|
||||
// intent.putExtra("title", "连接水族箱");
|
||||
// intent.putExtra("type", "bluetooth");
|
||||
// intent.putExtra("device", currentDevice);
|
||||
// startActivity(intent);
|
||||
// AnimationUtil.finishAnimation(requireActivity());
|
||||
// });
|
||||
// dialog.show();
|
||||
// }
|
||||
// });
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// private void openNotify(){
|
||||
// BleManager.getInstance().notify(currentDevice, SERVICE_UUID, WRITE_UUID, new BleNotifyCallback() {
|
||||
// @Override
|
||||
// public void onNotifySuccess() {
|
||||
// sendBleData();
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public void onNotifyFailure(BleException exception) {
|
||||
// dismissProgressDialogCancelble();
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public void onCharacteristicChanged(byte[] data) {
|
||||
//
|
||||
// if (data!=null && data.length>0){
|
||||
// int check_code = data[1];
|
||||
// int length = data[14];
|
||||
// byte[] dataByte = new byte[length];
|
||||
// for (int i = 0; i < dataByte.length; i++) {
|
||||
// dataByte[i] = data[i];
|
||||
// }
|
||||
//
|
||||
// Object object = ModelCodec.deCode(dataByte);
|
||||
// if (object!=null && object instanceof BackFunctionCode7_11){
|
||||
// BackFunctionCode7_11 model = (BackFunctionCode7_11) object;
|
||||
// device_mac = ByteUtil.bytesToHexString(model.getSrc());
|
||||
// checkoutMac(device_mac);
|
||||
// } else {
|
||||
// }
|
||||
// L.i("jjia-------device_mac-"+device_mac);
|
||||
// }
|
||||
// }
|
||||
// });
|
||||
// }
|
||||
// private void connectBle(){
|
||||
// BleManager.getInstance().connect(currentDevice, new BleGattCallback() {
|
||||
// @Override
|
||||
// public void onStartConnect() {
|
||||
//
|
||||
// L.i("jjia---------conne------");
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public void onConnectFail(BleDevice bleDevice, BleException exception) {
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public void onConnectSuccess(BleDevice bleDevice, BluetoothGatt gatt, int status) {
|
||||
//// ToastUtil.show(NewBindDeviceActivity.this,"蓝牙连接成功");
|
||||
// BleManager.getInstance().setMtu(bleDevice, 512, new BleMtuChangedCallback() {
|
||||
// @Override
|
||||
// public void onSetMTUFailure(BleException exception) {
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public void onMtuChanged(int mtu) {
|
||||
// openNotify();
|
||||
// }
|
||||
// });
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public void onDisConnected(boolean isActiveDisConnected, BleDevice device, BluetoothGatt gatt, int status) {
|
||||
// }
|
||||
// });
|
||||
// }
|
||||
private void initBooth(){
|
||||
|
||||
boolean enable = BleManager.getInstance().isBlueEnable();
|
||||
if (enable) {
|
||||
// BLEManger.getInstance().startDiscovery(true);
|
||||
BleManager.getInstance().scan(new BleScanCallback() {
|
||||
@Override
|
||||
public void onScanFinished(List<BleDevice> scanResultList) {
|
||||
if (currentDevice!=null){ // 扫描结束,有设备
|
||||
L.i("jjia---------scan---end-2--");
|
||||
connectBle();
|
||||
}else {
|
||||
L.i("jjia---------scan---end--1-");
|
||||
showDevice();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -2110,34 +2090,8 @@ public class DeviceFragment extends BaseV4Fragment implements ObservableScrollVi
|
|||
|
||||
}
|
||||
BleDevice currentDevice;
|
||||
Boolean lifecycle = true;
|
||||
public void onEventMainThread(BluetoothDevice event) {
|
||||
|
||||
L.i("jjia---------home---scan---");
|
||||
if (BLEManger.getInstance().isHome && event!=null && lifecycle){ // 搜索到蓝牙了
|
||||
|
||||
// 检测页面
|
||||
AlertDialog.Builder dialog = new AlertDialog.Builder(requireContext(), AlertDialog.THEME_HOLO_LIGHT);
|
||||
dialog.setCancelable(true);
|
||||
dialog.setTitle("提示");
|
||||
dialog.setMessage("发现一个新设备是否去添加");
|
||||
dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface arg0, int arg1) {
|
||||
arg0.dismiss();
|
||||
}
|
||||
});
|
||||
dialog.setPositiveButton("确定", (dialog1, which) -> {
|
||||
dialog1.dismiss();
|
||||
Intent intent = new Intent();
|
||||
intent.setClass(requireActivity(), NewBindDeviceActivity.class);
|
||||
intent.putExtra("title", "连接水族箱");
|
||||
intent.putExtra("type", "bluetooth");
|
||||
startActivity(intent);
|
||||
AnimationUtil.finishAnimation(requireActivity());
|
||||
});
|
||||
dialog.show();
|
||||
}
|
||||
}
|
||||
|
||||
public void onEventMainThread(EventBean eventBean){
|
||||
|
|
@ -2146,9 +2100,28 @@ public class DeviceFragment extends BaseV4Fragment implements ObservableScrollVi
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPause() {
|
||||
super.onPause();
|
||||
lifecycle = false;
|
||||
public void showDevice(){
|
||||
// 检测页面
|
||||
AlertDialog.Builder dialog = new AlertDialog.Builder(requireContext(), AlertDialog.THEME_HOLO_LIGHT);
|
||||
dialog.setCancelable(true);
|
||||
dialog.setTitle("提示");
|
||||
dialog.setMessage("发现一个新设备是否去添加");
|
||||
dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface arg0, int arg1) {
|
||||
arg0.dismiss();
|
||||
}
|
||||
});
|
||||
dialog.setPositiveButton("确定", (dialog1, which) -> {
|
||||
dialog1.dismiss();
|
||||
Intent intent = new Intent();
|
||||
intent.setClass(requireActivity(), NewBindDeviceActivity.class);
|
||||
intent.putExtra("title", "连接水族箱");
|
||||
intent.putExtra("type", "bluetooth");
|
||||
intent.putExtra("device", currentDevice);
|
||||
startActivity(intent);
|
||||
AnimationUtil.finishAnimation(requireActivity());
|
||||
});
|
||||
dialog.show();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue