|
@@ -0,0 +1,227 @@
|
|
|
+package cn.minbb.producttester.views;
|
|
|
+
|
|
|
+import android.annotation.SuppressLint;
|
|
|
+import android.app.ProgressDialog;
|
|
|
+import android.bluetooth.BluetoothAdapter;
|
|
|
+import android.content.Context;
|
|
|
+import android.content.Intent;
|
|
|
+import android.os.Bundle;
|
|
|
+import android.os.Handler;
|
|
|
+import android.os.Message;
|
|
|
+import android.support.v7.app.AlertDialog;
|
|
|
+import android.support.v7.app.AppCompatActivity;
|
|
|
+import android.support.v7.app.AppCompatDelegate;
|
|
|
+import android.view.Menu;
|
|
|
+import android.view.MenuItem;
|
|
|
+import android.view.View;
|
|
|
+import android.widget.TextView;
|
|
|
+import android.widget.Toast;
|
|
|
+
|
|
|
+import com.joanzapata.iconify.widget.IconTextView;
|
|
|
+
|
|
|
+import java.io.UnsupportedEncodingException;
|
|
|
+import java.util.regex.Matcher;
|
|
|
+import java.util.regex.Pattern;
|
|
|
+
|
|
|
+import butterknife.BindView;
|
|
|
+import butterknife.ButterKnife;
|
|
|
+import butterknife.OnClick;
|
|
|
+import cn.minbb.producttester.R;
|
|
|
+import cn.minbb.producttester.ctrl.App;
|
|
|
+import cn.minbb.producttester.ctrl.BluetoothChatService;
|
|
|
+
|
|
|
+public class DeviceBasicActivity extends AppCompatActivity {
|
|
|
+
|
|
|
+ @BindView(R.id.noDevice)
|
|
|
+ IconTextView noDevice;
|
|
|
+ @BindView(R.id.t1)
|
|
|
+ TextView t1;
|
|
|
+ @BindView(R.id.t2)
|
|
|
+ TextView t2;
|
|
|
+ @BindView(R.id.t3)
|
|
|
+ TextView t3;
|
|
|
+ @BindView(R.id.t4)
|
|
|
+ TextView t4;
|
|
|
+ @BindView(R.id.t5)
|
|
|
+ TextView t5;
|
|
|
+ @BindView(R.id.t6)
|
|
|
+ TextView t6;
|
|
|
+
|
|
|
+ private ProgressDialog dialog;
|
|
|
+
|
|
|
+ private BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
|
|
|
+ private BluetoothChatService chatService = null;
|
|
|
+
|
|
|
+ static {
|
|
|
+ AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onCreate(Bundle savedInstanceState) {
|
|
|
+ super.onCreate(savedInstanceState);
|
|
|
+ setContentView(R.layout.activity_device_basic);
|
|
|
+ ButterKnife.bind(this);
|
|
|
+
|
|
|
+ App.setupActionBar(this);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onResume() {
|
|
|
+ super.onResume();
|
|
|
+ if (chatService != null) {
|
|
|
+ // 只有状态是 STATE_NONE,我们知道我们还没有启动蓝牙
|
|
|
+ if (chatService.getState() == BluetoothChatService.STATE_NONE) {
|
|
|
+ // 启动 BluetoothChat 服务
|
|
|
+ chatService.start();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onDestroy() {
|
|
|
+ super.onDestroy();
|
|
|
+ mHandler.removeCallbacksAndMessages(null);
|
|
|
+ // 停止蓝牙通信连接服务
|
|
|
+ if (chatService != null) chatService.stop();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void start(Context context) {
|
|
|
+ Intent starter = new Intent(context, DeviceBasicActivity.class);
|
|
|
+ context.startActivity(starter);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean onCreateOptionsMenu(Menu menu) {
|
|
|
+ return super.onCreateOptionsMenu(menu);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
+ switch (item.getItemId()) {
|
|
|
+ case android.R.id.home:
|
|
|
+ AlertDialog.Builder builder = new AlertDialog.Builder(DeviceBasicActivity.this);
|
|
|
+ builder.setTitle("退出")
|
|
|
+ .setMessage("返回将断开设备的蓝牙连接")
|
|
|
+ .setNegativeButton("取消", null)
|
|
|
+ .setPositiveButton("退出", (dialog, which) -> finish())
|
|
|
+ .create()
|
|
|
+ .show();
|
|
|
+ default:
|
|
|
+ }
|
|
|
+ return super.onOptionsItemSelected(item);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
|
|
+ super.onActivityResult(requestCode, resultCode, data);
|
|
|
+ switch (requestCode) {
|
|
|
+ case 0:
|
|
|
+ if (resultCode == RESULT_OK) {
|
|
|
+ // 初始化蓝牙连接服务
|
|
|
+ chatService = new BluetoothChatService(this, mHandler);
|
|
|
+ // 获取设备的 MAC 地址
|
|
|
+ String address = data.getExtras().getString(BluetoothChatService.EXTRA_DEVICE_ADDRESS);
|
|
|
+ // 尝试连接到设备
|
|
|
+ chatService.connect(bluetoothAdapter.getRemoteDevice(address));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @OnClick({R.id.noDevice, R.id.opUp, R.id.opDown, R.id.opLeft, R.id.opRight})
|
|
|
+ public void onViewClicked(View view) {
|
|
|
+ switch (view.getId()) {
|
|
|
+ case R.id.noDevice:
|
|
|
+ startActivityForResult(new Intent(DeviceBasicActivity.this, ScanDeviceActivity.class), 0);
|
|
|
+ break;
|
|
|
+ case R.id.opUp:
|
|
|
+ break;
|
|
|
+ case R.id.opDown:
|
|
|
+ break;
|
|
|
+ case R.id.opLeft:
|
|
|
+ break;
|
|
|
+ case R.id.opRight:
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void showConnectDeviceProgress(boolean show) {
|
|
|
+ if (dialog == null) {
|
|
|
+ dialog = new ProgressDialog(this);
|
|
|
+ dialog.setTitle("连接");
|
|
|
+ dialog.setMessage("设备连接中······");
|
|
|
+ dialog.setCancelable(false);
|
|
|
+ }
|
|
|
+ if (show) {
|
|
|
+ dialog.show();
|
|
|
+ } else {
|
|
|
+ dialog.dismiss();
|
|
|
+ dialog = null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @SuppressLint("HandlerLeak")
|
|
|
+ private final Handler mHandler = new Handler() {
|
|
|
+ @Override
|
|
|
+ public void handleMessage(Message msg) {
|
|
|
+ switch (msg.what) {
|
|
|
+ case BluetoothChatService.MESSAGE_STATE_CHANGE:
|
|
|
+ System.err.println("MESSAGE_STATE_CHANGE = " + msg.arg1);
|
|
|
+ switch (msg.arg1) {
|
|
|
+ case BluetoothChatService.STATE_CONNECTED:
|
|
|
+ // 已连接
|
|
|
+ showConnectDeviceProgress(false);
|
|
|
+ noDevice.setVisibility(View.GONE);
|
|
|
+ break;
|
|
|
+ case BluetoothChatService.STATE_CONNECTING:
|
|
|
+ showConnectDeviceProgress(true);
|
|
|
+ break;
|
|
|
+ case BluetoothChatService.STATE_LISTEN:
|
|
|
+ showConnectDeviceProgress(false);
|
|
|
+ break;
|
|
|
+ case BluetoothChatService.STATE_NONE:
|
|
|
+ // 无连接
|
|
|
+ showConnectDeviceProgress(false);
|
|
|
+ noDevice.setVisibility(View.VISIBLE);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case BluetoothChatService.MESSAGE_READ:
|
|
|
+ byte[] readBuf = (byte[]) msg.obj;
|
|
|
+ String readMessage = null;
|
|
|
+ try {
|
|
|
+ readMessage = new String(readBuf, 0, msg.arg1, "GBK");
|
|
|
+ } catch (UnsupportedEncodingException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ System.err.println("/" + readMessage);
|
|
|
+
|
|
|
+ Pattern p = Pattern.compile("\\{.*?\\}");
|
|
|
+ Matcher m = p.matcher(readMessage);
|
|
|
+ // 判断正则表达式是否匹配到
|
|
|
+ if (m.find()) {
|
|
|
+ System.out.println("->" + m.group());
|
|
|
+ } else {
|
|
|
+ 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);
|
|
|
+ Toast.makeText(getApplicationContext(), "连接到 " + connectedDeviceName, Toast.LENGTH_SHORT).show();
|
|
|
+ break;
|
|
|
+ case BluetoothChatService.MESSAGE_TOAST:
|
|
|
+ Toast.makeText(getApplicationContext(), msg.getData().getString(BluetoothChatService.TOAST), Toast.LENGTH_SHORT).show();
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ };
|
|
|
+}
|