BluetoothManager.java 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. package com.nuoda.uniapp.Application;
  2. import android.annotation.SuppressLint;
  3. import android.bluetooth.BluetoothAdapter;
  4. import android.bluetooth.BluetoothDevice;
  5. import android.bluetooth.BluetoothGatt;
  6. import android.bluetooth.BluetoothGattCallback;
  7. import android.bluetooth.BluetoothGattCharacteristic;
  8. import android.bluetooth.BluetoothGattDescriptor;
  9. import android.bluetooth.BluetoothGattService;
  10. import android.bluetooth.BluetoothProfile;
  11. import android.content.Context;
  12. import android.util.Log;
  13. import java.util.ArrayList;
  14. import java.util.List;
  15. import java.util.Set;
  16. import java.util.UUID;
  17. /**
  18. * <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
  19. * <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
  20. * <uses-permission android:name="android.permission.BLUETOOTH" />
  21. * <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
  22. * */
  23. public class BluetoothManager {
  24. private BluetoothManager(){ }
  25. private static BluetoothManager mBluetoothManager;
  26. public static BluetoothManager getBluetoothManager(){
  27. if (mBluetoothManager==null)mBluetoothManager = new BluetoothManager();
  28. return mBluetoothManager;
  29. }
  30. private BluetoothGatt bluetoothGatt = null;
  31. private BluetoothGattCharacteristic myCharacteristic = null;
  32. private String bleValue = "....";
  33. private BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
  34. private final BluetoothGattCallback bluetoothGattCallback = new BluetoothGattCallback() {
  35. private UUID YOUR_SERVICE_UUID = UUID.fromString("4fafc201-1fb5-459e-8fcc-c5c9c331914b");
  36. private UUID YOUR_CHARACTERISTIC_UUID = UUID.fromString("beb5483e-36e1-4688-b7f5-ea07361b26a8");
  37. @Override
  38. public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) {
  39. super.onMtuChanged(gatt, mtu, status);
  40. openNotification();
  41. }
  42. @SuppressLint("MissingPermission")
  43. @Override
  44. public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
  45. //连接成功开启服务
  46. if (newState == BluetoothProfile.STATE_CONNECTED) {
  47. gatt.discoverServices();
  48. }else {
  49. disconnect();
  50. }
  51. }
  52. //当 BLE 设备的服务被发现时调用。这是在连接成功后进行服务发现时触发的回调
  53. @SuppressLint("MissingPermission")
  54. @Override
  55. public void onServicesDiscovered(BluetoothGatt gatt, int status) {
  56. if (status == BluetoothGatt.GATT_SUCCESS) {
  57. gatt.requestMtu(300);
  58. BluetoothGattService service = gatt.getService(YOUR_SERVICE_UUID);
  59. if (service!=null){
  60. myCharacteristic = service.getCharacteristic(YOUR_CHARACTERISTIC_UUID);
  61. bluetoothGatt = gatt;
  62. }
  63. }
  64. }
  65. //当特性值发生变化时调用。这通常是因为设备向手机推送了新的数据
  66. @Override
  67. public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic){
  68. bluetoothGatt = gatt;
  69. myCharacteristic = characteristic;
  70. getString();
  71. }
  72. //当读取特性值的操作完成时调用。此时可以获取特性的值
  73. @Override
  74. public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
  75. if (status == BluetoothGatt.GATT_SUCCESS){
  76. bluetoothGatt = gatt;
  77. myCharacteristic = characteristic;
  78. getString();
  79. }
  80. }
  81. //当写入特性值的操作完成时调用
  82. @Override
  83. public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
  84. // 检查写入操作是否成功
  85. if (status == BluetoothGatt.GATT_SUCCESS) {
  86. bluetoothGatt = gatt;
  87. myCharacteristic = characteristic;
  88. getString();
  89. }
  90. }
  91. };
  92. @SuppressLint("MissingPermission")
  93. private void openNotification(){
  94. if(bluetoothGatt==null || myCharacteristic==null)return;
  95. try{
  96. //开启通知
  97. bluetoothGatt.setCharacteristicNotification(myCharacteristic, true);
  98. BluetoothGattDescriptor descriptor = myCharacteristic.getDescriptors().get(0);
  99. descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
  100. bluetoothGatt.writeDescriptor(descriptor);
  101. }catch (Exception e){
  102. Log.d("bleerror:",e.getMessage());
  103. }
  104. //主动获取特征值
  105. read();
  106. }
  107. @SuppressLint("MissingPermission")
  108. public List<BluetoothDevice> getDivList(){
  109. List<BluetoothDevice> listData = new ArrayList<>();
  110. if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled()) {
  111. // 蓝牙不可用
  112. }else {
  113. Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
  114. for (BluetoothDevice bean:pairedDevices)listData.add(bean);
  115. }
  116. return listData;
  117. }
  118. @SuppressLint("MissingPermission")
  119. public void connect(Context context, BluetoothDevice device) {
  120. if (bluetoothGatt!=null)return;
  121. bluetoothGatt = device.connectGatt(context, false, bluetoothGattCallback);
  122. }
  123. //读取
  124. @SuppressLint("MissingPermission")
  125. public void read(){
  126. if (bluetoothGatt==null || myCharacteristic==null)return;
  127. bluetoothGatt.readCharacteristic(myCharacteristic);
  128. }
  129. //写入
  130. @SuppressLint("MissingPermission")
  131. public void write(String value){
  132. if (bluetoothGatt==null || myCharacteristic==null)return;
  133. myCharacteristic.setValue(value.getBytes());
  134. bluetoothGatt.writeCharacteristic(myCharacteristic);
  135. }
  136. @SuppressLint("MissingPermission")
  137. public void disconnect() {
  138. try {
  139. if (bluetoothGatt != null) {
  140. bluetoothGatt.disconnect();
  141. bluetoothGatt.close();
  142. bluetoothGatt = null;
  143. myCharacteristic=null;
  144. }
  145. } catch (Exception e) {
  146. // 处理异常关闭
  147. }
  148. }
  149. public boolean isble(){
  150. return bluetoothGatt!=null;
  151. }
  152. public String getValue(){
  153. return bleValue;
  154. }
  155. public void getString(){
  156. if (myCharacteristic==null)return;
  157. byte[] data = myCharacteristic.getValue();
  158. if (data==null)return;
  159. StringBuilder sb = new StringBuilder();
  160. for (byte b:data) sb.append((char) (b & 0xFF));
  161. bleValue = sb.toString();
  162. if (bleValue==null)bleValue="";
  163. if("r:not rfid".equals(bleValue)){
  164. bleValue = "未识别到ic卡";
  165. }else if ("r:rfid error".equals(bleValue)){
  166. bleValue = "操作ic卡异常";
  167. }else if ("r:write ok".equals(bleValue)){
  168. bleValue = "写入成功";
  169. }else if (bleValue.indexOf("r:")==0){
  170. bleValue = "ic卡读取数据:" + bleValue.substring(2);
  171. }else if ("c:...".equals(bleValue)){
  172. bleValue = "扫码头已打开";
  173. }else if ("c:end".equals(bleValue)){
  174. bleValue = "扫码头已关闭";
  175. }else if (bleValue.indexOf("c:")==0){
  176. bleValue = "扫码数据:" + bleValue.substring(2);
  177. }
  178. Log.d("bledata:",bleValue);
  179. }
  180. }