|
@@ -1,13 +1,17 @@
|
|
|
-package cn.minbb.producttester.views;
|
|
|
+package cn.minbb.tester.activities;
|
|
|
|
|
|
import android.annotation.SuppressLint;
|
|
|
import android.app.ProgressDialog;
|
|
|
import android.bluetooth.BluetoothAdapter;
|
|
|
+import android.bluetooth.BluetoothDevice;
|
|
|
+import android.content.BroadcastReceiver;
|
|
|
import android.content.Context;
|
|
|
import android.content.Intent;
|
|
|
+import android.content.IntentFilter;
|
|
|
import android.os.Bundle;
|
|
|
import android.os.Handler;
|
|
|
import android.os.Message;
|
|
|
+import android.support.design.widget.FloatingActionButton;
|
|
|
import android.support.v7.app.AlertDialog;
|
|
|
import android.support.v7.app.AppCompatActivity;
|
|
|
import android.support.v7.app.AppCompatDelegate;
|
|
@@ -15,6 +19,7 @@ import android.support.v7.widget.LinearLayoutManager;
|
|
|
import android.support.v7.widget.RecyclerView;
|
|
|
import android.view.Menu;
|
|
|
import android.view.MenuItem;
|
|
|
+import android.view.MotionEvent;
|
|
|
import android.view.View;
|
|
|
import android.widget.Toast;
|
|
|
|
|
@@ -30,18 +35,28 @@ import java.util.regex.Pattern;
|
|
|
import butterknife.BindView;
|
|
|
import butterknife.ButterKnife;
|
|
|
import butterknife.OnClick;
|
|
|
-import cn.minbb.producttester.R;
|
|
|
-import cn.minbb.producttester.adapter.BasicDeviceAdapter;
|
|
|
-import cn.minbb.producttester.ctrl.App;
|
|
|
-import cn.minbb.producttester.ctrl.BluetoothChatService;
|
|
|
-import cn.minbb.producttester.utils.DataTranslation;
|
|
|
+import cn.minbb.tester.R;
|
|
|
+import cn.minbb.tester.adapter.BasicDeviceAdapter;
|
|
|
+import cn.minbb.tester.ctrl.App;
|
|
|
+import cn.minbb.tester.ctrl.BluetoothChatService;
|
|
|
+import cn.minbb.tester.utils.DataTranslation;
|
|
|
|
|
|
-public class BasicDeviceActivity extends AppCompatActivity {
|
|
|
+public class BasicDeviceActivity extends AppCompatActivity implements View.OnTouchListener {
|
|
|
|
|
|
@BindView(R.id.noDevice)
|
|
|
IconTextView noDevice;
|
|
|
@BindView(R.id.content)
|
|
|
RecyclerView contentRecyclerView;
|
|
|
+ @BindView(R.id.opUp)
|
|
|
+ FloatingActionButton opUp;
|
|
|
+ @BindView(R.id.opLeft)
|
|
|
+ FloatingActionButton opLeft;
|
|
|
+ @BindView(R.id.opCenter)
|
|
|
+ FloatingActionButton opCenter;
|
|
|
+ @BindView(R.id.opRight)
|
|
|
+ FloatingActionButton opRight;
|
|
|
+ @BindView(R.id.opDown)
|
|
|
+ FloatingActionButton opDown;
|
|
|
|
|
|
private ProgressDialog dialog;
|
|
|
|
|
@@ -57,12 +72,22 @@ public class BasicDeviceActivity extends AppCompatActivity {
|
|
|
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
|
|
|
}
|
|
|
|
|
|
+ @SuppressLint("ClickableViewAccessibility")
|
|
|
@Override
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
super.onCreate(savedInstanceState);
|
|
|
setContentView(R.layout.activity_basic_device);
|
|
|
ButterKnife.bind(this);
|
|
|
|
|
|
+ // 设置广播信息过滤
|
|
|
+ IntentFilter intentFilter = new IntentFilter();
|
|
|
+ intentFilter.addAction(BluetoothDevice.ACTION_FOUND);
|
|
|
+ intentFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
|
|
|
+ intentFilter.addAction(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);
|
|
|
+ intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
|
|
|
+ // 注册广播接收器,接收并处理搜索结果
|
|
|
+ this.registerReceiver(receiver, intentFilter);
|
|
|
+
|
|
|
App.setupActionBar(this);
|
|
|
noDevice.setVisibility(View.VISIBLE);
|
|
|
|
|
@@ -72,6 +97,11 @@ public class BasicDeviceActivity extends AppCompatActivity {
|
|
|
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
|
|
|
linearLayoutManager.setReverseLayout(false);
|
|
|
contentRecyclerView.setLayoutManager(linearLayoutManager);
|
|
|
+ opUp.setOnTouchListener(this);
|
|
|
+ opLeft.setOnTouchListener(this);
|
|
|
+ opCenter.setOnTouchListener(this);
|
|
|
+ opRight.setOnTouchListener(this);
|
|
|
+ opDown.setOnTouchListener(this);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -161,33 +191,72 @@ public class BasicDeviceActivity extends AppCompatActivity {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- @OnClick({R.id.noDevice, R.id.opUp, R.id.opLeft, R.id.opCenter, R.id.opRight, R.id.opDown})
|
|
|
+ @OnClick({R.id.noDevice})
|
|
|
public void onViewClicked(View view) {
|
|
|
switch (view.getId()) {
|
|
|
case R.id.noDevice:
|
|
|
startActivityForResult(new Intent(BasicDeviceActivity.this, ScanDeviceActivity.class), 0);
|
|
|
break;
|
|
|
+ default:
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean onTouch(View view, MotionEvent motionEvent) {
|
|
|
+ switch (view.getId()) {
|
|
|
case R.id.opUp:
|
|
|
- sendDataToDevice("UD\r\n", false);
|
|
|
- sendDataToDevice("UU\r\n", false);
|
|
|
+ switch (motionEvent.getAction()) {
|
|
|
+ case MotionEvent.ACTION_DOWN:
|
|
|
+ sendDataToDevice("UD\r\n", false);
|
|
|
+ break;
|
|
|
+ case MotionEvent.ACTION_UP:
|
|
|
+ sendDataToDevice("UU\r\n", false);
|
|
|
+ break;
|
|
|
+ }
|
|
|
break;
|
|
|
case R.id.opLeft:
|
|
|
- sendDataToDevice("LD\r\n", false);
|
|
|
- sendDataToDevice("LU\r\n", false);
|
|
|
+ switch (motionEvent.getAction()) {
|
|
|
+ case MotionEvent.ACTION_DOWN:
|
|
|
+ sendDataToDevice("LD\r\n", false);
|
|
|
+ break;
|
|
|
+ case MotionEvent.ACTION_UP:
|
|
|
+ sendDataToDevice("LU\r\n", false);
|
|
|
+ break;
|
|
|
+ }
|
|
|
break;
|
|
|
case R.id.opCenter:
|
|
|
- sendDataToDevice("SD\r\n", false);
|
|
|
- sendDataToDevice("SU\r\n", false);
|
|
|
+ switch (motionEvent.getAction()) {
|
|
|
+ case MotionEvent.ACTION_DOWN:
|
|
|
+ sendDataToDevice("SD\r\n", false);
|
|
|
+ break;
|
|
|
+ case MotionEvent.ACTION_UP:
|
|
|
+ sendDataToDevice("SU\r\n", false);
|
|
|
+ break;
|
|
|
+ }
|
|
|
break;
|
|
|
case R.id.opRight:
|
|
|
- sendDataToDevice("RD\r\n", false);
|
|
|
- sendDataToDevice("RU\r\n", false);
|
|
|
+ switch (motionEvent.getAction()) {
|
|
|
+ case MotionEvent.ACTION_DOWN:
|
|
|
+ sendDataToDevice("RD\r\n", false);
|
|
|
+ break;
|
|
|
+ case MotionEvent.ACTION_UP:
|
|
|
+ sendDataToDevice("RU\r\n", false);
|
|
|
+ break;
|
|
|
+ }
|
|
|
break;
|
|
|
case R.id.opDown:
|
|
|
- sendDataToDevice("DD\r\n", false);
|
|
|
- sendDataToDevice("DU\r\n", false);
|
|
|
+ switch (motionEvent.getAction()) {
|
|
|
+ case MotionEvent.ACTION_DOWN:
|
|
|
+ sendDataToDevice("DD\r\n", false);
|
|
|
+ break;
|
|
|
+ case MotionEvent.ACTION_UP:
|
|
|
+ sendDataToDevice("DU\r\n", false);
|
|
|
+ break;
|
|
|
+ }
|
|
|
break;
|
|
|
+ default:
|
|
|
}
|
|
|
+ return true;
|
|
|
}
|
|
|
|
|
|
private void showConnectDeviceProgress(boolean show) {
|
|
@@ -277,17 +346,6 @@ public class BasicDeviceActivity extends AppCompatActivity {
|
|
|
System.out.println("未匹配到数据");
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- //检错误码计算函数
|
|
|
-// if (inhex == true) {
|
|
|
-// String readMessage = " " + Data_syn.bytesToHexString(readBuf, msg.arg1);
|
|
|
-// fmsg += readMessage;
|
|
|
-// mConversationView.append(readMessage);
|
|
|
-// // 接收计数,更显UI
|
|
|
-// countin += readMessage.length() / 2;
|
|
|
-// incount.setText("" + countin);
|
|
|
-// } else if (inhex == false) {
|
|
|
-// }
|
|
|
break;
|
|
|
case BluetoothChatService.MESSAGE_DEVICE_NAME:
|
|
|
String connectedDeviceName = msg.getData().getString(BluetoothChatService.DEVICE_NAME);
|
|
@@ -300,4 +358,18 @@ public class BasicDeviceActivity extends AppCompatActivity {
|
|
|
}
|
|
|
}
|
|
|
};
|
|
|
+
|
|
|
+ private BroadcastReceiver receiver = new BroadcastReceiver() {
|
|
|
+ @Override
|
|
|
+ public void onReceive(Context context, Intent intent) {
|
|
|
+ String action = intent.getAction();
|
|
|
+ if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
|
|
|
+ new Handler().postDelayed(() -> runOnUiThread(() -> {
|
|
|
+ if (!bluetoothAdapter.isEnabled()) {
|
|
|
+ if (null != chatService) chatService.stop();
|
|
|
+ }
|
|
|
+ }), 500);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ };
|
|
|
}
|