Commit 1dfe522cff8cc2a893ec97bd1741c97afc12c535

Authored by Penley
1 parent 5b8ff3b5

control linght

... ... @@ -133,6 +133,13 @@
133 133 android:name="com.qnbar.smc.service.TelinkLightService"
134 134 android:enabled="true" />
135 135
  136 + <activity android:name="com.qnbar.smc.SimpleLightDemo">
  137 + <!-- <intent-filter>
  138 + <action android:name="android.intent.action.MAIN" />
  139 +
  140 + <category android:name="android.intent.category.LAUNCHER" />
  141 + </intent-filter>-->
  142 + </activity>
136 143 </application>
137 144
138 145 </manifest>
\ No newline at end of file
... ...
  1 +package com.qnbar.smc;
  2 +
  3 +import android.app.Activity;
  4 +import android.app.AlertDialog;
  5 +import android.bluetooth.BluetoothAdapter;
  6 +import android.content.BroadcastReceiver;
  7 +import android.content.Context;
  8 +import android.content.DialogInterface;
  9 +import android.content.Intent;
  10 +import android.content.IntentFilter;
  11 +import android.os.Bundle;
  12 +import android.os.Handler;
  13 +import android.util.Log;
  14 +import android.view.View;
  15 +import android.view.Window;
  16 +import android.widget.Toast;
  17 +
  18 +import com.qnbar.smc.model.Light;
  19 +import com.qnbar.smc.model.Lights;
  20 +import com.qnbar.smc.model.Mesh;
  21 +import com.qnbar.smc.service.TelinkLightService;
  22 +import com.telink.bluetooth.LeBluetooth;
  23 +import com.telink.bluetooth.TelinkLog;
  24 +import com.telink.bluetooth.event.DeviceEvent;
  25 +import com.telink.bluetooth.event.MeshEvent;
  26 +import com.telink.bluetooth.event.NotificationEvent;
  27 +import com.telink.bluetooth.event.ServiceEvent;
  28 +import com.telink.bluetooth.light.ConnectionStatus;
  29 +import com.telink.bluetooth.light.DeviceInfo;
  30 +import com.telink.bluetooth.light.LeAutoConnectParameters;
  31 +import com.telink.bluetooth.light.LeRefreshNotifyParameters;
  32 +import com.telink.bluetooth.light.LightAdapter;
  33 +import com.telink.bluetooth.light.OnlineStatusNotificationParser;
  34 +import com.telink.bluetooth.light.Parameters;
  35 +import com.telink.util.BuildUtils;
  36 +import com.telink.util.Event;
  37 +import com.telink.util.EventListener;
  38 +import com.xgimi.gimicinema.R;
  39 +import com.xgimi.gimicinema.application.FangTangApplication;
  40 +
  41 +import java.util.List;
  42 +
  43 +public class SimpleLightDemo extends Activity implements EventListener<String> {
  44 + private final static String TAG = SimpleLightDemo.class.getSimpleName();
  45 + private FangTangApplication mApplication;
  46 + private int connectMeshAddress;
  47 +
  48 + private Handler mDelayHandler = new Handler();
  49 +
  50 + private BroadcastReceiver mReceiver = new BroadcastReceiver() {
  51 + @Override
  52 + public void onReceive(Context context, Intent intent) {
  53 + String action = intent.getAction();
  54 + if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
  55 + int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 0);
  56 +
  57 + switch (state) {
  58 + case BluetoothAdapter.STATE_ON:
  59 + Log.d(TAG, "蓝牙开启");
  60 + TelinkLightService.Instance().idleMode(true);
  61 + autoConnect();
  62 + break;
  63 + case BluetoothAdapter.STATE_OFF:
  64 + Log.d(TAG, "蓝牙关闭");
  65 + break;
  66 + }
  67 + }
  68 + }
  69 + };
  70 +
  71 + @Override
  72 + protected void onCreate(Bundle savedInstanceState) {
  73 + super.onCreate(savedInstanceState);
  74 + mApplication = (FangTangApplication) getApplication();
  75 + mApplication.doInit();
  76 + mApplication.addEventListener(NotificationEvent.GET_GROUP, this);
  77 +
  78 + requestWindowFeature(Window.FEATURE_NO_TITLE);
  79 + setContentView(R.layout.activity_main);
  80 +
  81 + IntentFilter filter = new IntentFilter();
  82 + filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
  83 + filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY - 1);
  84 + registerReceiver(mReceiver, filter);
  85 + }
  86 +
  87 + @Override
  88 + protected void onStart() {
  89 + super.onStart();
  90 + Log.d(TAG, "onStart");
  91 + int result = BuildUtils.assetSdkVersion("4.4");
  92 + Log.d(TAG, " Version : " + result);
  93 +
  94 + // 监听各种事件
  95 + mApplication.addEventListener(DeviceEvent.STATUS_CHANGED, this);
  96 + mApplication.addEventListener(NotificationEvent.ONLINE_STATUS, this);
  97 + mApplication.addEventListener(ServiceEvent.SERVICE_CONNECTED, this);
  98 + mApplication.addEventListener(MeshEvent.OFFLINE, this);
  99 + mApplication.addEventListener(MeshEvent.ERROR, this);
  100 + new Handler().postDelayed(new Runnable() {
  101 + @Override
  102 + public void run() {
  103 + SimpleLightDemo.this.autoConnect();
  104 + }
  105 + }, 3 * 1000);
  106 +
  107 + }
  108 +
  109 + @Override
  110 + protected void onResume() {
  111 + super.onResume();
  112 + //检查是否支持蓝牙设备
  113 + if (!LeBluetooth.getInstance().isSupport(getApplicationContext())) {
  114 + Toast.makeText(this, "ble not support", Toast.LENGTH_SHORT).show();
  115 + finish();
  116 + return;
  117 + }
  118 +
  119 + if (!LeBluetooth.getInstance().isEnabled()) {
  120 + AlertDialog.Builder builder = new AlertDialog.Builder(this);
  121 + builder.setMessage("开启蓝牙,体验智能灯!");
  122 + builder.setNeutralButton("cancel", new DialogInterface.OnClickListener() {
  123 + @Override
  124 + public void onClick(DialogInterface dialog, int which) {
  125 + finish();
  126 + }
  127 + });
  128 + builder.setNegativeButton("enable", new DialogInterface.OnClickListener() {
  129 + @Override
  130 + public void onClick(DialogInterface dialog, int which) {
  131 + LeBluetooth.getInstance().enable(getApplicationContext());
  132 + }
  133 + });
  134 + builder.show();
  135 + }
  136 +
  137 + DeviceInfo deviceInfo = mApplication.getConnectDevice();
  138 +
  139 + if (deviceInfo != null) {
  140 + connectMeshAddress = mApplication.getConnectDevice().meshAddress & 0xFF;
  141 + }
  142 +
  143 + Log.d(TAG, "onResume");
  144 +
  145 + }
  146 +
  147 + @Override
  148 + protected void onStop() {
  149 + super.onStop();
  150 + Log.d(TAG, "onStop");
  151 + //移除事件
  152 + mApplication.removeEventListener(this);
  153 + TelinkLightService.Instance().disableAutoRefreshNotify();
  154 + }
  155 +
  156 + @Override
  157 + protected void onDestroy() {
  158 + super.onDestroy();
  159 + Log.d(TAG, "onDestroy");
  160 + unregisterReceiver(mReceiver);
  161 + mApplication.doDestroy();
  162 + mDelayHandler.removeCallbacksAndMessages(null);
  163 + Lights.getInstance().clear();
  164 + }
  165 +
  166 + /**
  167 + * 自动重连
  168 + */
  169 + private void autoConnect() {
  170 + if (TelinkLightService.Instance() != null) {
  171 + Log.d(TAG, "connect not null");
  172 + if (TelinkLightService.Instance().getMode() != LightAdapter.MODE_AUTO_CONNECT_MESH) {
  173 + Lights.getInstance().clear();
  174 + if (mApplication.isEmptyMesh())
  175 + return;
  176 +
  177 + Mesh mesh = mApplication.getMesh();
  178 + //自动重连参数
  179 + LeAutoConnectParameters connectParams = Parameters.createAutoConnectParameters();
  180 + connectParams.setMeshName(mesh.name);
  181 + connectParams.setPassword(mesh.password);
  182 + connectParams.autoEnableNotification(true);
  183 + //自动重连
  184 + TelinkLightService.Instance().autoConnect(connectParams);
  185 + } else {
  186 + Log.d(TAG, "connect null");
  187 + }
  188 +
  189 + //刷新Notify参数
  190 + LeRefreshNotifyParameters refreshNotifyParams = Parameters.createRefreshNotifyParameters();
  191 + refreshNotifyParams.setRefreshRepeatCount(2);
  192 + refreshNotifyParams.setRefreshInterval(2000);
  193 + //开启自动刷新Notify
  194 + TelinkLightService.Instance().autoRefreshNotify(refreshNotifyParams);
  195 + }
  196 + }
  197 +
  198 + private void onDeviceStatusChanged(DeviceEvent event) {
  199 + DeviceInfo deviceInfo = event.getArgs();
  200 + switch (deviceInfo.status) {
  201 + case LightAdapter.STATUS_LOGIN:
  202 + connectMeshAddress = mApplication.getConnectDevice().meshAddress;
  203 + show("login success");
  204 + break;
  205 + case LightAdapter.STATUS_CONNECTING:
  206 + show("login");
  207 + break;
  208 + case LightAdapter.STATUS_LOGOUT:
  209 + show("disconnect");
  210 + break;
  211 + default:
  212 + break;
  213 + }
  214 + }
  215 +
  216 + /**
  217 + * 处理{@link NotificationEvent#ONLINE_STATUS}事件
  218 + *
  219 + * @param event event bus trans
  220 + */
  221 + private void onOnlineStatusNotify(NotificationEvent event) {
  222 + TelinkLog.d("Thread ID : " + Thread.currentThread().getId());
  223 + List<OnlineStatusNotificationParser.DeviceNotificationInfo> notificationInfoList;
  224 + //noinspection unchecked
  225 + notificationInfoList = (List<OnlineStatusNotificationParser.DeviceNotificationInfo>) event.parse();
  226 +
  227 + if (notificationInfoList == null || notificationInfoList.size() <= 0)
  228 + return;
  229 +
  230 + for (OnlineStatusNotificationParser.DeviceNotificationInfo notificationInfo : notificationInfoList) {
  231 + int meshAddress = notificationInfo.meshAddress;
  232 + int brightness = notificationInfo.brightness;
  233 + Light light = Lights.getInstance().getByMeshAddress(meshAddress);
  234 + if (light == null) {
  235 + light = new Light();
  236 + Lights.getInstance().add(light);
  237 + }
  238 + light.meshAddress = meshAddress;
  239 + light.brightness = brightness;
  240 + light.status = notificationInfo.connectStatus;
  241 + light.updateIcon();
  242 + }
  243 + }
  244 +
  245 + private void onServiceConnected(ServiceEvent event) {
  246 + autoConnect();
  247 + }
  248 +
  249 + private void onServiceDisconnected(ServiceEvent event) {
  250 +
  251 + }
  252 +
  253 + private void onMeshOffline(MeshEvent event) {
  254 +
  255 + List<Light> lights = Lights.getInstance().get();
  256 + for (Light light : lights) {
  257 + light.status = ConnectionStatus.OFFLINE;
  258 + light.updateIcon();
  259 + }
  260 + }
  261 +
  262 + private void onMeshError(MeshEvent event) {
  263 + new AlertDialog.Builder(this).setMessage("蓝牙出问题了,重启蓝牙试试!!").show();
  264 + }
  265 +
  266 + /**
  267 + * 事件处理方法
  268 + *
  269 + * @param event
  270 + */
  271 + @Override
  272 + public void performed(Event<String> event) {
  273 + switch (event.getType()) {
  274 + case NotificationEvent.ONLINE_STATUS:
  275 + onOnlineStatusNotify((NotificationEvent) event);
  276 + break;
  277 + case DeviceEvent.STATUS_CHANGED:
  278 + onDeviceStatusChanged((DeviceEvent) event);
  279 + break;
  280 + case MeshEvent.OFFLINE:
  281 + onMeshOffline((MeshEvent) event);
  282 + break;
  283 + case MeshEvent.ERROR:
  284 + onMeshError((MeshEvent) event);
  285 + break;
  286 + case ServiceEvent.SERVICE_CONNECTED:
  287 + onServiceConnected((ServiceEvent) event);
  288 + break;
  289 + case ServiceEvent.SERVICE_DISCONNECTED:
  290 + onServiceDisconnected((ServiceEvent) event);
  291 + break;
  292 + }
  293 + }
  294 +
  295 + public void openLight(View view) {
  296 + byte opcode = (byte) 0xD0;
  297 + int address = 0xFFFF;
  298 + byte[] params = new byte[]{0x01, 0x00, 0x00};
  299 + if (TelinkLightService.Instance().sendCommandNoResponse(opcode, address, params)) {
  300 + show("open all success");
  301 + }
  302 + }
  303 +
  304 + public void closeLight(View view) {
  305 + byte opcode = (byte) 0xD0;
  306 + int address = 0xFFFF;
  307 + byte[] params = new byte[]{0x00, 0x00, 0x00};
  308 + if (TelinkLightService.Instance().sendCommandNoResponse(opcode, address, params)) {
  309 + show("close all success");
  310 + }
  311 + }
  312 +
  313 + private void show(String msg) {
  314 + Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
  315 + }
  316 +
  317 +}
