BluetoothService.java 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. package com.uas.aowei_wms;
  2. import android.app.Activity;
  3. import android.app.ProgressDialog;
  4. import android.bluetooth.BluetoothAdapter;
  5. import android.bluetooth.BluetoothDevice;
  6. import android.content.BroadcastReceiver;
  7. import android.content.Context;
  8. import android.content.Intent;
  9. import android.content.IntentFilter;
  10. import android.view.View;
  11. import android.widget.AdapterView;
  12. import android.widget.Button;
  13. import android.widget.ListView;
  14. import android.widget.SimpleAdapter;
  15. import android.widget.Toast;
  16. import java.lang.reflect.Method;
  17. import java.util.ArrayList;
  18. import java.util.HashMap;
  19. /**
  20. * Created by jsxiaoshui on 2022-07-29
  21. */
  22. public class BluetoothService {
  23. private Context context = null;
  24. private BluetoothAdapter bluetoothAdapter = BluetoothAdapter
  25. .getDefaultAdapter();
  26. private ArrayList<BluetoothDevice> unbondDevices = null; // 用于存放未配对蓝牙设备
  27. private ArrayList<BluetoothDevice> bondDevices = null;// 用于存放已配对蓝牙设备
  28. private Button switchBT = null;
  29. private Button searchDevices = null;
  30. private ListView unbondDevicesListView = null;
  31. private ListView bondDevicesListView = null;
  32. /**
  33. * 添加已绑定蓝牙设备到ListView
  34. */
  35. private void addBondDevicesToListView() {
  36. ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
  37. int count = this.bondDevices.size();
  38. System.out.println("已绑定设备数量:" + count);
  39. for (int i = 0; i < count; i++) {
  40. HashMap<String, Object> map = new HashMap<String, Object>();
  41. map.put("deviceName", this.bondDevices.get(i).getName());
  42. data.add(map);// 把item项的数据加到data中
  43. }
  44. String[] from = { "deviceName" };
  45. int[] to = { R.id.device_name };
  46. SimpleAdapter simpleAdapter = new SimpleAdapter(this.context, data,
  47. R.layout.bonddevice_item, from, to);
  48. // 把适配器装载到listView中
  49. this.bondDevicesListView.setAdapter(simpleAdapter);
  50. this.bondDevicesListView
  51. .setOnItemClickListener(new AdapterView.OnItemClickListener() {
  52. @Override
  53. public void onItemClick(AdapterView<?> arg0, View arg1,
  54. int arg2, long arg3) {
  55. BluetoothDevice device = bondDevices.get(arg2);
  56. Intent intent = new Intent();
  57. intent.setClassName(context,
  58. "com.uas.aowei_wms.PrintDataActivity");
  59. intent.putExtra("deviceAddress", device.getAddress());
  60. context.startActivity(intent);
  61. }
  62. });
  63. }
  64. /**
  65. * 添加未绑定蓝牙设备到ListView
  66. */
  67. private void addUnbondDevicesToListView() {
  68. ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
  69. int count = this.unbondDevices.size();
  70. System.out.println("未绑定设备数量:" + count);
  71. for (int i = 0; i < count; i++) {
  72. HashMap<String, Object> map = new HashMap<String, Object>();
  73. map.put("deviceName", this.unbondDevices.get(i).getName());
  74. data.add(map);// 把item项的数据加到data中
  75. }
  76. String[] from = { "deviceName" };
  77. int[] to = { R.id.device_name };
  78. SimpleAdapter simpleAdapter = new SimpleAdapter(this.context, data,
  79. R.layout.unbonddevice_item, from, to);
  80. // 把适配器装载到listView中
  81. this.unbondDevicesListView.setAdapter(simpleAdapter);
  82. // 为每个item绑定监听,用于设备间的配对
  83. this.unbondDevicesListView
  84. .setOnItemClickListener(new AdapterView.OnItemClickListener() {
  85. @Override
  86. public void onItemClick(AdapterView<?> arg0, View arg1,
  87. int arg2, long arg3) {
  88. try {
  89. Method createBondMethod = BluetoothDevice.class
  90. .getMethod("createBond");
  91. createBondMethod
  92. .invoke(unbondDevices.get(arg2));
  93. // 将绑定好的设备添加的已绑定list集合
  94. bondDevices.add(unbondDevices.get(arg2));
  95. // 将绑定好的设备从未绑定list集合中移除
  96. unbondDevices.remove(arg2);
  97. addBondDevicesToListView();
  98. addUnbondDevicesToListView();
  99. } catch (Exception e) {
  100. Toast.makeText(context, "配对失败!", Toast.LENGTH_SHORT)
  101. .show();
  102. }
  103. }
  104. });
  105. }
  106. public BluetoothService(Context context, ListView unbondDevicesListView,
  107. ListView bondDevicesListView, Button switchBT, Button searchDevices) {
  108. this.context = context;
  109. this.unbondDevicesListView = unbondDevicesListView;
  110. this.bondDevicesListView = bondDevicesListView;
  111. // this.bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
  112. this.unbondDevices = new ArrayList<BluetoothDevice>();
  113. this.bondDevices = new ArrayList<BluetoothDevice>();
  114. this.switchBT = switchBT;
  115. this.searchDevices = searchDevices;
  116. this.initIntentFilter();
  117. }
  118. private void initIntentFilter() {
  119. // 设置广播信息过滤
  120. IntentFilter intentFilter = new IntentFilter();
  121. intentFilter.addAction(BluetoothDevice.ACTION_FOUND);
  122. intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
  123. intentFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
  124. intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
  125. // 注册广播接收器,接收并处理搜索结果
  126. context.registerReceiver(receiver, intentFilter);
  127. }
  128. /**
  129. * 打开蓝牙
  130. */
  131. public void openBluetooth(Activity activity) {
  132. Intent enableBtIntent = new Intent(
  133. BluetoothAdapter.ACTION_REQUEST_ENABLE);
  134. activity.startActivityForResult(enableBtIntent, 1);
  135. }
  136. /**
  137. * 关闭蓝牙
  138. */
  139. public void closeBluetooth() {
  140. this.bluetoothAdapter.disable();
  141. }
  142. /**
  143. * 判断蓝牙是否打开
  144. *
  145. * @return boolean
  146. */
  147. public boolean isOpen() {
  148. return this.bluetoothAdapter.isEnabled();
  149. }
  150. /**
  151. * 搜索蓝牙设备
  152. */
  153. public void searchDevices() {
  154. this.bondDevices.clear();
  155. this.unbondDevices.clear();
  156. // 寻找蓝牙设备,android会将查找到的设备以广播形式发出去
  157. this.bluetoothAdapter.startDiscovery();
  158. }
  159. /**
  160. * 添加未绑定蓝牙设备到list集合
  161. *
  162. * @param device
  163. */
  164. public void addUnbondDevices(BluetoothDevice device) {
  165. System.out.println("未绑定设备名称:" + device.getName());
  166. if (!this.unbondDevices.contains(device)) {
  167. this.unbondDevices.add(device);
  168. }
  169. }
  170. /**
  171. * 添加已绑定蓝牙设备到list集合
  172. *
  173. * @param device
  174. */
  175. public void addBandDevices(BluetoothDevice device) {
  176. System.out.println("已绑定设备名称:" + device.getName());
  177. if (!this.bondDevices.contains(device)) {
  178. this.bondDevices.add(device);
  179. }
  180. }
  181. /**
  182. * 蓝牙广播接收器
  183. */
  184. private BroadcastReceiver receiver = new BroadcastReceiver() {
  185. ProgressDialog progressDialog = null;
  186. @Override
  187. public void onReceive(Context context, Intent intent) {
  188. String action = intent.getAction();
  189. if (BluetoothDevice.ACTION_FOUND.equals(action)) {
  190. BluetoothDevice device = intent
  191. .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
  192. if (device.getBondState() == BluetoothDevice.BOND_BONDED) {
  193. addBandDevices(device);
  194. } else {
  195. addUnbondDevices(device);
  196. }
  197. } else if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
  198. progressDialog = ProgressDialog.show(context, "请稍等...",
  199. "搜索蓝牙设备中...", true);
  200. } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED
  201. .equals(action)) {
  202. System.out.println("设备搜索完毕");
  203. progressDialog.dismiss();
  204. addUnbondDevicesToListView();
  205. addBondDevicesToListView();
  206. // bluetoothAdapter.cancelDiscovery();
  207. }
  208. if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
  209. if (bluetoothAdapter.getState() == BluetoothAdapter.STATE_ON) {
  210. System.out.println("--------打开蓝牙-----------");
  211. switchBT.setText("关闭蓝牙");
  212. searchDevices.setEnabled(true);
  213. bondDevicesListView.setEnabled(true);
  214. unbondDevicesListView.setEnabled(true);
  215. } else if (bluetoothAdapter.getState() == BluetoothAdapter.STATE_OFF) {
  216. System.out.println("--------关闭蓝牙-----------");
  217. switchBT.setText("打开蓝牙");
  218. searchDevices.setEnabled(false);
  219. bondDevicesListView.setEnabled(false);
  220. unbondDevicesListView.setEnabled(false);
  221. }
  222. }
  223. }
  224. };
  225. }