diff --git a/iFish7/src/main/AndroidManifest.xml b/iFish7/src/main/AndroidManifest.xml index 95f47394e..eba87c056 100644 --- a/iFish7/src/main/AndroidManifest.xml +++ b/iFish7/src/main/AndroidManifest.xml @@ -191,17 +191,6 @@ android:theme="@style/MyAppTheme" android:usesCleartextTraffic="true" tools:replace="android:icon,android:theme,android:allowBackup"> - - - - + = 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(); } + + } diff --git a/iFish7/src/main/java/com/ifish/activity/MainSix_4F_BlueControlActivity.java b/iFish7/src/main/java/com/ifish/activity/MainSix_4F_BlueControlActivity.java deleted file mode 100644 index ce7f220b0..000000000 --- a/iFish7/src/main/java/com/ifish/activity/MainSix_4F_BlueControlActivity.java +++ /dev/null @@ -1,1299 +0,0 @@ -package com.ifish.activity; - -import static com.ifish.activity.MainTabActivity.map; - -import android.app.AlertDialog; -import android.app.Dialog; -import android.bluetooth.BluetoothDevice; -import android.content.DialogInterface; -import android.content.Intent; -import android.os.Bundle; -import android.os.CountDownTimer; -import android.os.Handler; -import android.text.TextUtils; -import android.view.View; -import android.view.Window; -import android.widget.Button; -import android.widget.ImageView; -import android.widget.LinearLayout; -import android.widget.TextView; - -import com.bumptech.glide.Glide; -import com.google.gson.Gson; -import com.google.gson.reflect.TypeToken; -import com.ifish.basebean.BaseBean; -import com.ifish.basebean.DeleteDevice; -import com.ifish.basebean.Device; -import com.ifish.basebean.ErrorReceiveObj; -import com.ifish.basebean.ErrorReceiveToast; -import com.ifish.basebean.ErrorSendObj; -import com.ifish.basebean.ErrorSendTimeSetting; -import com.ifish.basebean.ErrorSendToast; -import com.ifish.basebean.JpushDeleteDeviceMac; -import com.ifish.basebean.NetworkChangBean; -import com.ifish.basebean.RenameDevice; -import com.ifish.basebean.SetDevice; -import com.ifish.basebean.ShareDevice; -import com.ifish.basebean.UnBindDevice; -import com.ifish.baseclass.BaseGradeActivity; -import com.ifish.tcp.BackInfoModelSeven_2F_TimeSet; -import com.ifish.tcp.BackInfoModelSix_4F; -import com.ifish.tcp.BackInfoModelSix_4F_FeedFish; -import com.ifish.tcp.FishFeedType; -import com.ifish.tcp.TcpReceiveThread; -import com.ifish.tcp.TcpSendThread; -import com.ifish.utils.AnimationUtil; -import com.ifish.utils.BLEManger; -import com.ifish.utils.Commons; -import com.ifish.utils.HttpListener; -import com.ifish.utils.HttpManager; -import com.ifish.utils.L; -import com.ifish.utils.SPUtil; -import com.ifish.utils.ToastUtil; -import com.ifish.view.IosAlertDialog; -import com.ifish.view.PopWindowDevice_EventBus; -import com.ifish.view.SelectorImageView; -import com.ifish.view.SwipeRefreshLayout; -import com.ifish.wheelview.adapter.ArrayWheelAdapter; -import com.ifish.wheelview.view.WheelView; -import com.squareup.picasso.Picasso; - -import java.net.Socket; -import java.util.ArrayList; -import java.util.List; - -import de.greenrobot.event.EventBus; - -/** - * @author:zau18 - * @date: 2022/5/17 10:02 下午 新增加的 - * @description: 绚多6控 4f,5f 设备界面 - */ -public class MainSix_4F_BlueControlActivity extends BaseGradeActivity implements SwipeRefreshLayout.OnRefreshListener { - private SPUtil sp; - private ImageView title_img_right; - private SwipeRefreshLayout sw; - private SelectorImageView iv_control_1, iv_control_2, iv_control_3, iv_control_4, iv_control_5, iv_control_6, iv_control_7, iv_control_8; - private TextView tv_control_1, tv_control_2, tv_control_3, tv_control_4, tv_control_5, tv_control_6, tv_control_7, tv_control_8; - private TextView tv_iconName_1, tv_iconName_2, tv_iconName_3, tv_iconName_4, tv_iconName_5, tv_iconName_6, tv_iconName_7, tv_iconName_8; - private static final String hot_text = "- 加热 -"; - private static final String cold_text = "- 室内温度 -"; - private static final String wendu_text = "℃"; - private boolean isToast = true; - private boolean rl_setting_clickble = true; - private boolean isFirstAPP = false; - private boolean DeviceOnLine = false; - private String mac = ""; - private TextView tv_wendu, tv_typename, tv_dianliang; - private LinearLayout ll_dianliang, ll_wendu; - private CountDownTimer RefreshTimer; - private CountDownTimer WifiChangeTimer; - private CountDownTimer TcpTimer; - private HttpManager hm = HttpManager.getInstance(); - private Boolean isWaterDialog = false; - private BackInfoModelSix_4F backInfoModelSix_4F; - private Dialog waterTempDialog; - private int position = 0; - private TextView tv_humidity; - private TextView tv_setfish; - private TextView tv_setdian; - private boolean isClick_FeedFish = false; - private TextView tv_heatWendu; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.main_six_4f_control_activity); - initView(); - initListener(); - init(); - LoginDevice(); - try { - initTitle(Commons.DEVICE.get(Commons.DevicePosition).getShowName()); - } catch (Exception e) { - e.printStackTrace(); - } - ImageView iv_logo = (ImageView) findViewById(R.id.iv_logo); - TextView tv_logo = (TextView) findViewById(R.id.tv_logo); - try { - tv_logo.setText(Commons.DEVICE.get(sp.getInt(Commons.LoginSPKey.Position, 0)).getVenderList().getBrandName()); - Picasso.with(MainSix_4F_BlueControlActivity.this).load(HttpManager.Store_URL + Commons.DEVICE.get(sp.getInt(Commons.LoginSPKey.Position, 0)).getVenderList().getLogo()).error(R.drawable.ifish_logo_default).into(iv_logo); - } catch (Exception e) { - e.printStackTrace(); - } - - } - - @Override - protected void onResume() { - super.onResume(); - try { - setTitle(Commons.DEVICE.get(Commons.DevicePosition).getShowName()); - } catch (Exception e1) { - Commons.DevicePosition = 0; - sp.putInt(Commons.LoginSPKey.Position, 0); - try { - setTitle(Commons.DEVICE.get(Commons.DevicePosition).getShowName()); - } catch (Exception e) { - e.printStackTrace(); - } - } - Commons.IS_EventBus = true; - if (isFirstAPP) { - if (DeviceOnLine) { - checkDevice(); - } else { - //设备已离线,登录设备 - LoginDevice(); - } - } - isFirstAPP = true; - getLocalIconName(); - } - - @Override - protected void onDestroy() { - isToast = false; - EventBus.getDefault().unregister(this); - closeSoket(); - stopAllTimer(); - super.onDestroy(); - } - - public void closeSoket() {//关闭soket - if (MainTabActivity.socket != null) { - try { - MainTabActivity.socket.close(); - } catch (Exception e) { - } - } - MainTabActivity.socket = null; - } - - private void stopAllTimer() { - StopWifiChangeTimer(); - stopRefreshTimer(); - stopTimer(); - } - - //发送查询设备温度 定时器的指令 - private void checkDevice() { - new Thread() { - @Override - public void run() { - super.run(); - map.put(Commons.FishKey.Key, Commons.FishKey.Query);//查询 - map.put(Commons.FishKey.Mac, mac); - TcpSendThread ts3 = new TcpSendThread(MainTabActivity.socket, map); - Thread thread3 = new Thread(ts3); - thread3.start(); - } - }.start(); - } - - /** - * 获取本地图标名字 - */ - public void getLocalIconName() { - String locatText = sp.getString(mac + Commons.LoginSPKey.ICONNAME); - //本地集合json字符串不为空才能进行转化 - if (!TextUtils.isEmpty(locatText)) { - List locatTextList = new ArrayList<>(); - try { - locatTextList = new Gson().fromJson(locatText, new TypeToken>() { - }.getType()); - if (locatTextList != null && locatTextList.size() == 8) { - if (!TextUtils.isEmpty(locatTextList.get(0))) { - tv_iconName_1.setText(locatTextList.get(0)); - } - if (!TextUtils.isEmpty(locatTextList.get(1))) { - tv_iconName_2.setText(locatTextList.get(1)); - } - if (!TextUtils.isEmpty(locatTextList.get(2))) { - tv_iconName_3.setText(locatTextList.get(2)); - } - if (!TextUtils.isEmpty(locatTextList.get(3))) { - tv_iconName_4.setText(locatTextList.get(3)); - } - if (!TextUtils.isEmpty(locatTextList.get(4))) { - tv_iconName_5.setText(locatTextList.get(4)); - } - if (!TextUtils.isEmpty(locatTextList.get(5))) { - tv_iconName_6.setText(locatTextList.get(5)); - } - if (!TextUtils.isEmpty(locatTextList.get(6))) { - tv_iconName_7.setText(locatTextList.get(6)); - } - if (!TextUtils.isEmpty(locatTextList.get(7))) { - tv_iconName_8.setText(locatTextList.get(7)); - } - } - } catch (Exception e) { - } - } - } - - private void initView() { - tv_setfish = findViewById(R.id.tv_setfish); - tv_setdian = findViewById(R.id.tv_setdian); - ImageView iv_bg1 = (ImageView) findViewById(R.id.iv_bg1); - ImageView iv_bg2 = (ImageView) findViewById(R.id.iv_bg2); - String type = Commons.DEVICE.get(Commons.DevicePosition).type; - if(Device.TYPE_5F.equals(type)){ //海水 - Glide.with(this).load(R.drawable.seven5f_bg1).into(iv_bg1); - Glide.with(this).load(R.drawable.seven5f_bg2).into(iv_bg2); - }else{ //淡水 - Glide.with(this).load(R.drawable.sevenbg1).into(iv_bg1); - Glide.with(this).load(R.drawable.sevenbg2).into(iv_bg2); - } - title_img_right = findMyViewById(R.id.title_img_right); - sw = findMyViewById(R.id.sw); - tv_offline = (TextView) findViewById(R.id.tv_offline); - tv_humidity = findMyViewById(R.id.tv_humidity); - iv_control_1 = findMyViewById(R.id.iv_control_1); - iv_control_2 = findMyViewById(R.id.iv_control_2); - iv_control_3 = findMyViewById(R.id.iv_control_3); - iv_control_4 = findMyViewById(R.id.iv_control_4); - iv_control_5 = findMyViewById(R.id.iv_control_5); - iv_control_6 = findMyViewById(R.id.iv_control_6); - iv_control_7 = findMyViewById(R.id.iv_control_7); - iv_control_8 = findMyViewById(R.id.iv_control_8); - tv_heatWendu = findViewById(R.id.tv_heatWendu); - tv_control_1 = findMyViewById(R.id.tv_control_1); - tv_control_2 = findMyViewById(R.id.tv_control_2); - tv_control_3 = findMyViewById(R.id.tv_control_3); - tv_control_4 = findMyViewById(R.id.tv_control_4); - tv_control_5 = findMyViewById(R.id.tv_control_5); - tv_control_6 = findMyViewById(R.id.tv_control_6); - tv_control_7 = findMyViewById(R.id.tv_control_7); - tv_control_8 = findMyViewById(R.id.tv_control_8); - tv_iconName_1 = findMyViewById(R.id.tv_iconName_1); - tv_iconName_2 = findMyViewById(R.id.tv_iconName_2); - tv_iconName_3 = findMyViewById(R.id.tv_iconName_3); - tv_iconName_4 = findMyViewById(R.id.tv_iconName_4); - tv_iconName_5 = findMyViewById(R.id.tv_iconName_5); - tv_iconName_6 = findMyViewById(R.id.tv_iconName_6); - tv_iconName_7 = findMyViewById(R.id.tv_iconName_7); - tv_iconName_8 = findMyViewById(R.id.tv_iconName_8); - tv_wendu = findMyViewById(R.id.tv_wendu); - tv_typename = findMyViewById(R.id.tv_typename); - tv_dianliang = findMyViewById(R.id.tv_dianliang); - ll_dianliang = findMyViewById(R.id.ll_dianliang); - ll_wendu = findMyViewById(R.id.ll_wendu); - ImageView iv_gif = findMyViewById(R.id.iv_gif); - Glide.with(MainSix_4F_BlueControlActivity.this).load(R.drawable.sevenloading).into(iv_gif); - ll_gif = findMyViewById(R.id.ll_gif); - - if(Device.TYPE_5F.equals(type)){ //海水 - iv_control_2.setSelectorDrawable(getDrawable(R.drawable.daifenqi_on)); - iv_control_2.setDefaultDrawable(getDrawable(R.drawable.daifenqi_off)); - tv_iconName_2.setText("蛋分器"); - }else{ //淡水 - iv_control_2.setSelectorDrawable(getDrawable(R.drawable.lamp0bon2)); - iv_control_2.setDefaultDrawable(getDrawable(R.drawable.lamp0boff2)); - tv_iconName_2.setText("增氧泵"); - } - } - - private void showgif() { - ll_gif.setVisibility(View.VISIBLE); - } - - private void hidegif() { - ll_gif.setVisibility(View.GONE); - } - - private LinearLayout ll_gif; - - private void initListener() { - tv_setfish.setOnClickListener(this); - tv_setdian.setOnClickListener(this); - title_img_right.setOnClickListener(this); - iv_control_1.setOnClickListener(this); - iv_control_2.setOnClickListener(this); - iv_control_3.setOnClickListener(this); - iv_control_4.setOnClickListener(this); - iv_control_5.setOnClickListener(this); - iv_control_6.setOnClickListener(this); - iv_control_7.setOnClickListener(this); - iv_control_8.setOnClickListener(this); -// findViewById(R.id.rl_typegone).setOnClickListener(this); - sw.setOnRefreshListener(this); - } - BluetoothDevice currentDevice; - private void init() { - EventBus.getDefault().register(this); - sp = SPUtil.getInstance(this); - isToast = true; - isFirstAPP = false; - try { - - currentDevice = BLEManger.getInstance().currentDevice; - if (currentDevice!=null){ - mac =currentDevice.getAddress(); - L.i("===============发现设备的mac" + mac); - if (!TextUtils.isEmpty(mac)){ - mac = mac.replace(":","").toLowerCase(); - } - } - } catch (Exception e) { - } - } - - //发送登录设备的指令 - private void LoginDevice() { - startRefreshTimer(); - new Thread() { - @Override - public void run() { - super.run(); - try { - if (MainTabActivity.socket == null) { - MainTabActivity.socket = new Socket(HttpManager.FISH_URL, HttpManager.FISH_URL_PORT); - L.i("===============新建的soket" + MainTabActivity.socket.getLocalPort()); - } - map.put(Commons.FishKey.Key, Commons.FishKey.Login);//登录 - map.put(Commons.FishKey.Mac, mac); - L.i("===============mac" + mac); - TcpReceiveThread tr = new TcpReceiveThread(MainTabActivity.socket); - Thread thread1 = new Thread(tr); - thread1.start(); - - TcpSendThread ts = new TcpSendThread(MainTabActivity.socket, map); - Thread thread2 = new Thread(ts); - thread2.start(); - } catch (Exception e) { - e.printStackTrace(); - } - } - }.start(); - } - - private void startRefreshTimer() { - if (RefreshTimer == null) { - RefreshTimer = new CountDownTimer(3500, 2000) { - @Override - public void onTick(long millisUntilFinished) { - } - - @Override - public void onFinish() { - rl_setting_clickble = true; - tv_wendu.setText(""); - sw.setRefreshing(false); - if (isToast) { - ToastUtil.showSpecial(MainSix_4F_BlueControlActivity.this, Commons.Text.Repost); - } - } - }; - } - RefreshTimer.start(); - } - - private void stopRefreshTimer() { - if (RefreshTimer != null) { - RefreshTimer.cancel(); - } - } - - private void StartWifiChangeTimer() { - if (WifiChangeTimer == null) { - WifiChangeTimer = new CountDownTimer(2500, 2000) { - @Override - public void onTick(long millisUntilFinished) { - } - - @Override - public void onFinish() { - LoginDevice(); - } - }; - } - WifiChangeTimer.start(); - } - - private void StopWifiChangeTimer() { - if (WifiChangeTimer != null) { - WifiChangeTimer.cancel(); - } - } - - private void startTimer() { - rl_setting_clickble = false; - if (TcpTimer == null) { - TcpTimer = new CountDownTimer(4000, 600) {//倒数4秒 没有反应则判断TCP指令失效 - @Override - public void onTick(long millisUntilFinished) { - } - - @Override - public void onFinish() {//倒计时结束 - resetIVFlag(); - isWaterDialog = false; - dismissProgressDialog(); - ToastUtil.showSpecial(MainSix_4F_BlueControlActivity.this, Commons.Text.Repost); - } - }; - } - TcpTimer.start(); - } - - //重置三个按钮所有状态 - private void resetIVFlag() { - rl_setting_clickble = true; - } - - private void stopTimer() { - if (TcpTimer != null) { - TcpTimer.cancel(); - } - } - - //设置温度校准返回 - public void onEventMainThread(BackInfoModelSeven_2F_TimeSet event){ - if (!DeviceOnLine) { - ToastUtil.show(MainSix_4F_BlueControlActivity.this, Commons.Text.OFFDevice); - return; - } - checkDevice(); //再次查询下温度 - } - - public void onEventMainThread(NetworkChangBean event) {//网络改变 - StartWifiChangeTimer(); - } - - public void onEventMainThread(ShareDevice event) {//分享鱼缸 - Intent i = new Intent(this, ShareActivity.class); - i.putExtra("deviceId", Commons.DEVICE.get(sp.getInt(Commons.LoginSPKey.Position, 0)).getDeviceId()); - startActivity(i); - AnimationUtil.startAnimation(this); - } - - public void onEventMainThread(SetDevice event) { - if (!DeviceOnLine) { - ToastUtil.show(MainSix_4F_BlueControlActivity.this, Commons.Text.OFFDevice); - return; - } - Intent i = new Intent(); - i.setClass(MainSix_4F_BlueControlActivity.this, TimeSettingSix_4F.class); - i.putExtra(TimeSettingSix_4F.BYTEOBJECT, backInfoModelSix_4F); - i.putExtra(TimeSettingSix_4F.MAC, mac); - startActivity(i); - AnimationUtil.startAnimation(MainSix_4F_BlueControlActivity.this); - } - - public void onEventMainThread(RenameDevice event) {//修改鱼缸名字 - Intent i = new Intent(this, ChangeDeviceName_2F_Activity.class); - i.putExtra("device", Commons.DEVICE.get(sp.getInt(Commons.LoginSPKey.Position, 0))); - i.putExtra("position", sp.getInt(Commons.LoginSPKey.Position, 0)); - startActivity(i); - AnimationUtil.startAnimation(this); - } - - public void onEventMainThread(DeleteDevice event) {//删除鱼缸 - delDevice(); - } - - //一键喂鱼查询返回 - public void onEventMainThread(BackInfoModelSix_4F_FeedFish event){ - if(isClick_FeedFish){ - isClick_FeedFish = false; - if(!event.isLight_select1() && !event.isLight_select2() && !event.isLight_select3() && !event.isLight_select4() && !event.isLight_select5() && !event.isLight_select6()){ //一个设备都没有关联 - //没有设置一键喂鱼,先去设置 - gotoSetFeedFishSetting(); - }else{ - //判断是否是立即开启还是关闭一键喂鱼 - showFeedFishStyleDialog(); - } - } - } - - private void gotoSetFeedFishSetting(){ - IosAlertDialog dialog = new IosAlertDialog(this).builder(); - dialog.setTitle("提示"); - dialog.setCancelable(true); - dialog.setMessage("是否需要设置喂鱼设置?"); - dialog.setNegativeButton("取消", new View.OnClickListener() { - @Override - public void onClick(View view) { - - } - }); - dialog.setPositiveButton("确认", new View.OnClickListener() { - @Override - public void onClick(View view) { - if (!DeviceOnLine) { - ToastUtil.show(MainSix_4F_BlueControlActivity.this, Commons.Text.OFFDevice); - return; - } - Intent intent1 = new Intent(MainSix_4F_BlueControlActivity.this,FeedFishSettingActivity.class); - intent1.putExtra(TimeSettingSix_4F.BYTEOBJECT,backInfoModelSix_4F); - intent1.putExtra(TimeSettingSix_4F.MAC, mac); - startActivity(intent1); - AnimationUtil.startAnimation(MainSix_4F_BlueControlActivity.this); - } - }); - dialog.show(); - } - - //是否立即开始喂鱼 - private void startFeedFishSoon(){ - IosAlertDialog dialog = new IosAlertDialog(this).builder(); - dialog.setTitle("提示"); - dialog.setCancelable(true); - dialog.setMessage("你确定要立即喂鱼吗?"); - dialog.setNegativeButton("取消", new View.OnClickListener() { - @Override - public void onClick(View view) { - - } - }); - dialog.setPositiveButton("确认", new View.OnClickListener() { - @Override - public void onClick(View view) { - openFishFeedingNow(FishFeedType.OPEN_FISH_FEED+"");//立即一键喂鱼 - new Handler().postDelayed(new Runnable() { - @Override - public void run() { - checkDevice();//查询下设置的状态 - } - },1000); - } - }); - dialog.show(); - } - - //接受6控查询返回的指令 - public void onEventMainThread(BackInfoModelSix_4F event) { - hidegif(); - dismissProgressDialog(); - dismissProgressDialogCancelble(); - tv_offline.setVisibility(View.GONE); - backInfoModelSix_4F = event; - DeviceOnLine = true; - resetIVFlag(); - stopAllTimer(); - rl_setting_clickble = true; - sw.setRefreshing(false); - tv_wendu.setText((float) event.getWendu() / 10 + ""); - if (isWaterDialog) { - if (waterTempDialog != null) { - waterTempDialog.dismiss(); - } - isWaterDialog = false; - } - if (waterTempDialog != null) { - if (waterTempDialog.isShowing()) { - if (iv_hot != null) { - if (Byte.toString(event.getOnoff7()).equals(Commons.FishKey.On)) { - iv_hot.toggle(true); - } else { - iv_hot.toggle(false); - } - } - } - } -// tv_dianliang.setText((float) event.getDianliang() / 10 + ""); - byte string1 = event.getOnoff1(); - byte string2 = event.getOnoff2(); - byte string3 = event.getOnoff3(); - byte string4 = event.getOnoff4(); - byte string5 = event.getOnoff5(); - byte string6 = event.getOnoff6(); - byte string7 = event.getHeatStatus(); - byte string8 = event.getSarkLamp(); - if (Byte.toString(string1).equals(Commons.FishKey.On)) { - iv_control_2.toggle(true); - } else { - iv_control_2.toggle(false); - } - - if (Byte.toString(string2).equals(Commons.FishKey.On)) { - iv_control_3.toggle(true); - } else { - iv_control_3.toggle(false); - } - if (Byte.toString(string3).equals(Commons.FishKey.On)) { - iv_control_4.toggle(true); - } else { - iv_control_4.toggle(false); - } - if (Byte.toString(string4).equals(Commons.FishKey.On)) { - iv_control_7.toggle(true); - } else { - iv_control_7.toggle(false); - } - if (Byte.toString(string5).equals(Commons.FishKey.On)) { - iv_control_1.toggle(true); - } else { - iv_control_1.toggle(false); - } - if (Byte.toString(string6).equals(Commons.FishKey.On)) { - iv_control_5.toggle(true); - } else { - iv_control_5.toggle(false); - } - if (Byte.toString(string7).equals(Commons.FishKey.On)) { - iv_control_8.toggle(true); - } else { - iv_control_8.toggle(false); - } - - short heatWendu = event.getHeatWendu(); - if(tv_heatWendu != null){ - if(heatWendu != 0){ - tv_heatWendu.setVisibility(View.VISIBLE); - tv_heatWendu.setText(event.getHeatWendu() / 10 + ""); - }else{ - tv_heatWendu.setVisibility(View.GONE); - } - } - - if (Byte.toString(string8).equals(Commons.FishKey.On)) { - iv_control_6.toggle(true); - } else { - iv_control_6.toggle(false); - } - String humidity = "0"; //水位报警提示 - if ("0".equals(humidity)) { - tv_humidity.setVisibility(View.GONE); - } else { - tv_humidity.setVisibility(View.VISIBLE); - } - //一键喂鱼是否开启 - byte feed_fish_s2m = event.getFeed_fish_s2m(); - if(Byte.toString(feed_fish_s2m).equals(Commons.FishKey.On)){ - tv_setfish.setBackground(getDrawable(R.drawable.select_bg)); - }else{ - tv_setfish.setBackground(getDrawable(R.drawable.select_un_bg)); - } - //电量功率 - if(event.getDianliang() != 0){ - tv_setdian.setVisibility(View.VISIBLE); - tv_setdian.setText((float) event.getDianliang() / 10 + "w"); - }else{ - tv_setdian.setVisibility(View.GONE); - } - } - - //接收到发送方出错的指令 - public void onEventMainThread(ErrorSendObj event) { - L.i("===========发送方异常============="); - errorDevice(); - } - - //接收到接受方出错的指令 - public void onEventMainThread(ErrorReceiveObj event) { - L.i("===========接收方异常============="); - errorDevice(); - } - - //接收到发送方出错的吐司 - public void onEventMainThread(ErrorSendToast event) { - errorDevice(); - } - - //接收到接受方出错的吐司 - public void onEventMainThread(ErrorReceiveToast event) { - errorDevice(); - } - - private void errorDevice() { - closeSoket(); - DeviceOnLine = false; - sw.setRefreshing(false); - Commons.IS_EventBus = true; - EventBus.getDefault().post(new ErrorSendTimeSetting());//提示设置界面已离线 - L.i("===========离线============="); - } - - //收到鱼缸解绑推送 - public void onEventMainThread(JpushDeleteDeviceMac event) { - if (mac.equals(event.mac)) { - ToastUtil.show(MainSix_4F_BlueControlActivity.this, "您已失去设备的控制权"); - finish(); - AnimationUtil.finishAnimation(this); - } - } - - //接收登录返回的指令 - public void onEventMainThread(Boolean event) { - if (event) { - checkDevice(); - DeviceOnLine = true; - } else { - stopRefreshTimer(); - OffLineDevice(); - } - isWaterDialog = false; - dismissProgressDialog(); - } - - private void OffLineDevice() {//设备离线的处理 - DeviceOnLine = false; - rl_setting_clickble = true; - sw.setRefreshing(false); - Commons.IS_EventBus = true; - EventBus.getDefault().post(new ErrorSendTimeSetting());//提示设置界面已离线 - tv_offline.setVisibility(View.VISIBLE); - } - - private TextView tv_offline; - - private void delDevice() { - for (int i = 0; i < Commons.DEVICE.size(); i++) { - if (mac.equals(Commons.DEVICE.get(i).getMacAddress())) { - position = i; - break; - } - } - AlertDialog.Builder dialog = new AlertDialog.Builder(MainSix_4F_BlueControlActivity.this, AlertDialog.THEME_HOLO_LIGHT); - dialog.setMessage("确认删除设备?"); - dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() { - @Override - public void onClick( - DialogInterface dialog, - int which) { - showProgressDialog(); - hm.deleteDeviceUser(new HttpListener>() { - private int result; - - @Override - public void success(BaseBean baseBean) { - result = baseBean.result; - } - - @Override - public void finish() { - UIhandler.sendEmptyMessage(result); - } - - @Override - public void error(Exception e, String msg) { - result = Commons.NetWork.ERROR; - } - }, Commons.DEVICE.get(position).getDeviceId(), Commons.USER.getUserId()); - } - }); - dialog.setNegativeButton("取消", null); - dialog.show(); - } - - public Handler UIhandler = new Handler() { - public void handleMessage(android.os.Message msg) { - dismissProgressDialog(); - /** - * 1:修改成功 - 0:修改失败 - -4:请求验证失败,请重新登陆 - */ - switch (msg.what) { - case Commons.NetWork.Del: - case Commons.NetWork.Success: - for (int i = 0; i < Commons.DeviceCamera.size(); i++) { - if (Commons.DEVICE.get(position).getDeviceId().equals(Commons.DeviceCamera.get(i).deviceId)) { - Commons.DeviceCamera.remove(i); - break;//删除摄像头和鱼缸的关系 - } - } - Commons.DEVICE.remove(position); - if (Commons.DEVICE.size() == 0) {//当删除的是最后一个设备的时候,则回到解绑状态 隐藏聊天室 - EventBus.getDefault().post(new UnBindDevice()); - } else { - if (position <= Commons.DevicePosition) { - if (Commons.DevicePosition == 0) { - sp.putInt(Commons.LoginSPKey.Position, Commons.DevicePosition); - } else { - Commons.DevicePosition = Commons.DevicePosition - 1; - sp.putInt(Commons.LoginSPKey.Position, Commons.DevicePosition); - } - } - Commons.IS_CAMERA = false; - sp.putBoolean(Commons.LoginSPKey.IS_CAMERA, Commons.IS_CAMERA); - } - ToastUtil.show(MainSix_4F_BlueControlActivity.this, "删除成功"); - finish(); - AnimationUtil.finishAnimation(MainSix_4F_BlueControlActivity.this); - break; - case Commons.NetWork.Fail: - ToastUtil.show(MainSix_4F_BlueControlActivity.this, "删除失败"); - break; - case Commons.NetWork.ReLogin: - ToastUtil.show(MainSix_4F_BlueControlActivity.this, "请求验证失败 请重新登陆"); - Commons.clean(); - startActivity(new Intent(MainSix_4F_BlueControlActivity.this, LoadingActivity.class)); - break; - case Commons.NetWork.ERROR: - ToastUtil.show(MainSix_4F_BlueControlActivity.this, Commons.Text.ERROR); - break; - case Commons.NetWork.NotImplemented: - ToastUtil.show(MainSix_4F_BlueControlActivity.this, Commons.Text.Unknown); - break; - default: - ToastUtil.show(MainSix_4F_BlueControlActivity.this, Commons.Text.ServerException); - break; - } - - } - }; - - @Override - public void onRefresh() { - RefreshDevice(); - } - - //刷新设备 - private void RefreshDevice() { - rl_setting_clickble = false; - Commons.is_ShowSpecial = true; - LoginDevice(); - } - - //查询一键喂鱼设置 - private void getFeedFishSettings(){ - new Thread() { - @Override - public void run() { - super.run(); - map.put(Commons.FishKey.Key, Commons.FishKey.QueryFeedFish);//查询一键喂鱼 - map.put(Commons.FishKey.Mac, mac); - TcpSendThread tsf = new TcpSendThread(MainTabActivity.socket,map); - Thread thread3 = new Thread(tsf); - thread3.start(); - } - }.start(); - } - - //点击一键喂鱼按钮后的弹出框 - private void showFeedFishStyleDialog(){ - IosAlertDialog dialog = new IosAlertDialog(this).builder(); - dialog.setTitle("提示"); - dialog.setCancelable(true); - byte feed_fish_s2m = backInfoModelSix_4F.getFeed_fish_s2m(); - String option = null; - if(Byte.toString(feed_fish_s2m).equals(Commons.FishKey.On)){ //已经开启了 - dialog.setMessage("确认关闭一键喂鱼吗?"); - option = FishFeedType.CLOSE_FISH_FEED + ""; - }else{ - dialog.setMessage("确认开始一键喂鱼吗?"); - option = FishFeedType.OPEN_FISH_FEED + ""; - } - dialog.setNegativeButton("取消", new View.OnClickListener() { - @Override - public void onClick(View view) { - - } - }); - final String finalOption = option; - dialog.setPositiveButton("确认", new View.OnClickListener() { - @Override - public void onClick(View view) { - openFishFeedingNow(finalOption);//立即一键喂鱼 - new Handler().postDelayed(new Runnable() { - @Override - public void run() { - if(finalOption.equals(FishFeedType.CLOSE_FISH_FEED + "")){ - tv_setfish.setBackground(getDrawable(R.drawable.select_un_bg)); - }else{ - tv_setfish.setBackground(getDrawable(R.drawable.select_bg)); - } - } - },1000); - } - }); - dialog.show(); - } - - /** - * 立即执行一键喂鱼 - */ - private void openFishFeedingNow(final String feedType){ - new Thread() { - @Override - public void run() { - super.run(); - map.put(Commons.FishKey.Key, Commons.FishKey.OpenFishFeedingNow);//开启一键喂鱼 - map.put(Commons.FishKey.Mac, mac); - map.put(Commons.FishKey.OptionType,feedType); - TcpSendThread ts3 = new TcpSendThread(MainTabActivity.socket, map); - Thread thread3 = new Thread(ts3); - thread3.start(); - } - }.start(); - } - - @Override - public void onClick(View v) {//点击事件 - Intent i = new Intent(); - super.onClick(v); - switch (v.getId()) { -// case R.id.iv_right://定时设置 -// case R.id.tv_right: { -// if (rl_setting_clickble) { -// if (!DeviceOnLine) { -// ToastUtil.show(MainSeven_2F_ControlActivity.this, Commons.Text.OFFDevice); -// return; -// } -// i.setClass(MainSeven_2F_ControlActivity.this, TimeSettingFour_2B.class); -// i.putExtra(TimeSettingFour_2B.BYTEOBJECT, backQueryObjFour_2B); -// i.putExtra(TimeSettingFour_2B.MAC, mac); -// startActivity(i); -// AnimationUtil.startAnimation(MainSeven_2F_ControlActivity.this); -// } -// break; -// } - case R.id.tv_setfish: //一键喂鱼 - //先查询一键喂鱼设置 - isClick_FeedFish = true; - getFeedFishSettings(); - break; - case R.id.tv_setdian://电量统计 - //跳转到电量统计页面 - if (!DeviceOnLine) { - ToastUtil.show(MainSix_4F_BlueControlActivity.this, Commons.Text.OFFDevice); - return; - } - i.setClass(MainSix_4F_BlueControlActivity.this, DianLiangControlActivity.class); - i.putExtra(DianLiangControlActivity.BYTEOBJECT, backInfoModelSix_4F); - i.putExtra(DianLiangControlActivity.MAC, mac); - startActivity(i); - AnimationUtil.startAnimation(MainSix_4F_BlueControlActivity.this); - break; - case R.id.title_img_right: {//打开右侧泡泡窗口 - //是否展示“分享设备” - if (Commons.FishKey.On.equals(Commons.DEVICE.get(sp.getInt(Commons.LoginSPKey.Position, 0)).getIsMaster())) { - PopWindowDevice_EventBus popWindowDevice_eventBus = new PopWindowDevice_EventBus(MainSix_4F_BlueControlActivity.this, true); - popWindowDevice_eventBus.showPopupWindow(title_img_right); - popWindowDevice_eventBus.showSet(); - } else { - PopWindowDevice_EventBus popWindowDevice_eventBus = new PopWindowDevice_EventBus(MainSix_4F_BlueControlActivity.this, false); - popWindowDevice_eventBus.showPopupWindow(title_img_right); - popWindowDevice_eventBus.showSet(); - } - break; - } - case R.id.iv_control_1: { - if (rl_setting_clickble) { - rl_setting_clickble = false; - startTimer(); - showgif(); - if (!DeviceOnLine) {//离线就发登录指令 否则开关灯 - LoginDevice(); - } else { - if (!iv_control_1.isChecked()) { - OnDevice(5); - } else { - OFFDevice(5); - } - } - } - break; - } - case R.id.iv_control_2: { - if (rl_setting_clickble) { - rl_setting_clickble = false; - startTimer(); - showgif(); - if (!DeviceOnLine) {//离线就发登录指令 否则开关灯 - LoginDevice(); - } else { - if (!iv_control_2.isChecked()) { - OnDevice(1); - } else { - OFFDevice(1); - } - } - } - break; - } - case R.id.iv_control_3: { - if (rl_setting_clickble) { - rl_setting_clickble = false; - startTimer(); - showgif(); - if (!DeviceOnLine) {//离线就发登录指令 否则开关灯 - LoginDevice(); - } else { - if (!iv_control_3.isChecked()) { - OnDevice(2); - } else { - OFFDevice(2); - } - } - } - break; - } - case R.id.iv_control_4: { - if (rl_setting_clickble) { - rl_setting_clickble = false; - startTimer(); - showgif(); - if (!DeviceOnLine) {//离线就发登录指令 否则开关灯 - LoginDevice(); - } else { - if (!iv_control_4.isChecked()) { - OnDevice(3); - } else { - OFFDevice(3); - } - } - } - break; - } - case R.id.iv_control_5: { - if (rl_setting_clickble) { - rl_setting_clickble = false; - startTimer(); - showgif(); - if (!DeviceOnLine) {//离线就发登录指令 否则开关灯 - LoginDevice(); - } else { - if (!iv_control_5.isChecked()) { - OnDevice(6); - } else { - OFFDevice(6); - } - } - } - break; - } - case R.id.iv_control_6: { - if (rl_setting_clickble) { - rl_setting_clickble = false; - startTimer(); - showgif(); - if (!DeviceOnLine) {//离线就发登录指令 否则开关灯 - LoginDevice(); - } else { - if (!iv_control_6.isChecked()) { - SarkLampOn(); - } else { - SarkLampOff(); - } - } - } - break; - } - case R.id.iv_control_7: { - if (rl_setting_clickble) { - rl_setting_clickble = false; - startTimer(); - if (!DeviceOnLine) {//离线就发登录指令 否则开关灯 - showgif(); - LoginDevice(); - } else { - if (!iv_control_7.isChecked()) { - showgif(); - OnDevice(4); - } else { - showOffWaterDialog(4); - } - } - } - break; - } - case R.id.iv_control_8: { - if (rl_setting_clickble) { - rl_setting_clickble = false; - if (!DeviceOnLine) {//离线就发登录指令 否则开关灯 - showgif(); - startTimer(); - LoginDevice(); - } else { - //设置加热棒温度对话框 - showHeatWenduDialog(); - } - } - break; - } - case R.id.rl_typegone: { - if (ll_wendu.getVisibility() == View.VISIBLE) { - ll_dianliang.setVisibility(View.VISIBLE); - ll_wendu.setVisibility(View.GONE); - tv_typename.setText("当月电量"); - new Handler().postDelayed(new Runnable() { - @Override - public void run() { - ll_dianliang.setVisibility(View.GONE); - ll_wendu.setVisibility(View.VISIBLE); - tv_typename.setText("鱼缸温度"); - } - }, 3000); - } else { - ll_dianliang.setVisibility(View.GONE); - ll_wendu.setVisibility(View.VISIBLE); - tv_typename.setText("鱼缸温度"); - } - break; - } - } - - } - - private SelectorImageView iv_hot; - - private void showHeatWenduDialog() { - rl_setting_clickble = true; - final String[] temps = new String[51]; - for (int i = 0; i < 51; i++) { - temps[i] = (i + 1) + ""; - } - int currentItem = 0; - final WheelView wv_water; - Button bt_submit; - waterTempDialog = new Dialog(MainSix_4F_BlueControlActivity.this, R.style.HOLOMyDialogs); - waterTempDialog.setCancelable(true); - Window window = waterTempDialog.getWindow(); - window.setContentView(R.layout.setwatertemp_dialog); - wv_water = (WheelView) window.findViewById(R.id.wv_water); - bt_submit = (Button) window.findViewById(R.id.bt_submit); - iv_hot = (SelectorImageView) window.findViewById(R.id.iv_hot); - iv_hot.toggle(false); - wv_water.setViewAdapter(new ArrayWheelAdapter(MainSix_4F_BlueControlActivity.this, temps)); - waterTempDialog.show(); - if (backInfoModelSix_4F != null) { - if (Byte.toString(backInfoModelSix_4F.getOnoff7()).equals(Commons.FishKey.On)) { - iv_hot.toggle(true); - } else { - iv_hot.toggle(false); - } - String temp = backInfoModelSix_4F.getHeatWendu() / 10 + ""; - for (int i = 0; i < temps.length; i++) { - if (temp.equals(temps[i])) { - currentItem = i; - break; - } - } - if (currentItem == 0) {//如果默认设置第一个 就显示第一个温度 但是获取到的温度为空的话就显示20摄氏度 - if ("1".equals(temp)) { - wv_water.setCurrentItem(currentItem); - } else { - wv_water.setCurrentItem(20); - } - } else { - wv_water.setCurrentItem(currentItem); - } - } else { - wv_water.setCurrentItem(20); - } - iv_hot.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - showProgressDialogCancelble(); - if (iv_hot.isChecked()) { - OFFDevice(7); - } else { - OnDevice(7); - } - } - }); - bt_submit.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - isWaterDialog = true; - showgif(); - startTimer(); - setHotWater(temps[wv_water.getCurrentItem()]); - } - }); - } - - //发送控制设备开灯的指令 - private void OnDevice(final int timer_number) { - new Thread() { - @Override - public void run() { - super.run(); - MainTabActivity.spmap.put(Commons.FishKey.timer_number, timer_number); - map.put(Commons.FishKey.Key, Commons.FishKey.On);//开灯 - map.put(Commons.FishKey.Mac, mac); - TcpSendThread ts3 = new TcpSendThread(MainTabActivity.socket, map, MainTabActivity.spmap); - Thread thread3 = new Thread(ts3); - thread3.start(); - } - }.start(); - } - - //发送控制设备关灯的指令 - private void OFFDevice(final int timer_number) { - new Thread() { - @Override - public void run() { - super.run(); - MainTabActivity.spmap.put(Commons.FishKey.timer_number, timer_number); - map.put(Commons.FishKey.Key, Commons.FishKey.Off);//关灯 - map.put(Commons.FishKey.Mac, mac); - TcpSendThread ts3 = new TcpSendThread(MainTabActivity.socket, map, MainTabActivity.spmap); - Thread thread3 = new Thread(ts3); - thread3.start(); - } - }.start(); - } - - //柜灯开 - private void SarkLampOn() { - new Thread() { - @Override - public void run() { - super.run(); - map.put(Commons.FishKey.Key, Commons.FishKey.SarkLampOn);//柜灯开 - map.put(Commons.FishKey.Mac, mac); - TcpSendThread ts3 = new TcpSendThread(MainTabActivity.socket, map); - Thread thread3 = new Thread(ts3); - thread3.start(); - } - }.start(); - } - - //柜灯关 - private void SarkLampOff() { - new Thread() { - @Override - public void run() { - super.run(); - map.put(Commons.FishKey.Key, Commons.FishKey.SarkLampOff);//柜灯关 - map.put(Commons.FishKey.Mac, mac); - TcpSendThread ts3 = new TcpSendThread(MainTabActivity.socket, map); - Thread thread3 = new Thread(ts3); - thread3.start(); - } - }.start(); - } - - //设置加热棒温度 - private void setHotWater(final String hot_water_temp) { - new Thread() { - @Override - public void run() { - super.run(); - map.put(Commons.FishKey.Key, Commons.FishKey.HotWaterTemp); - map.put(Commons.FishKey.hot_water_temp, hot_water_temp); - map.put(Commons.FishKey.Mac, mac); - TcpSendThread ts3 = new TcpSendThread(MainTabActivity.socket, map); - Thread thread3 = new Thread(ts3); - thread3.start(); - } - }.start(); - } - - private void showOffWaterDialog(final int num) { - AlertDialog.Builder dialog = new AlertDialog.Builder(MainSix_4F_BlueControlActivity.this); - dialog.setTitle("提示"); - dialog.setCancelable(true); - dialog.setMessage("确认关闭循环泵"); - dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface arg0, int arg1) { - } - }); - dialog.setPositiveButton("确认", new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - dialog.dismiss(); - OFFDevice(num); - startTimer(); - } - }); - dialog.show(); - } -} diff --git a/iFish7/src/main/java/com/ifish/activity/newbind/NewBindDeviceActivity.java b/iFish7/src/main/java/com/ifish/activity/newbind/NewBindDeviceActivity.java index d0a18dc0a..a14154a42 100644 --- a/iFish7/src/main/java/com/ifish/activity/newbind/NewBindDeviceActivity.java +++ b/iFish7/src/main/java/com/ifish/activity/newbind/NewBindDeviceActivity.java @@ -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); + } } \ No newline at end of file diff --git a/iFish7/src/main/java/com/ifish/adapter/BlueToothController.java b/iFish7/src/main/java/com/ifish/adapter/BlueToothController.java index 8a75141fa..8875c8936 100644 --- a/iFish7/src/main/java/com/ifish/adapter/BlueToothController.java +++ b/iFish7/src/main/java/com/ifish/adapter/BlueToothController.java @@ -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; +// } +// +// +// } +// +//} diff --git a/iFish7/src/main/java/com/ifish/fragment/DeviceFragment.java b/iFish7/src/main/java/com/ifish/fragment/DeviceFragment.java index 72ed9b333..5d5690890 100644 --- a/iFish7/src/main/java/com/ifish/fragment/DeviceFragment.java +++ b/iFish7/src/main/java/com/ifish/fragment/DeviceFragment.java @@ -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 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(); } } diff --git a/iFish7/src/main/java/com/ifish/utils/BLEManger.java b/iFish7/src/main/java/com/ifish/utils/BLEManger.java index 5ef450f84..e04252212 100644 --- a/iFish7/src/main/java/com/ifish/utils/BLEManger.java +++ b/iFish7/src/main/java/com/ifish/utils/BLEManger.java @@ -1,517 +1,517 @@ -package com.ifish.utils; - -import android.annotation.SuppressLint; -import android.bluetooth.BluetoothAdapter; -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.content.Context; -import android.content.Intent; -import android.os.Build; -import android.text.TextUtils; - -import androidx.annotation.RequiresApi; -import androidx.appcompat.app.AppCompatActivity; - -import com.ifish.basebean.EventBean; -import com.ifish.baseclass.BaseApplication; -import com.ifish.bluetooth.ClsUtils; - -import java.util.List; - -import de.greenrobot.event.EventBus; - -public class BLEManger { - BluetoothAdapter bluetoothAdapter; - BluetoothManager bluetoothManager; - - - BLEManger(){ - bluetoothManager = (BluetoothManager) BaseApplication.app.getSystemService(Context.BLUETOOTH_SERVICE); - bluetoothAdapter = bluetoothManager.getAdapter(); - } - private static BLEManger single=null; - //静态工厂方法 - public static BLEManger getInstance() { - if (single == null) { - single = new BLEManger(); - } - return single; - } - - /** - * 获取蓝牙状态 - */ - public boolean isEnable(){ - if(bluetoothAdapter == null){ - return false; - } - return bluetoothAdapter.isEnabled(); - } - - /** - * 打开蓝牙 - * @param isFast true 直接打开蓝牙 false 提示用户打开 - */ - @SuppressLint("MissingPermission") - public void openBluetooth(Context context, boolean isFast){ - if(!isEnable()){ - if(isFast){ - L.i("jjia-----直接打开手机蓝牙"); - bluetoothAdapter.enable(); //BLUETOOTH_ADMIN权限 - }else{ - L.i("jjia-----提示用户去打开手机蓝牙"); - Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); - context.startActivity(enableBtIntent); - } - }else{ - L.i("jjia-----手机蓝牙状态已开"); - } - } - - public Boolean isHome =false; - @SuppressLint("MissingPermission") - public void startDiscovery(Boolean home){ - L.i("jjia---------startDiscovery---init---"); - if (bluetoothAdapter!=null){ - isHome = home; - bluetoothAdapter.startLeScan(leScanCallback); - } - } - public BluetoothDevice currentDevice; - - public int count = 0; - private BluetoothAdapter.LeScanCallback leScanCallback = new BluetoothAdapter.LeScanCallback() { - @SuppressLint("MissingPermission") - @Override - public void onLeScan(BluetoothDevice bluetoothDevice, int rssi, byte[] bytes) { - //在onLeScan()回调中尽量做少的操作,可以将扫描到的设备扔到另一个线程中处理 - if(bluetoothDevice == null){ - return; - } - String name = bluetoothDevice.getName(); - if (!TextUtils.isEmpty(name) && name.startsWith("ifishly")){ - stopScan(); - count++; - if (count<1){ - - } - L.i("---jjia----------匹配到了"); - currentDevice = bluetoothDevice; - EventBus.getDefault().post(currentDevice); - - L.d(bluetoothDevice.getName() + "--jjia>" + bluetoothDevice.getAddress()+"---"+rssi); - - } -// L.d(bluetoothDevice.getName() + "--jjia---toher>" + bluetoothDevice.getAddress()+"---"+rssi); -// BLEDevice bleDevice = new BLEDevice(bluetoothDevice,rssi); -// if(onDeviceSearchListener != null){ -// onDeviceSearchListener.onDeviceFound(bleDevice); //扫描到设备回调 -// } - } - }; - - @SuppressLint("MissingPermission") - public void stopScan(){ - if (bluetoothAdapter!=null){ - bluetoothAdapter.stopLeScan(leScanCallback); - } - } - - @SuppressLint("MissingPermission") - public void connect(AppCompatActivity activity){ - if (currentDevice!=null){ - currentDevice.connectGatt(activity,false,bluetoothGattCallback); - } - - } - - //连接/通讯结果回调 - @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2) - private BluetoothGattCallback bluetoothGattCallback = new BluetoothGattCallback() { - //连接状态回调-连接成功/断开连接 - @SuppressLint("MissingPermission") - @Override - public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { - super.onConnectionStateChange(gatt, status, newState); - L.d("jjia-----status:" + status); - L.d("jjia------newState:" + newState); - // GATT的链接状态回调 - switch (newState){ - case BluetoothGatt.STATE_CONNECTED: - L.d("jjia----蓝牙连接成功"); - gatt.discoverServices(); // 去发现服务 - break; - - case BluetoothGatt.STATE_DISCONNECTED: - //青空系统缓存 - ClsUtils.refreshDeviceCache(gatt); - L.d("jjia----蓝牙连接失败"); - gatt.close();//断开连接释放连接 - - if (status == 133){ - L.d("jjia----无法连接"); - }else if (status == 62){ - L.d("jjia----连接成功服务没有发现"); - }else if(status ==0){ - L.d("jjia----0正常断开 回调"); - }else if(status ==8){ - L.d("jjia----距离太远或者无法供电而断开"); - } - break; - - case BluetoothGatt.STATE_CONNECTING: - L.d("jjia----正在连接中。。。。。"); - break; - - case BluetoothGatt.STATE_DISCONNECTING: - L.d("jjia----正在断开中。。。。。"); - break; - } - BluetoothDevice bluetoothDevice = gatt.getDevice(); - L.d("jjia连接的设备:" + bluetoothDevice.getName() + " " + bluetoothDevice.getAddress()); - -// isConnectIng = false; -// //移除连接超时 -// mHandler.removeCallbacks(connectOutTimeRunnable); +//package com.ifish.utils; // -// if(newState == BluetoothGatt.STATE_CONNECTED){ -// L.d("jjia连接成功"); -// //连接成功去发现服务 -// gatt.discoverServices(); -// //设置发现服务超时时间 -// mHandler.postDelayed(serviceDiscoverOutTimeRunnable,MAX_CONNECT_TIME); +//import android.annotation.SuppressLint; +//import android.bluetooth.BluetoothAdapter; +//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.content.Context; +//import android.content.Intent; +//import android.os.Build; +//import android.text.TextUtils; // -// if(onBleConnectListener != null){ -// onBleConnectListener.onConnectSuccess(gatt,bluetoothDevice,status); //连接成功回调 -// } -// }else if(newState == BluetoothGatt.STATE_DISCONNECTED) { -// //清空系统缓存 -// ClsUtils.refreshDeviceCache(gatt); -// Log.e(TAG, "断开连接status:" + status); -// gatt.close(); //断开连接释放连接 +//import androidx.annotation.RequiresApi; +//import androidx.appcompat.app.AppCompatActivity; // -// if(status == 133){ -// //无法连接 -// if(onBleConnectListener != null){ -// gatt.close(); -// onBleConnectListener.onConnectFailure(gatt,bluetoothDevice,"连接异常!",status); //133连接异常 异常断开 -// L.d("jjia连接失败status:" + status + " " + bluetoothDevice.getAddress()); -// } -// }else if(status == 62){ -// //成功连接没有发现服务断开 -// if(onBleConnectListener != null){ -// gatt.close(); -// onBleConnectListener.onConnectFailure(gatt,bluetoothDevice,"连接成功服务未发现断开!",status); //62没有发现服务 异常断开 -// L.d("jjia连接成功服务未发现断开status:" + status); -// } +//import com.ifish.basebean.EventBean; +//import com.ifish.baseclass.BaseApplication; +//import com.ifish.bluetooth.ClsUtils; // -// }else if(status == 0){ -// if(onBleConnectListener != null){ -// onBleConnectListener.onDisConnectSuccess(gatt,bluetoothDevice,status); //0正常断开 回调 -// } -// }else if(status == 8){ -// //因为距离远或者电池无法供电断开连接 -// // 已经成功发现服务 -// if(onBleConnectListener != null){ -// onBleConnectListener.onDisConnectSuccess(gatt,bluetoothDevice,status); //8断电断开 回调 -// } -// }else if(status == 34){ -// if(onBleConnectListener != null){ -// onBleConnectListener.onDisConnectSuccess(gatt,bluetoothDevice,status); //34断开 -// } -// }else { -// //其它断开连接 -// if(onBleConnectListener != null){ -// onBleConnectListener.onDisConnectSuccess(gatt,bluetoothDevice,status); //其它断开 -// } -// } -// }else if(newState == BluetoothGatt.STATE_CONNECTING){ -// L.d("jjia正在连接..."); -// if(onBleConnectListener != null){ -// onBleConnectListener.onConnecting(gatt,bluetoothDevice); //正在连接回调 -// } -// }else if(newState == BluetoothGatt.STATE_DISCONNECTING){ -// L.d("jjia正在断开..."); -// if(onBleConnectListener != null){ -// onBleConnectListener.onDisConnecting(gatt,bluetoothDevice); //正在断开回调 -// } -// } - } - - - - - ///////////////////////////////////////// 打开通知 ////////////////////////////////////////// - - /** - * 设置读特征接收通知 - * @param enable 为true打开通知 - * @param gatt 连接 - * @param characteristic 特征 - */ - @SuppressLint("MissingPermission") - public void enableNotification(boolean enable, BluetoothGatt gatt, BluetoothGattCharacteristic characteristic){ - if(gatt == null){ - L.i("jjia------enableNotification-->gatt == null"); - return; - } - if(characteristic == null){ - L.i("jjia------enableNotification-->characteristic == null"); - return; - } - //这一步必须要有,否则接收不到通知 - gatt.setCharacteristicNotification(characteristic,enable); - } - //发现服务 - //获取GATT服务发现后的回调 - @SuppressLint("MissingPermission") - @Override - public void onServicesDiscovered(BluetoothGatt gatt, int status) { - super.onServicesDiscovered(gatt, status); - - if (status == BluetoothGatt.GATT_SUCCESS){ - - mGatt = gatt; - - L.i("jjia------GATT_SUCCESS---services"); //服务发现 - - if (gatt.getServices()!=null){ - L.i("jjia-----find---services"+gatt.getServices().size()); //服务发现 - for (BluetoothGattService bluetoothGattService : gatt.getServices()) { - if (bluetoothGattService!=null ){ - String UID = bluetoothGattService.getUuid().toString(); - if (!TextUtils.isEmpty(UID) && SERVICE_UUID.equals(UID)){ - service = bluetoothGattService; - L.i("jjia------Service_UUID:" + bluetoothGattService.getUuid()); // 我们可以遍历到该蓝牙设备的全部Service对象。然后通过比较Service的UUID,我们可以区分该服务是属于什么业务的 - for (BluetoothGattCharacteristic characteristic : bluetoothGattService.getCharacteristics()) { - L.i("jjia------characteristic-UUID:" + characteristic.getUuid()); - if (characteristic!=null && !TextUtils.isEmpty(characteristic.getUuid().toString()) && WRITE_UUID.equals(characteristic.getUuid().toString())){ - - write = characteristic; - read = characteristic; - - //打开读通知 - enableNotification(true, gatt, read); - - //重点中重点,需要重新设置 - List descriptors = write.getDescriptors(); - for (BluetoothGattDescriptor descriptor : descriptors) { - descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); - gatt.writeDescriptor(descriptor); - } - } - } - } - - } - - - EventBean eventBean = new EventBean("blue_ok"); - EventBus.getDefault().post(eventBean); +//import java.util.List; // -// if (SERVICE_UUID.equals(bluetoothGattService.getUuid().toString())) { +//import de.greenrobot.event.EventBus; // -// for (BluetoothGattCharacteristic characteristic : bluetoothGattService.getCharacteristics()) { -// L.i("jjia------characteristic-UUID:" + characteristic.getUuid()); -//// prepareBroadcastDataNotify(gatt, characteristic); //给满足条件的属性配置上消息通知 -// } -// return;//结束循环操作 -// } - } - - - }else { - L.i("jjia------no---services"); //服务发现 - } - - } - //移除发现服务超时 -// mHandler.removeCallbacks(serviceDiscoverOutTimeRunnable); -// L.d("jjia移除发现服务超时"); +//public class BLEManger { +// BluetoothAdapter bluetoothAdapter; +// BluetoothManager bluetoothManager; // -// L.d("jjia发现服务"); // -// //配置服务信息 -// if(setupService(gatt,serviceUUID,readUUID,writeUUID)){ -// if(onBleConnectListener != null){ -// onBleConnectListener.onServiceDiscoverySucceed(gatt,gatt.getDevice(),status); //成功发现服务回调 -// } +// BLEManger(){ +// bluetoothManager = (BluetoothManager) BaseApplication.app.getSystemService(Context.BLUETOOTH_SERVICE); +// bluetoothAdapter = bluetoothManager.getAdapter(); +// } +// private static BLEManger single=null; +// //静态工厂方法 +// public static BLEManger getInstance() { +// if (single == null) { +// single = new BLEManger(); +// } +// return single; +// } +// +// /** +// * 获取蓝牙状态 +// */ +// public boolean isEnable(){ +// if(bluetoothAdapter == null){ +// return false; +// } +// return bluetoothAdapter.isEnabled(); +// } +// +// /** +// * 打开蓝牙 +// * @param isFast true 直接打开蓝牙 false 提示用户打开 +// */ +// @SuppressLint("MissingPermission") +// public void openBluetooth(Context context, boolean isFast){ +// if(!isEnable()){ +// if(isFast){ +// L.i("jjia-----直接打开手机蓝牙"); +// bluetoothAdapter.enable(); //BLUETOOTH_ADMIN权限 // }else{ -// if(onBleConnectListener != null){ -// onBleConnectListener.onServiceDiscoveryFailed(gatt,gatt.getDevice(),"获取服务特征异常"); //发现服务失败回调 -// } +// L.i("jjia-----提示用户去打开手机蓝牙"); +// Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); +// context.startActivity(enableBtIntent); // } - } - - @Override - public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { - super.onCharacteristicRead(gatt, characteristic, status); - L.d("jjia-------------读status: " + status); - } - - //向蓝牙设备写入数据结果回调 - @Override - public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { - super.onCharacteristicWrite(gatt, characteristic, status); - - - byte [] bb = characteristic.getValue(); - L.i(characteristic.getUuid()+"jjia---------------向蓝牙设备写入数据结果回调--"+new String(bb)); - L.i(status+"jjia---------------向蓝牙设备写入数据结果回调--"+BluetoothGatt.GATT_SUCCESS); -// if(characteristic.getValue() == null){ -// L.d("jjiacharacteristic.getValue() == null"); +// }else{ +// L.i("jjia-----手机蓝牙状态已开"); +// } +// } +// +// public Boolean isHome =false; +// @SuppressLint("MissingPermission") +// public void startDiscovery(Boolean home){ +// L.i("jjia---------startDiscovery---init---"); +// if (bluetoothAdapter!=null){ +// isHome = home; +// bluetoothAdapter.startLeScan(leScanCallback); +// } +// } +// public BluetoothDevice currentDevice; +// +// public int count = 0; +// private BluetoothAdapter.LeScanCallback leScanCallback = new BluetoothAdapter.LeScanCallback() { +// @SuppressLint("MissingPermission") +// @Override +// public void onLeScan(BluetoothDevice bluetoothDevice, int rssi, byte[] bytes) { +// //在onLeScan()回调中尽量做少的操作,可以将扫描到的设备扔到另一个线程中处理 +// if(bluetoothDevice == null){ // return; // } -// //将收到的字节数组转换成十六进制字符串 -// String msg = TypeConversion.bytes2HexString(characteristic.getValue(),characteristic.getValue().length); -// if(status == BluetoothGatt.GATT_SUCCESS){ -// //写入成功 -// L.d("jjia写入成功:" + msg); -// if(onBleConnectListener != null){ -// onBleConnectListener.onWriteSuccess(gatt,gatt.getDevice(),characteristic.getValue()); //写入成功回调 +// String name = bluetoothDevice.getName(); +// if (!TextUtils.isEmpty(name) && name.startsWith("ifishly")){ +// stopScan(); +// count++; +// if (count<1){ +// +// } +// L.i("---jjia----------匹配到了"); +// currentDevice = bluetoothDevice; +// EventBus.getDefault().post(currentDevice); +// +// L.d(bluetoothDevice.getName() + "--jjia>" + bluetoothDevice.getAddress()+"---"+rssi); +// +// } +//// L.d(bluetoothDevice.getName() + "--jjia---toher>" + bluetoothDevice.getAddress()+"---"+rssi); +//// BLEDevice bleDevice = new BLEDevice(bluetoothDevice,rssi); +//// if(onDeviceSearchListener != null){ +//// onDeviceSearchListener.onDeviceFound(bleDevice); //扫描到设备回调 +//// } +// } +// }; +// +// @SuppressLint("MissingPermission") +// public void stopScan(){ +// if (bluetoothAdapter!=null){ +// bluetoothAdapter.stopLeScan(leScanCallback); +// } +// } +// +// @SuppressLint("MissingPermission") +// public void connect(AppCompatActivity activity){ +// if (currentDevice!=null){ +// currentDevice.connectGatt(activity,false,bluetoothGattCallback); +// } +// +// } +// +// //连接/通讯结果回调 +// @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2) +// private BluetoothGattCallback bluetoothGattCallback = new BluetoothGattCallback() { +// //连接状态回调-连接成功/断开连接 +// @SuppressLint("MissingPermission") +// @Override +// public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { +// super.onConnectionStateChange(gatt, status, newState); +// L.d("jjia-----status:" + status); +// L.d("jjia------newState:" + newState); +// // GATT的链接状态回调 +// switch (newState){ +// case BluetoothGatt.STATE_CONNECTED: +// L.d("jjia----蓝牙连接成功"); +// gatt.discoverServices(); // 去发现服务 +// break; +// +// case BluetoothGatt.STATE_DISCONNECTED: +// //青空系统缓存 +// ClsUtils.refreshDeviceCache(gatt); +// L.d("jjia----蓝牙连接失败"); +// gatt.close();//断开连接释放连接 +// +// if (status == 133){ +// L.d("jjia----无法连接"); +// }else if (status == 62){ +// L.d("jjia----连接成功服务没有发现"); +// }else if(status ==0){ +// L.d("jjia----0正常断开 回调"); +// }else if(status ==8){ +// L.d("jjia----距离太远或者无法供电而断开"); +// } +// break; +// +// case BluetoothGatt.STATE_CONNECTING: +// L.d("jjia----正在连接中。。。。。"); +// break; +// +// case BluetoothGatt.STATE_DISCONNECTING: +// L.d("jjia----正在断开中。。。。。"); +// break; +// } +// BluetoothDevice bluetoothDevice = gatt.getDevice(); +// L.d("jjia连接的设备:" + bluetoothDevice.getName() + " " + bluetoothDevice.getAddress()); +// +//// isConnectIng = false; +//// //移除连接超时 +//// mHandler.removeCallbacks(connectOutTimeRunnable); +//// +//// if(newState == BluetoothGatt.STATE_CONNECTED){ +//// L.d("jjia连接成功"); +//// //连接成功去发现服务 +//// gatt.discoverServices(); +//// //设置发现服务超时时间 +//// mHandler.postDelayed(serviceDiscoverOutTimeRunnable,MAX_CONNECT_TIME); +//// +//// if(onBleConnectListener != null){ +//// onBleConnectListener.onConnectSuccess(gatt,bluetoothDevice,status); //连接成功回调 +//// } +//// }else if(newState == BluetoothGatt.STATE_DISCONNECTED) { +//// //清空系统缓存 +//// ClsUtils.refreshDeviceCache(gatt); +//// Log.e(TAG, "断开连接status:" + status); +//// gatt.close(); //断开连接释放连接 +//// +//// if(status == 133){ +//// //无法连接 +//// if(onBleConnectListener != null){ +//// gatt.close(); +//// onBleConnectListener.onConnectFailure(gatt,bluetoothDevice,"连接异常!",status); //133连接异常 异常断开 +//// L.d("jjia连接失败status:" + status + " " + bluetoothDevice.getAddress()); +//// } +//// }else if(status == 62){ +//// //成功连接没有发现服务断开 +//// if(onBleConnectListener != null){ +//// gatt.close(); +//// onBleConnectListener.onConnectFailure(gatt,bluetoothDevice,"连接成功服务未发现断开!",status); //62没有发现服务 异常断开 +//// L.d("jjia连接成功服务未发现断开status:" + status); +//// } +//// +//// }else if(status == 0){ +//// if(onBleConnectListener != null){ +//// onBleConnectListener.onDisConnectSuccess(gatt,bluetoothDevice,status); //0正常断开 回调 +//// } +//// }else if(status == 8){ +//// //因为距离远或者电池无法供电断开连接 +//// // 已经成功发现服务 +//// if(onBleConnectListener != null){ +//// onBleConnectListener.onDisConnectSuccess(gatt,bluetoothDevice,status); //8断电断开 回调 +//// } +//// }else if(status == 34){ +//// if(onBleConnectListener != null){ +//// onBleConnectListener.onDisConnectSuccess(gatt,bluetoothDevice,status); //34断开 +//// } +//// }else { +//// //其它断开连接 +//// if(onBleConnectListener != null){ +//// onBleConnectListener.onDisConnectSuccess(gatt,bluetoothDevice,status); //其它断开 +//// } +//// } +//// }else if(newState == BluetoothGatt.STATE_CONNECTING){ +//// L.d("jjia正在连接..."); +//// if(onBleConnectListener != null){ +//// onBleConnectListener.onConnecting(gatt,bluetoothDevice); //正在连接回调 +//// } +//// }else if(newState == BluetoothGatt.STATE_DISCONNECTING){ +//// L.d("jjia正在断开..."); +//// if(onBleConnectListener != null){ +//// onBleConnectListener.onDisConnecting(gatt,bluetoothDevice); //正在断开回调 +//// } +//// } +// } +// +// +// +// +// ///////////////////////////////////////// 打开通知 ////////////////////////////////////////// +// +// /** +// * 设置读特征接收通知 +// * @param enable 为true打开通知 +// * @param gatt 连接 +// * @param characteristic 特征 +// */ +// @SuppressLint("MissingPermission") +// public void enableNotification(boolean enable, BluetoothGatt gatt, BluetoothGattCharacteristic characteristic){ +// if(gatt == null){ +// L.i("jjia------enableNotification-->gatt == null"); +// return; +// } +// if(characteristic == null){ +// L.i("jjia------enableNotification-->characteristic == null"); +// return; +// } +// //这一步必须要有,否则接收不到通知 +// gatt.setCharacteristicNotification(characteristic,enable); +// } +// //发现服务 +// //获取GATT服务发现后的回调 +// @SuppressLint("MissingPermission") +// @Override +// public void onServicesDiscovered(BluetoothGatt gatt, int status) { +// super.onServicesDiscovered(gatt, status); +// +// if (status == BluetoothGatt.GATT_SUCCESS){ +// +// mGatt = gatt; +// +// L.i("jjia------GATT_SUCCESS---services"); //服务发现 +// +// if (gatt.getServices()!=null){ +// L.i("jjia-----find---services"+gatt.getServices().size()); //服务发现 +// for (BluetoothGattService bluetoothGattService : gatt.getServices()) { +// if (bluetoothGattService!=null ){ +// String UID = bluetoothGattService.getUuid().toString(); +// if (!TextUtils.isEmpty(UID) && SERVICE_UUID.equals(UID)){ +// service = bluetoothGattService; +// L.i("jjia------Service_UUID:" + bluetoothGattService.getUuid()); // 我们可以遍历到该蓝牙设备的全部Service对象。然后通过比较Service的UUID,我们可以区分该服务是属于什么业务的 +// for (BluetoothGattCharacteristic characteristic : bluetoothGattService.getCharacteristics()) { +// L.i("jjia------characteristic-UUID:" + characteristic.getUuid()); +// if (characteristic!=null && !TextUtils.isEmpty(characteristic.getUuid().toString()) && WRITE_UUID.equals(characteristic.getUuid().toString())){ +// +// write = characteristic; +// read = characteristic; +// +// //打开读通知 +// enableNotification(true, gatt, read); +// +// //重点中重点,需要重新设置 +// List descriptors = write.getDescriptors(); +// for (BluetoothGattDescriptor descriptor : descriptors) { +// descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); +// gatt.writeDescriptor(descriptor); +// } +// } +// } +// } +// +// } +// +// +// EventBean eventBean = new EventBean("blue_ok"); +// EventBus.getDefault().post(eventBean); +//// +//// if (SERVICE_UUID.equals(bluetoothGattService.getUuid().toString())) { +//// +//// for (BluetoothGattCharacteristic characteristic : bluetoothGattService.getCharacteristics()) { +//// L.i("jjia------characteristic-UUID:" + characteristic.getUuid()); +////// prepareBroadcastDataNotify(gatt, characteristic); //给满足条件的属性配置上消息通知 +//// } +//// return;//结束循环操作 +//// } +// } +// +// +// }else { +// L.i("jjia------no---services"); //服务发现 // } // -// }else if(status == BluetoothGatt.GATT_FAILURE){ -// //写入失败 -// L.d("jjia写入失败:" + msg); -// if(onBleConnectListener != null){ -// onBleConnectListener.onWriteFailure(gatt,gatt.getDevice(),characteristic.getValue(),"写入失败"); //写入失败回调 -// } -// }else if(status == BluetoothGatt.GATT_WRITE_NOT_PERMITTED){ -// //没有权限 -// L.d("jjia没有权限!"); // } - } - - //读取蓝牙设备发出来的数据回调 - @Override - public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { - super.onCharacteristicChanged(gatt, characteristic); - - //接收数据 - byte[] bytes = characteristic.getValue(); - - L.i(characteristic.getUuid()+"jjia---------------读取蓝牙设备发出来的数据回调--"); -// Log.w("TAG","收到数据str:" + TypeConversion.bytes2HexString(bytes,bytes.length)); -// if(onBleConnectListener != null){ -// onBleConnectListener.onReceiveMessage(gatt,gatt.getDevice(),characteristic,characteristic.getValue()); //接收数据回调 -// } - } - - @Override - public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) { - super.onDescriptorRead(gatt, descriptor, status); - } - - @Override - public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) { - super.onDescriptorWrite(gatt, descriptor, status); - } - - @Override - public void onReliableWriteCompleted(BluetoothGatt gatt, int status) { - super.onReliableWriteCompleted(gatt, status); - L.d("jjiaonReliableWriteCompleted"); - } - - @Override - public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) { - super.onReadRemoteRssi(gatt, rssi, status); -// if(status == BluetoothGatt.GATT_SUCCESS){ -// L.d("jjia读取RSSI值成功,RSSI值:" + rssi + ",status" + status); -// if(onBleConnectListener != null){ -// onBleConnectListener.onReadRssi(gatt,rssi,status); //成功读取连接的信号强度回调 -// } -// }else if(status == BluetoothGatt.GATT_FAILURE){ -// L.d("jjia读取RSSI值失败,status:" + status); -// } - } - - //修改MTU值结果回调 - @Override - public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) { - super.onMtuChanged(gatt, mtu, status); - ///设置mtu值,即bluetoothGatt.requestMtu()时触发,提示该操作是否成功 -// if(status == BluetoothGatt.GATT_SUCCESS){ //设置MTU成功 -// //MTU默认取的是23,当收到 onMtuChanged 后,会根据传递的值修改MTU,注意由于传输用掉3字节,因此传递的值需要减3。 -// //mtu - 3 -// L.d("jjia设置MTU成功,新的MTU值:" + (mtu-3) + ",status" + status); -// if(onBleConnectListener != null){ -// onBleConnectListener.onMTUSetSuccess("设置后新的MTU值 = " + (mtu-3) + " status = " + status,mtu - 3); //MTU设置成功 -// } +// //移除发现服务超时 +//// mHandler.removeCallbacks(serviceDiscoverOutTimeRunnable); +//// L.d("jjia移除发现服务超时"); +//// +//// L.d("jjia发现服务"); +//// +//// //配置服务信息 +//// if(setupService(gatt,serviceUUID,readUUID,writeUUID)){ +//// if(onBleConnectListener != null){ +//// onBleConnectListener.onServiceDiscoverySucceed(gatt,gatt.getDevice(),status); //成功发现服务回调 +//// } +//// }else{ +//// if(onBleConnectListener != null){ +//// onBleConnectListener.onServiceDiscoveryFailed(gatt,gatt.getDevice(),"获取服务特征异常"); //发现服务失败回调 +//// } +//// } +// } // -// }else if(status == BluetoothGatt.GATT_FAILURE){ //设置MTU失败 -// L.d("jjia设置MTU值失败:" + (mtu-3) + ",status" + status); -// if(onBleConnectListener != null){ -// onBleConnectListener.onMTUSetFailure("设置MTU值失败:" + (mtu-3) + " status:" + status); //MTU设置失败 -// } +// @Override +// public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { +// super.onCharacteristicRead(gatt, characteristic, status); +// L.d("jjia-------------读status: " + status); +// } +// +// //向蓝牙设备写入数据结果回调 +// @Override +// public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { +// super.onCharacteristicWrite(gatt, characteristic, status); +// +// +// byte [] bb = characteristic.getValue(); +// L.i(characteristic.getUuid()+"jjia---------------向蓝牙设备写入数据结果回调--"+new String(bb)); +// L.i(status+"jjia---------------向蓝牙设备写入数据结果回调--"+BluetoothGatt.GATT_SUCCESS); +//// if(characteristic.getValue() == null){ +//// L.d("jjiacharacteristic.getValue() == null"); +//// return; +//// } +//// //将收到的字节数组转换成十六进制字符串 +//// String msg = TypeConversion.bytes2HexString(characteristic.getValue(),characteristic.getValue().length); +//// if(status == BluetoothGatt.GATT_SUCCESS){ +//// //写入成功 +//// L.d("jjia写入成功:" + msg); +//// if(onBleConnectListener != null){ +//// onBleConnectListener.onWriteSuccess(gatt,gatt.getDevice(),characteristic.getValue()); //写入成功回调 +//// } +//// +//// }else if(status == BluetoothGatt.GATT_FAILURE){ +//// //写入失败 +//// L.d("jjia写入失败:" + msg); +//// if(onBleConnectListener != null){ +//// onBleConnectListener.onWriteFailure(gatt,gatt.getDevice(),characteristic.getValue(),"写入失败"); //写入失败回调 +//// } +//// }else if(status == BluetoothGatt.GATT_WRITE_NOT_PERMITTED){ +//// //没有权限 +//// L.d("jjia没有权限!"); +//// } +// } +// +// //读取蓝牙设备发出来的数据回调 +// @Override +// public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { +// super.onCharacteristicChanged(gatt, characteristic); +// +// //接收数据 +// byte[] bytes = characteristic.getValue(); +// +// L.i(characteristic.getUuid()+"jjia---------------读取蓝牙设备发出来的数据回调--"); +//// Log.w("TAG","收到数据str:" + TypeConversion.bytes2HexString(bytes,bytes.length)); +//// if(onBleConnectListener != null){ +//// onBleConnectListener.onReceiveMessage(gatt,gatt.getDevice(),characteristic,characteristic.getValue()); //接收数据回调 +//// } +// } +// +// @Override +// public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) { +// super.onDescriptorRead(gatt, descriptor, status); +// } +// +// @Override +// public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) { +// super.onDescriptorWrite(gatt, descriptor, status); +// } +// +// @Override +// public void onReliableWriteCompleted(BluetoothGatt gatt, int status) { +// super.onReliableWriteCompleted(gatt, status); +// L.d("jjiaonReliableWriteCompleted"); +// } +// +// @Override +// public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) { +// super.onReadRemoteRssi(gatt, rssi, status); +//// if(status == BluetoothGatt.GATT_SUCCESS){ +//// L.d("jjia读取RSSI值成功,RSSI值:" + rssi + ",status" + status); +//// if(onBleConnectListener != null){ +//// onBleConnectListener.onReadRssi(gatt,rssi,status); //成功读取连接的信号强度回调 +//// } +//// }else if(status == BluetoothGatt.GATT_FAILURE){ +//// L.d("jjia读取RSSI值失败,status:" + status); +//// } +// } +// +// //修改MTU值结果回调 +// @Override +// public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) { +// super.onMtuChanged(gatt, mtu, status); +// ///设置mtu值,即bluetoothGatt.requestMtu()时触发,提示该操作是否成功 +//// if(status == BluetoothGatt.GATT_SUCCESS){ //设置MTU成功 +//// //MTU默认取的是23,当收到 onMtuChanged 后,会根据传递的值修改MTU,注意由于传输用掉3字节,因此传递的值需要减3。 +//// //mtu - 3 +//// L.d("jjia设置MTU成功,新的MTU值:" + (mtu-3) + ",status" + status); +//// if(onBleConnectListener != null){ +//// onBleConnectListener.onMTUSetSuccess("设置后新的MTU值 = " + (mtu-3) + " status = " + status,mtu - 3); //MTU设置成功 +//// } +//// +//// }else if(status == BluetoothGatt.GATT_FAILURE){ //设置MTU失败 +//// L.d("jjia设置MTU值失败:" + (mtu-3) + ",status" + status); +//// if(onBleConnectListener != null){ +//// onBleConnectListener.onMTUSetFailure("设置MTU值失败:" + (mtu-3) + " status:" + status); //MTU设置失败 +//// } +//// } +// +// } +// }; +// +// public static final String SERVICE_UUID = "000000ff-0000-1000-8000-00805f9b34fb"; //蓝牙通讯服务 +// public static final String READ_UUID = "0000ff01-0000-1000-8000-00805f9b34fb"; //读特征 +// public static final String WRITE_UUID = "0000ff01-0000-1000-8000-00805f9b34fb"; //写特征 +// BluetoothGattService service; +// BluetoothGattCharacteristic read; +// BluetoothGattCharacteristic write; +// BluetoothGatt mGatt ; +// +// @SuppressLint("MissingPermission") +// public void sendData(byte [] data){ +// if (write!=null){ +// L.d("jjia------send--------"); +// write.setValue(data); +// mGatt.writeCharacteristic(write); // } - - } - }; - - public static final String SERVICE_UUID = "000000ff-0000-1000-8000-00805f9b34fb"; //蓝牙通讯服务 - public static final String READ_UUID = "0000ff01-0000-1000-8000-00805f9b34fb"; //读特征 - public static final String WRITE_UUID = "0000ff01-0000-1000-8000-00805f9b34fb"; //写特征 - BluetoothGattService service; - BluetoothGattCharacteristic read; - BluetoothGattCharacteristic write; - BluetoothGatt mGatt ; - - @SuppressLint("MissingPermission") - public void sendData(byte [] data){ - if (write!=null){ - L.d("jjia------send--------"); - write.setValue(data); - mGatt.writeCharacteristic(write); - } - - } -//00002b29-0000-1000-8000-00805f9b34fb // 写 - - - /** - * 设置读特征接收通知 - * @param enable 为true打开通知 - * @param gatt 连接 - * @param characteristic 特征 - */ - @SuppressLint("MissingPermission") - @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2) - public void enableNotification(boolean enable, BluetoothGatt gatt, BluetoothGattCharacteristic characteristic){ - if(gatt == null){ - L.i("jjia---------enableNotification-->gatt == null"); - return; - } - if(characteristic == null){ - L.i("jjia--------enableNotification-->characteristic == null"); - return; - } - //这一步必须要有,否则接收不到通知 - gatt.setCharacteristicNotification(characteristic,enable); - } - - - public static final String CLIENT_CHARACTERISTIC_CONFIG = "00002902-0000-1000-8000-00805f9b34fb"; - - @SuppressLint("MissingPermission") - private void setAutoReceiveData(BluetoothGatt gatt,BluetoothGattService linkLossService,BluetoothGattCharacteristic data,BluetoothGattDescriptor defaultDescriptor) { - try { -// BluetoothGattService linkLossService = gatt.getService(SERVICE_UUID); -// BluetoothGattCharacteristic data = linkLossService.getCharacteristic(CHARACTERISTIC_UUID); -// BluetoothGattDescriptor defaultDescriptor = data.getDescriptor(UUID.fromString(CLIENT_CHARACTERISTIC_CONFIG)); - if (null != defaultDescriptor) { - defaultDescriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); - gatt.writeDescriptor(defaultDescriptor); - } - gatt.setCharacteristicNotification(data, true); - } catch (Exception e) { -// BleLogUtils.appendLog("setAutoReceiveData:" + e.getMessage()); - } - } -} +// +// } +////00002b29-0000-1000-8000-00805f9b34fb // 写 +// +// +// /** +// * 设置读特征接收通知 +// * @param enable 为true打开通知 +// * @param gatt 连接 +// * @param characteristic 特征 +// */ +// @SuppressLint("MissingPermission") +// @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2) +// public void enableNotification(boolean enable, BluetoothGatt gatt, BluetoothGattCharacteristic characteristic){ +// if(gatt == null){ +// L.i("jjia---------enableNotification-->gatt == null"); +// return; +// } +// if(characteristic == null){ +// L.i("jjia--------enableNotification-->characteristic == null"); +// return; +// } +// //这一步必须要有,否则接收不到通知 +// gatt.setCharacteristicNotification(characteristic,enable); +// } +// +// +// public static final String CLIENT_CHARACTERISTIC_CONFIG = "00002902-0000-1000-8000-00805f9b34fb"; +// +// @SuppressLint("MissingPermission") +// private void setAutoReceiveData(BluetoothGatt gatt,BluetoothGattService linkLossService,BluetoothGattCharacteristic data,BluetoothGattDescriptor defaultDescriptor) { +// try { +//// BluetoothGattService linkLossService = gatt.getService(SERVICE_UUID); +//// BluetoothGattCharacteristic data = linkLossService.getCharacteristic(CHARACTERISTIC_UUID); +//// BluetoothGattDescriptor defaultDescriptor = data.getDescriptor(UUID.fromString(CLIENT_CHARACTERISTIC_CONFIG)); +// if (null != defaultDescriptor) { +// defaultDescriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); +// gatt.writeDescriptor(defaultDescriptor); +// } +// gatt.setCharacteristicNotification(data, true); +// } catch (Exception e) { +//// BleLogUtils.appendLog("setAutoReceiveData:" + e.getMessage()); +// } +// } +//}