... ...
... ... @@ -16,6 +16,8 @@
16 16 package com.xgimi.gimicinema.activity;
17 17
18 18 import android.app.AlertDialog;
  19 +import android.bluetooth.BluetoothAdapter;
  20 +import android.content.BroadcastReceiver;
19 21 import android.content.Context;
20 22 import android.content.DialogInterface;
21 23 import android.content.Intent;
... ... @@ -30,6 +32,7 @@ import android.os.Handler;
30 32 import android.support.v7.widget.GridLayoutManager;
31 33 import android.support.v7.widget.RecyclerView;
32 34 import android.text.TextUtils;
  35 +import android.util.Log;
33 36 import android.view.KeyEvent;
34 37 import android.view.MotionEvent;
35 38 import android.view.View;
... ... @@ -55,8 +58,29 @@ import com.gimi.common.cinema.utils.M1905Utils;
55 58 import com.gimi.common.cinema.utils.SambaFileCharge;
56 59 import com.gimi.common.cinema.utils.ScreenUtils;
57 60 import com.gimi.common.cinema.utils.SystemUtils;
  61 +import com.qnbar.smc.model.Light;
  62 +import com.qnbar.smc.model.Lights;
  63 +import com.qnbar.smc.model.Mesh;
  64 +import com.qnbar.smc.service.TelinkLightService;
  65 +import com.telink.bluetooth.LeBluetooth;
  66 +import com.telink.bluetooth.TelinkLog;
  67 +import com.telink.bluetooth.event.DeviceEvent;
  68 +import com.telink.bluetooth.event.MeshEvent;
  69 +import com.telink.bluetooth.event.NotificationEvent;
  70 +import com.telink.bluetooth.event.ServiceEvent;
  71 +import com.telink.bluetooth.light.ConnectionStatus;
  72 +import com.telink.bluetooth.light.DeviceInfo;
  73 +import com.telink.bluetooth.light.LeAutoConnectParameters;
  74 +import com.telink.bluetooth.light.LeRefreshNotifyParameters;
  75 +import com.telink.bluetooth.light.LightAdapter;
  76 +import com.telink.bluetooth.light.OnlineStatusNotificationParser;
  77 +import com.telink.bluetooth.light.Parameters;
  78 +import com.telink.util.BuildUtils;
  79 +import com.telink.util.Event;
  80 +import com.telink.util.EventListener;
