|
@@ -1,9 +1,9 @@
|
|
|
package cn.minbb.producttester.views;
|
|
|
|
|
|
+import android.annotation.SuppressLint;
|
|
|
import android.app.ProgressDialog;
|
|
|
import android.bluetooth.BluetoothAdapter;
|
|
|
import android.bluetooth.BluetoothDevice;
|
|
|
-import android.bluetooth.BluetoothSocket;
|
|
|
import android.content.BroadcastReceiver;
|
|
|
import android.content.Context;
|
|
|
import android.content.DialogInterface;
|
|
@@ -13,30 +13,27 @@ import android.content.pm.PackageManager;
|
|
|
import android.os.Build;
|
|
|
import android.os.Bundle;
|
|
|
import android.os.Handler;
|
|
|
+import android.os.Message;
|
|
|
import android.support.annotation.NonNull;
|
|
|
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.AdapterView;
|
|
|
import android.widget.ListView;
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
-import java.io.IOException;
|
|
|
import java.lang.reflect.Method;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
import java.util.Set;
|
|
|
-import java.util.UUID;
|
|
|
-import java.util.function.Consumer;
|
|
|
|
|
|
import butterknife.BindView;
|
|
|
import butterknife.ButterKnife;
|
|
|
import cn.minbb.producttester.R;
|
|
|
import cn.minbb.producttester.adapter.DeviceListAdapter;
|
|
|
import cn.minbb.producttester.ctrl.App;
|
|
|
+import cn.minbb.producttester.ctrl.BluetoothChatService;
|
|
|
|
|
|
public class ScanDeviceActivity extends AppCompatActivity {
|
|
|
|
|
@@ -45,12 +42,14 @@ public class ScanDeviceActivity extends AppCompatActivity {
|
|
|
|
|
|
private ProgressDialog dialog;
|
|
|
|
|
|
- private BluetoothAdapter adapter = App.getApp().getBluetoothAdapter();
|
|
|
+ private BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
|
|
|
private List<DeviceListAdapter.ItemBean> deviceItemBeanList = new ArrayList<>();
|
|
|
private DeviceListAdapter deviceListAdapter;
|
|
|
private BluetoothDevice selectDevice;
|
|
|
private int selectIndex;
|
|
|
|
|
|
+ private BluetoothChatService chatService = null;
|
|
|
+
|
|
|
static {
|
|
|
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
|
|
|
}
|
|
@@ -71,18 +70,18 @@ public class ScanDeviceActivity extends AppCompatActivity {
|
|
|
this.registerReceiver(receiver, intentFilter);
|
|
|
|
|
|
// 检查设备是否支持蓝牙
|
|
|
- if (adapter == null) {
|
|
|
+ if (bluetoothAdapter == null) {
|
|
|
// 设备不支持蓝牙
|
|
|
Toast.makeText(ScanDeviceActivity.this, "设备不支持蓝牙", Toast.LENGTH_LONG).show();
|
|
|
ScanDeviceActivity.this.finish();
|
|
|
} else {
|
|
|
- if (!adapter.isEnabled()) {
|
|
|
+ if (!bluetoothAdapter.isEnabled()) {
|
|
|
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
|
|
|
// 设置蓝牙可见性,最多 300 秒
|
|
|
intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 60);
|
|
|
ScanDeviceActivity.this.startActivityForResult(intent, 0);
|
|
|
}
|
|
|
- Set<BluetoothDevice> devices = adapter.getBondedDevices();
|
|
|
+ Set<BluetoothDevice> devices = bluetoothAdapter.getBondedDevices();
|
|
|
for (BluetoothDevice device : devices) {
|
|
|
deviceItemBeanList.add(new DeviceListAdapter.ItemBean(device));
|
|
|
}
|
|
@@ -90,20 +89,31 @@ public class ScanDeviceActivity extends AppCompatActivity {
|
|
|
deviceList.setAdapter(deviceListAdapter);
|
|
|
}
|
|
|
|
|
|
+ // 初始化蓝牙连接服务
|
|
|
+ chatService = new BluetoothChatService(this, mHandler);
|
|
|
+
|
|
|
initView();
|
|
|
setListener();
|
|
|
loadData();
|
|
|
}
|
|
|
|
|
|
+ @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();
|
|
|
this.unregisterReceiver(receiver);
|
|
|
- }
|
|
|
-
|
|
|
- public static void start(Context context) {
|
|
|
- Intent starter = new Intent(context, ScanDeviceActivity.class);
|
|
|
- context.startActivity(starter);
|
|
|
+ mHandler.removeCallbacksAndMessages(null);
|
|
|
}
|
|
|
|
|
|
private void initView() {
|
|
@@ -111,37 +121,31 @@ public class ScanDeviceActivity extends AppCompatActivity {
|
|
|
}
|
|
|
|
|
|
private void setListener() {
|
|
|
- deviceList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
|
|
|
- @Override
|
|
|
- public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
|
|
|
- selectIndex = i;
|
|
|
- selectDevice = deviceItemBeanList.get(i).device;
|
|
|
- switch (selectDevice.getBondState()) {
|
|
|
- case BluetoothDevice.BOND_NONE:
|
|
|
- // 未配对 - 尝试配对
|
|
|
- try {
|
|
|
- Method createBondMethod = BluetoothDevice.class.getMethod("createBond");
|
|
|
- createBondMethod.invoke(selectDevice);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- break;
|
|
|
- case BluetoothDevice.BOND_BONDING:
|
|
|
- // 配对中
|
|
|
- break;
|
|
|
- case BluetoothDevice.BOND_BONDED:
|
|
|
- // 已配对 - 尝试连接
|
|
|
- // 固定的 UUID
|
|
|
- String SPP_UUID = "00001101-0000-1000-8000-00805F9B34FB";
|
|
|
- UUID uuid = UUID.fromString(SPP_UUID);
|
|
|
- try {
|
|
|
- BluetoothSocket socket = selectDevice.createRfcommSocketToServiceRecord(uuid);
|
|
|
- socket.connect();
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- break;
|
|
|
- }
|
|
|
+ deviceList.setOnItemClickListener((adapterView, view, i, l) -> {
|
|
|
+ selectIndex = i;
|
|
|
+ selectDevice = deviceItemBeanList.get(i).device;
|
|
|
+ switch (selectDevice.getBondState()) {
|
|
|
+ case BluetoothDevice.BOND_NONE:
|
|
|
+ // 未配对 - 尝试配对
|
|
|
+ try {
|
|
|
+ Method createBondMethod = BluetoothDevice.class.getMethod("createBond");
|
|
|
+ createBondMethod.invoke(selectDevice);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case BluetoothDevice.BOND_BONDING:
|
|
|
+ // 配对中
|
|
|
+ break;
|
|
|
+ case BluetoothDevice.BOND_BONDED:
|
|
|
+ showConnectDeviceProgress(true);
|
|
|
+ // 已配对 - 尝试连接到设备
|
|
|
+ // BluetoothDevice device = bluetoothAdapter.getRemoteDevice(address);
|
|
|
+ // 设备的 MAC 地址
|
|
|
+ BluetoothDevice device = bluetoothAdapter.getRemoteDevice(selectDevice.getAddress());
|
|
|
+ chatService.connect(device);
|
|
|
+ break;
|
|
|
+ default:
|
|
|
}
|
|
|
});
|
|
|
}
|
|
@@ -162,7 +166,7 @@ public class ScanDeviceActivity extends AppCompatActivity {
|
|
|
dialog.setMessage("搜索设备中······");
|
|
|
dialog.setCancelable(false);
|
|
|
dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", (dialogInterface, i) -> {
|
|
|
- adapter.cancelDiscovery();
|
|
|
+ bluetoothAdapter.cancelDiscovery();
|
|
|
dialog.dismiss();
|
|
|
});
|
|
|
dialog.setOnDismissListener(dialog -> {
|
|
@@ -172,7 +176,7 @@ public class ScanDeviceActivity extends AppCompatActivity {
|
|
|
count++;
|
|
|
}
|
|
|
}
|
|
|
- Toast.makeText(ScanDeviceActivity.this, "查找到 " + count + " 个蓝牙设备", Toast.LENGTH_SHORT).show();
|
|
|
+ Toast.makeText(getApplicationContext(), "查找到 " + count + " 个蓝牙设备", Toast.LENGTH_SHORT).show();
|
|
|
});
|
|
|
}
|
|
|
if (show) {
|
|
@@ -198,6 +202,21 @@ public class ScanDeviceActivity extends AppCompatActivity {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public boolean onCreateOptionsMenu(Menu menu) {
|
|
|
menu.add(0, 0, 0, "提示").setIcon(R.drawable.ic_info_outline_white_24dp).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
|
|
@@ -230,22 +249,22 @@ public class ScanDeviceActivity extends AppCompatActivity {
|
|
|
deviceItemBeanList.removeAll(unboundItemBeanList);
|
|
|
deviceListAdapter.notifyDataSetChanged();
|
|
|
// 打开蓝牙
|
|
|
- if (!adapter.isEnabled()) {
|
|
|
+ if (!bluetoothAdapter.isEnabled()) {
|
|
|
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
|
|
|
// 设置蓝牙可见性,最多 300 秒
|
|
|
intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
|
|
|
ScanDeviceActivity.this.startActivity(intent);
|
|
|
}
|
|
|
// 寻找蓝牙设备,Android 会将查找到的设备以广播形式发出去
|
|
|
- if (!adapter.isDiscovering()) {
|
|
|
- adapter.startDiscovery();
|
|
|
+ if (!bluetoothAdapter.isDiscovering()) {
|
|
|
+ bluetoothAdapter.startDiscovery();
|
|
|
}
|
|
|
// 弹出是否允许扫描蓝牙设备的选择对话框
|
|
|
// Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
|
|
|
// startActivityForResult(intent, 1);
|
|
|
new Handler().postDelayed(() -> runOnUiThread(() -> {
|
|
|
showSearchDeviceProgress(false);
|
|
|
- adapter.cancelDiscovery();
|
|
|
+ bluetoothAdapter.cancelDiscovery();
|
|
|
}), 20000);
|
|
|
break;
|
|
|
}
|
|
@@ -291,6 +310,98 @@ public class ScanDeviceActivity extends AppCompatActivity {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ @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);
|
|
|
+ break;
|
|
|
+ case BluetoothChatService.STATE_CONNECTING:
|
|
|
+ Toast.makeText(getApplicationContext(), "连接中", Toast.LENGTH_SHORT).show();
|
|
|
+ break;
|
|
|
+ case BluetoothChatService.STATE_LISTEN:
|
|
|
+ Toast.makeText(getApplicationContext(), "侦听中", Toast.LENGTH_SHORT).show();
|
|
|
+ break;
|
|
|
+ case BluetoothChatService.STATE_NONE:
|
|
|
+ Toast.makeText(getApplicationContext(), "无连接", Toast.LENGTH_SHORT).show();
|
|
|
+ showConnectDeviceProgress(false);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case BluetoothChatService.MESSAGE_WRITE:
|
|
|
+ byte[] writeBuf = (byte[]) msg.obj;
|
|
|
+ // 自动发送
|
|
|
+ if (false) {
|
|
|
+ // 自动发送模块
|
|
|
+ mHandler.postDelayed(new Runnable() {
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ }
|
|
|
+ }, 1000);
|
|
|
+ } else if (false) {
|
|
|
+ mHandler.removeCallbacks(new Runnable() {
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ // 发送计数
|
|
|
+// if (outhex == true) {
|
|
|
+// String writeMessage = Data_syn.Bytes2HexString(writeBuf);
|
|
|
+// countout += writeMessage.length() / 2;
|
|
|
+// outcount.setText("" + countout);
|
|
|
+// } else if (outhex == false) {
|
|
|
+// String writeMessage = null;
|
|
|
+// try {
|
|
|
+// writeMessage = new String(writeBuf, "GBK");
|
|
|
+// } catch (UnsupportedEncodingException e1) {
|
|
|
+// e1.printStackTrace();
|
|
|
+// }
|
|
|
+// countout += writeMessage.length();
|
|
|
+// outcount.setText("" + countout);
|
|
|
+// }
|
|
|
+ break;
|
|
|
+ case BluetoothChatService.MESSAGE_READ:
|
|
|
+ byte[] readBuf = (byte[]) msg.obj;
|
|
|
+ //检错误码计算函数
|
|
|
+// 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) {
|
|
|
+// String readMessage = null;
|
|
|
+// try {
|
|
|
+// readMessage = new String(readBuf, 0, msg.arg1, "GBK");
|
|
|
+// } catch (UnsupportedEncodingException e) {
|
|
|
+// e.printStackTrace();
|
|
|
+// }
|
|
|
+// fmsg += readMessage;
|
|
|
+// mConversationView.append(readMessage);
|
|
|
+// // 接收计数,更新UI
|
|
|
+// countin += readMessage.length();
|
|
|
+// incount.setText("" + countin);
|
|
|
+// }
|
|
|
+ break;
|
|
|
+ case BluetoothChatService.MESSAGE_DEVICE_NAME:
|
|
|
+ String connectedDeviceName = msg.getData().getString(BluetoothChatService.DEVICE_NAME);
|
|
|
+ Toast.makeText(getApplicationContext(), "连接到 " + connectedDeviceName, Toast.LENGTH_SHORT).show();
|
|
|
+ ScanDeviceActivity.this.finish();
|
|
|
+ break;
|
|
|
+ case BluetoothChatService.MESSAGE_TOAST:
|
|
|
+ Toast.makeText(getApplicationContext(), msg.getData().getString(BluetoothChatService.TOAST), Toast.LENGTH_SHORT).show();
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
private BroadcastReceiver receiver = new BroadcastReceiver() {
|
|
|
@Override
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
@@ -298,12 +409,10 @@ public class ScanDeviceActivity extends AppCompatActivity {
|
|
|
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
|
|
|
// 获取查找到的蓝牙设备
|
|
|
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
|
|
|
-
|
|
|
if (!deviceItemBeanList.contains(new DeviceListAdapter.ItemBean(device)) && device.getName() != null && device.getAddress() != null) {
|
|
|
deviceItemBeanList.add(new DeviceListAdapter.ItemBean(device));
|
|
|
deviceListAdapter.notifyDataSetChanged();
|
|
|
}
|
|
|
-
|
|
|
// 如果查找到的设备符合要连接的设备,处理
|
|
|
// if (device.getName().equalsIgnoreCase(device.getName())) {
|
|
|
// // 搜索蓝牙设备的过程占用资源比较多,一旦找到需要连接的设备后需要及时关闭搜索
|
|
@@ -329,36 +438,35 @@ public class ScanDeviceActivity extends AppCompatActivity {
|
|
|
} else if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) {
|
|
|
// 状态改变的广播
|
|
|
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
|
|
|
- if (device.getName().equalsIgnoreCase(device.getName())) {
|
|
|
- switch (device.getBondState()) {
|
|
|
- case BluetoothDevice.BOND_NONE:
|
|
|
- // 取消配对
|
|
|
- System.out.println("取消配对");
|
|
|
- showMatchDeviceProgress(false);
|
|
|
- break;
|
|
|
- case BluetoothDevice.BOND_BONDING:
|
|
|
- // 正在配对
|
|
|
- System.out.println("正在配对");
|
|
|
- showMatchDeviceProgress(true);
|
|
|
- break;
|
|
|
- case BluetoothDevice.BOND_BONDED:
|
|
|
- // 完成配对
|
|
|
- System.out.println("完成配对");
|
|
|
- showMatchDeviceProgress(false);
|
|
|
- deviceItemBeanList.set(selectIndex, new DeviceListAdapter.ItemBean(device));
|
|
|
- deviceListAdapter.notifyDataSetChanged();
|
|
|
- Toast.makeText(ScanDeviceActivity.this, "和设备 " + device.getName() + " 配对成功", Toast.LENGTH_SHORT).show();
|
|
|
- break;
|
|
|
- }
|
|
|
+ switch (device.getBondState()) {
|
|
|
+ case BluetoothDevice.BOND_NONE:
|
|
|
+ // 取消配对
|
|
|
+ System.out.println("取消配对");
|
|
|
+ showMatchDeviceProgress(false);
|
|
|
+ break;
|
|
|
+ case BluetoothDevice.BOND_BONDING:
|
|
|
+ // 正在配对
|
|
|
+ System.out.println("正在配对");
|
|
|
+ showMatchDeviceProgress(true);
|
|
|
+ break;
|
|
|
+ case BluetoothDevice.BOND_BONDED:
|
|
|
+ // 完成配对
|
|
|
+ System.out.println("完成配对");
|
|
|
+ showMatchDeviceProgress(false);
|
|
|
+ deviceItemBeanList.set(selectIndex, new DeviceListAdapter.ItemBean(device));
|
|
|
+ deviceListAdapter.notifyDataSetChanged();
|
|
|
+ Toast.makeText(getApplicationContext(), "和设备 " + device.getName() + " 配对成功", Toast.LENGTH_SHORT).show();
|
|
|
+ break;
|
|
|
+ default:
|
|
|
}
|
|
|
} else if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
|
|
|
new Handler().postDelayed(() -> runOnUiThread(() -> {
|
|
|
- if (!adapter.isEnabled()) {
|
|
|
- Toast.makeText(ScanDeviceActivity.this, "蓝牙已关闭", Toast.LENGTH_SHORT).show();
|
|
|
+ if (!bluetoothAdapter.isEnabled()) {
|
|
|
+ Toast.makeText(getApplicationContext(), "蓝牙已关闭", Toast.LENGTH_SHORT).show();
|
|
|
ScanDeviceActivity.this.finish();
|
|
|
} else {
|
|
|
deviceItemBeanList.clear();
|
|
|
- Set<BluetoothDevice> devices = adapter.getBondedDevices();
|
|
|
+ Set<BluetoothDevice> devices = bluetoothAdapter.getBondedDevices();
|
|
|
for (BluetoothDevice device : devices) {
|
|
|
deviceItemBeanList.add(new DeviceListAdapter.ItemBean(device));
|
|
|
}
|