58 81 import com.xgimi.gimicinema.BuildConfig;
59 82 import com.xgimi.gimicinema.R;
  83 +import com.xgimi.gimicinema.application.FangTangApplication;
60 84 import com.xgimi.gimicinema.mview.IMainView;
61 85 import com.xgimi.gimicinema.presenter.MainPresenter;
62 86 import com.xgimi.gimicinema.view.ClazzItem;
... ... @@ -72,8 +96,9 @@ import java.util.Locale;
72 96 /**
73 97 * Created by wugian on 2016/9/23
74 98 */
75   -public class MainActivity extends BaseActivity implements IMainView {
  99 +public class MainActivity extends BaseActivity implements IMainView, EventListener<String> {
76 100
  101 + private static final String TAG = "MainActivity";
77 102 private static final long NO_KEY_EVENT_MSEC = 30 * 1000;
78 103
79 104 private Context context;
... ... @@ -125,7 +150,11 @@ public class MainActivity extends BaseActivity implements IMainView {
125 150 @Override
126 151 protected void onCreate(Bundle savedInstanceState) {
127 152 super.onCreate(savedInstanceState);
  153 + mApplication = (FangTangApplication) getApplication();
  154 + mApplication.doInit();
128 155 setContentView(R.layout.a_main);
  156 + initLight();
  157 +// initLock();
129 158 context = this;
130 159 mData = new ArrayList<>();
131 160 clazzData = new ArrayList<>();
... ... @@ -293,7 +322,8 @@ public class MainActivity extends BaseActivity implements IMainView {
293 322
294 323 @Override
295 324 public void jumpToSearch() {
296   - Intent intentSearch = new Intent(context, SearchNewActivity.class);
  325 + Intent intentSearch = new Intent(context, com.qnbar.smc.MainActivity.class);
  326 +// Intent intentSearch = new Intent(context, SearchNewActivity.class);
297 327 context.startActivity(intentSearch);
298 328 }
299 329
... ... @@ -328,13 +358,46 @@ public class MainActivity extends BaseActivity implements IMainView {
328 358 handler.removeCallbacks(startAdsRunnable);
329 359 handler.postDelayed(startAdsRunnable, 30 * 1000);
330 360 }
331   - if(HAS_SYSTEM_FEATURE_BLUETOOTH_LE){
  361 + if (HAS_SYSTEM_FEATURE_BLUETOOTH_LE) {
332 362 IntentFilter intentFilter = new IntentFilter();
333 363 intentFilter.addAction(BLEBroadcastReceiver.ACTION_BLE_ERROR);
334 364 intentFilter.addAction(BLEBroadcastReceiver.ACTION_SHAKING_BLE_ERROR);
335 365 intentFilter.addAction(BLEBroadcastReceiver.ACTION_WRITTEN_SUCCESS);
336 366 registerReceiver(bleBroadcastReceiver, intentFilter);
337 367 }
  368 + if (!LeBluetooth.getInstance().isSupport(getApplicationContext())) {
  369 + Toast.makeText(this, "ble not support", Toast.LENGTH_SHORT).show();
  370 + finish();
  371 + return;
  372 + }
  373 +
  374 + if (!LeBluetooth.getInstance().isEnabled()) {
  375 + AlertDialog.Builder builder = new AlertDialog.Builder(this);
  376 + builder.setMessage("开启蓝牙,体验智能灯!");
  377 + builder.setNeutralButton("cancel", new DialogInterface.OnClickListener() {
  378 + @Override
  379 + public void onClick(DialogInterface dialog, int which) {
  380 + finish();
  381 + }
  382 + });
  383 + builder.setNegativeButton("enable", new DialogInterface.OnClickListener() {
  384 + @Override
  385 + public void onClick(DialogInterface dialog, int which) {
  386 + LeBluetooth.getInstance().enable(getApplicationContext());
  387 + }
  388 + });
  389 + builder.show();
  390 + }
  391 +
  392 + DeviceInfo deviceInfo = mApplication.getConnectDevice();
  393 +
  394 + if (deviceInfo != null) {
  395 + connectMeshAddress = mApplication.getConnectDevice().meshAddress & 0xFF;
  396 + } else {
  397 + autoConnect();
  398 + }
  399 +
  400 + Log.d(TAG, "onResume");
338 401 }
339 402
340 403 @Override
... ... @@ -364,7 +427,7 @@ public class MainActivity extends BaseActivity implements IMainView {
364 427 protected void onPause() {
365 428 needStartScreenSaver = false;
366 429 super.onPause();
367   - if(HAS_SYSTEM_FEATURE_BLUETOOTH_LE){
  430 + if (HAS_SYSTEM_FEATURE_BLUETOOTH_LE) {
368 431 unregisterReceiver(bleBroadcastReceiver);
369 432 }
370 433 }
... ... @@ -557,6 +620,9 @@ public class MainActivity extends BaseActivity implements IMainView {
557 620 if (!Constant.gimiAuth) {
558 621 return;
559 622 }
  623 + if (true) {
  624 + return;
  625 + }
560 626 if (AuthUtils.isFinish(context)) {
561 627 return;
562 628 }
... ... @@ -581,9 +647,11 @@ public class MainActivity extends BaseActivity implements IMainView {
581 647 protected void onDestroy() {
582 648 super.onDestroy();
583 649 presenter.umountSamba();
  650 + unregisterReceiver(mReceiver);
584 651 }
585 652
586 653
  654 + //lock
587 655 private String openCMD = "Open the door";
588 656 private String onClickDeviceMac;
589 657
... ... @@ -626,7 +694,216 @@ public class MainActivity extends BaseActivity implements IMainView {
626 694 GREENBLE.send(this, "DC:F6:70:C1:AA:D6", openCMD.getBytes());
627 695 }
628 696
629   - public void initLight(){
  697 + @Override
  698 + protected void onStart() {
  699 + super.onStart();
  700 + Log.d(TAG, "onStart");
  701 + int result = BuildUtils.assetSdkVersion("4.4");
  702 + Log.d(TAG, " Version : " + result);
  703 +
  704 + // 监听各种事件
  705 + mApplication.addEventListener(DeviceEvent.STATUS_CHANGED, this);
  706 + mApplication.addEventListener(NotificationEvent.ONLINE_STATUS, this);
  707 + mApplication.addEventListener(ServiceEvent.SERVICE_CONNECTED, this);
  708 + mApplication.addEventListener(MeshEvent.OFFLINE, this);
  709 + mApplication.addEventListener(MeshEvent.ERROR, this);
  710 + new Handler().postDelayed(new Runnable() {
  711 + @Override
  712 + public void run() {
  713 + MainActivity.this.autoConnect();
  714 + }
  715 + }, 3 * 1000);
  716 + }
  717 +
  718 + //lights
  719 +
  720 + private FangTangApplication mApplication;
  721 + private int connectMeshAddress;
  722 + private Handler mHandler = new Handler();
  723 +
  724 + private Handler mDelayHandler = new Handler();
  725 +
  726 + private BroadcastReceiver mReceiver = new BroadcastReceiver() {
  727 + @Override
  728 + public void onReceive(Context context, Intent intent) {
  729 + String action = intent.getAction();
  730 + if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
  731 + int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 0);
  732 +
  733 + switch (state) {
  734 + case BluetoothAdapter.STATE_ON:
  735 + Log.d(TAG, "蓝牙开启");
  736 + TelinkLightService.Instance().idleMode(true);
  737 + autoConnect();
  738 + break;
  739 + case BluetoothAdapter.STATE_OFF:
  740 + Log.d(TAG, "蓝牙关闭");
  741 + break;
  742 + }
  743 + }
  744 + }
  745 + };
  746 +
  747 + /**
  748 + * 自动重连
  749 + */
  750 + private void autoConnect() {
  751 +
  752 + if (TelinkLightService.Instance() != null) {
  753 + Log.d(TAG, "connect not null");
  754 +
  755 + if (TelinkLightService.Instance().getMode() != LightAdapter.MODE_AUTO_CONNECT_MESH) {
  756 +
  757 + Lights.getInstance().clear();
  758 +
  759 + if (mApplication.isEmptyMesh())
  760 + return;
  761 +
  762 + Mesh mesh = mApplication.getMesh();
  763 +
  764 + //自动重连参数
  765 + LeAutoConnectParameters connectParams = Parameters.createAutoConnectParameters();
  766 + connectParams.setMeshName(mesh.name);
  767 + connectParams.setPassword(mesh.password);
  768 + connectParams.autoEnableNotification(true);
  769 + //自动重连
  770 + TelinkLightService.Instance().autoConnect(connectParams);
  771 + } else {
  772 + Log.d(TAG, "connect null");
  773 + }
  774 +
  775 + //刷新Notify参数
  776 + LeRefreshNotifyParameters refreshNotifyParams = Parameters.createRefreshNotifyParameters();
  777 + refreshNotifyParams.setRefreshRepeatCount(2);
  778 + refreshNotifyParams.setRefreshInterval(2000);
  779 + //开启自动刷新Notify
  780 + TelinkLightService.Instance().autoRefreshNotify(refreshNotifyParams);
  781 + }
  782 + }
  783 +
  784 +
  785 + private void show(String msg) {
  786 + Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
  787 + }
  788 +
  789 + private void onDeviceStatusChanged(DeviceEvent event) {
  790 +
  791 + DeviceInfo deviceInfo = event.getArgs();
  792 +
  793 + switch (deviceInfo.status) {
  794 + case LightAdapter.STATUS_LOGIN:
  795 + connectMeshAddress = mApplication.getConnectDevice().meshAddress;
  796 + show("main login success");
  797 + mHandler.postDelayed(new Runnable() {
  798 + @Override
  799 + public void run() {
  800 + TelinkLightService.Instance().sendCommandNoResponse((byte) 0xE4, 0xFFFF, new byte[]{});
  801 +// getDeviceGroup();
  802 + }
  803 + }, 3 * 1000);
  804 + break;
  805 + case LightAdapter.STATUS_CONNECTING:
  806 + show("login");
  807 + break;
  808 + case LightAdapter.STATUS_LOGOUT:
  809 + show("disconnect");
  810 + break;
  811 + default:
  812 + break;
  813 + }
  814 + }
  815 +
  816 + private void onServiceConnected(ServiceEvent event) {
  817 + autoConnect();
  818 + }
  819 +
  820 + private void onServiceDisconnected(ServiceEvent event) {
  821 +
  822 + }
  823 +
  824 + private void onMeshOffline(MeshEvent event) {
  825 +
  826 + List<Light> lights = Lights.getInstance().get();
  827 + for (Light light : lights) {
  828 + light.status = ConnectionStatus.OFFLINE;
  829 + light.updateIcon();
  830 + }
  831 + adapter.notifyDataSetChanged();
  832 +// deviceFragment.notifyDataSetChanged();
  833 + }
  834 +
  835 + private void onMeshError(MeshEvent event) {
  836 + new AlertDialog.Builder(this).setMessage("蓝牙出问题了,重启蓝牙试试!!").show();
  837 + }
  838 +
  839 + /**
  840 + * 处理{@link NotificationEvent#ONLINE_STATUS}事件
  841 + *
  842 + * @param event event bus trans
  843 + */
  844 + private void onOnlineStatusNotify(NotificationEvent event) {
  845 + TelinkLog.d("Thread ID : " + Thread.currentThread().getId());
  846 + List<OnlineStatusNotificationParser.DeviceNotificationInfo> notificationInfoList;
  847 + //noinspection unchecked
  848 + notificationInfoList = (List<OnlineStatusNotificationParser.DeviceNotificationInfo>) event.parse();
  849 +
  850 + if (notificationInfoList == null || notificationInfoList.size() <= 0)
  851 + return;
  852 +
  853 + for (OnlineStatusNotificationParser.DeviceNotificationInfo notificationInfo : notificationInfoList) {
  854 + int meshAddress = notificationInfo.meshAddress;
  855 + int brightness = notificationInfo.brightness;
  856 + Light light = Lights.getInstance().getByMeshAddress(meshAddress);
  857 + if (light == null) {
  858 + light = new Light();
  859 + Lights.getInstance().add(light);
  860 + }
  861 + light.meshAddress = meshAddress;
  862 + light.brightness = brightness;
  863 + light.status = notificationInfo.connectStatus;
  864 + light.updateIcon();
  865 + }
  866 + mHandler.post(new Runnable() {
  867 + @Override
  868 + public void run() {
  869 + adapter.notifyDataSetChanged();
  870 + }
  871 + });
  872 + }
  873 +
  874 + /**
  875 + * 事件处理方法
  876 + *
  877 + * @param event
  878 + */
  879 + @Override
  880 + public void performed(Event<String> event) {
  881 + switch (event.getType()) {
  882 + case NotificationEvent.ONLINE_STATUS:
  883 + onOnlineStatusNotify((NotificationEvent) event);
  884 + break;
  885 + case DeviceEvent.STATUS_CHANGED:
  886 + onDeviceStatusChanged((DeviceEvent) event);
  887 + break;
  888 + case MeshEvent.OFFLINE:
  889 + onMeshOffline((MeshEvent) event);
  890 + break;
  891 + case MeshEvent.ERROR:
  892 + onMeshError((MeshEvent) event);
  893 + break;
  894 + case ServiceEvent.SERVICE_CONNECTED:
  895 + onServiceConnected((ServiceEvent) event);
  896 + break;
  897 + case ServiceEvent.SERVICE_DISCONNECTED:
  898 + onServiceDisconnected((ServiceEvent) event);
  899 + break;
  900 + }
  901 + }
630 902
  903 + public void initLight() {
  904 + IntentFilter filter = new IntentFilter();
  905 + filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
  906 + filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY - 1);
  907 + registerReceiver(mReceiver, filter);
631 908 }
632 909 }
... ...
... ... @@ -135,13 +135,13 @@ public class MovieListsActivity extends BaseActivity implements IMovieListView {
135 135 OnRecyclerViewItemClickListener listener = new OnRecyclerViewItemClickListener() {
136 136 @Override
137 137 public void onItemClick(View view, int data) {
138   - MessageEvent messageEvent = new MessageEvent();
139   - messageEvent.setEventId(1);
140   - messageEvent.setMessage("click item");
141   - EventBus.getDefault().post(messageEvent);
142   -// clickPosition = data;
143   -// LocalMovieMessage localMovieMessage = mData.get(data);
144   -// presenter.jumpToDetail(localMovieMessage);
  138 +// MessageEvent messageEvent = new MessageEvent();
  139 +// messageEvent.setEventId(1);
  140 +// messageEvent.setMessage("click item");
  141 +// EventBus.getDefault().post(messageEvent);
  142 + clickPosition = data;
  143 + LocalMovieMessage localMovieMessage = mData.get(data);
  144 + presenter.jumpToDetail(localMovieMessage);
145 145 }
146 146 };
147 147
... ...
... ... @@ -25,6 +25,8 @@ import android.content.ServiceConnection;
25 25 import android.os.IBinder;
26 26 import android.os.RemoteException;
27 27 import android.util.Log;
  28 +
  29 +import com.qnbar.smc.service.TelinkLightService;
28 30 import com.xgimi.gimicinema.ICinemaControl;
29 31 import com.xgimi.gimicinema.ICinemaSMC;
30 32
... ... @@ -79,26 +81,17 @@ public class CinemaControlService extends Service {
79 81 if (state != currentState) {
80 82 currentState = state;
81 83 Log.d("aidl", "state change," + duration + "," + currentPosition);
82   - try {
83   - if (cinemaSMC != null) {
84   - Log.d("aidl", "state change cinemaSMC not null");
85   - switch (state) {
86   - case 0:
87   - cinemaSMC.open();
88   - break;
89   - case 1:
90   - cinemaSMC.close();
91   - break;
92   - case 2:
93   - cinemaSMC.open();
94   - break;
95   - }
96   - } else {
97   - Log.d("aidl", "state change cinemaSMC null");
98   - }
99   - } catch (RemoteException e) {
100   - Log.d("aidl", "RemoteException:" + e.getMessage());
101   - e.printStackTrace();
  84 + Log.d("aidl", "state change cinemaSMC not null");
  85 + switch (state) {
  86 + case 0:
  87 + open();
  88 + break;
  89 + case 1:
  90 + close();
  91 + break;
  92 + case 2:
  93 + open();
  94 + break;
102 95 }
103 96 }
104 97 }
... ... @@ -205,4 +198,23 @@ public class CinemaControlService extends Service {
205 198 return isRunning;
206 199 }
207 200 }
  201 +
  202 + private void open() {
  203 + byte opcode = (byte) 0xD0;
  204 + int address = 0xFFFF;
  205 + byte[] params = new byte[]{0x01, 0x00, 0x00};
  206 + if (TelinkLightService.Instance().sendCommandNoResponse(opcode, address, params)) {
  207 +// show("open all success");
  208 + }
  209 + }
  210 +
  211 + private void close() {
  212 + byte opcode = (byte) 0xD0;
  213 + int address = 0xFFFF;
  214 + byte[] params = new byte[]{0x00, 0x00, 0x00};
  215 + if (TelinkLightService.Instance().sendCommandNoResponse(opcode, address, params)) {
  216 +// show("open all success");
  217 + }
  218 +
  219 + }
208 220 }
... ...
Please register or login to post a comment