1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
| public class Blue { Context mContext; BluetoothGatt bluetoothGatt; BluetoothDevice device; BluetoothGattCharacteristic controlCharacteristicl, notifyCharacteristic; BlueCount blueCount; String uuidFa; String uuidShou; String s; boolean isConnecting = false; ConversionUtils conversionUtils;
public Blue(final Context mContext, BluetoothGatt bluetoothGatt, final BluetoothGattCharacteristic controlCharacteristicl, BluetoothGattCharacteristic notifyCharacteristic) { this.mContext = mContext; this.bluetoothGatt = bluetoothGatt; this.controlCharacteristicl = controlCharacteristicl; this.notifyCharacteristic = notifyCharacteristic; }
private static OnErWenListener onErWenListener;
public interface OnErWenListener { void onErWenListener(int position); }
public void setOnErWenListener(OnErWenListener onErWenListener) { this.onErWenListener = onErWenListener; }
public void onDes() { if (isConnecting) { bluetoothGatt.disconnect(); LogUtils.e("耳温仪已断开蓝牙连接"); } }
public void init(final BluetoothDevice device) {
onErWenListener.onErWenListener(1); this.device = device; conversionUtils = new ConversionUtils(); new Thread() { @Override public void run() { super.run(); if (device == null) { return; } bluetoothGatt = device.connectGatt(mContext, false, gattCallback); if (bluetoothGatt == null) { return; } uuidShou = ""; uuidFa = ""; } }.start(); }
private Handler handler1 = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); if (isConnecting) { LogUtils.e("-----》发送指令;"); byte[] byte2 = conversionUtils.hexStringToBytes(""); controlCharacteristicl.setValue(byte2); bluetoothGatt.writeCharacteristic(controlCharacteristicl);
} } };
private BluetoothGattCallback gattCallback = new BluetoothGattCallback() {
@Override public void onConnectionStateChange(final BluetoothGatt gatt, int status, final int newState) { super.onConnectionStateChange(gatt, status, newState); switch (newState) { case BluetoothGatt.STATE_CONNECTED: LogUtils.e("连接状态:已连接"); isConnecting = true; onErWenListener.onErWenListener(2); bluetoothGatt.discoverServices(); break; case BluetoothGatt.STATE_CONNECTING: LogUtils.e("连接状态:正在连接"); break; case BluetoothGatt.STATE_DISCONNECTED: LogUtils.e("连接状态:断开连接"); bluetoothGatt.close(); onErWenListener.onErWenListener(3); isConnecting = false; break; case BluetoothGatt.STATE_DISCONNECTING: LogUtils.e("连接状态:正在断开连接"); onErWenListener.onErWenListener(10); break; default: break; } }
@Override public void onServicesDiscovered(BluetoothGatt gatt, int status) { super.onServicesDiscovered(gatt, status); if (status == BluetoothGatt.GATT_SUCCESS) { List<BluetoothGattService> services = bluetoothGatt.getServices(); for (BluetoothGattService bluetoothGattService : services) {
List<BluetoothGattCharacteristic> characteristics = bluetoothGattService.getCharacteristics(); for (BluetoothGattCharacteristic bluetoothGattCharacteristic : characteristics) {
if (bluetoothGattCharacteristic.getUuid().toString().equals(uuidFa)) {
LogUtils.e("准备发送指令"); controlCharacteristicl = bluetoothGattCharacteristic; handler1.sendEmptyMessageDelayed(1, 1000);
} else if (bluetoothGattCharacteristic.getUuid().toString().equals(uuidShou)) {
LogUtils.e("准备接收数据"); bluetoothGatt.setCharacteristicNotification(bluetoothGattCharacteristic, true); notifyCharacteristic = bluetoothGattCharacteristic; enableNotification(true, notifyCharacteristic); } } } } }
@Override public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { super.onCharacteristicRead(gatt, characteristic, status); if (status == BluetoothGatt.GATT_SUCCESS) LogUtils.e("onCharRead " + gatt.getDevice().getName() + " read " + characteristic.getUuid().toString() + " -> " + Utils.bytesToHexString(characteristic.getValue())); }
@Override public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { super.onCharacteristicWrite(gatt, characteristic, status); ConversionUtils conversionUtils = new ConversionUtils(); LogUtils.e("onCharWrite " + gatt.getDevice().getName() + " write " + characteristic.getUuid().toString() + " -> " + conversionUtils.Bytes2HexString(characteristic.getValue())); }
@Override public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { super.onCharacteristicChanged(gatt, characteristic);
if (characteristic.getUuid().toString().equals(notifyCharacteristic.getUuid().toString())) {
erWen(characteristic); } }
@Override public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) { super.onDescriptorRead(gatt, descriptor, status); }
@Override public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) { super.onDescriptorWrite(gatt, descriptor, status); }
@Override public void onReliableWriteCompleted(BluetoothGatt gatt, int status) { super.onReliableWriteCompleted(gatt, status); }
@Override public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) { super.onReadRemoteRssi(gatt, rssi, status); }
@Override public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) { super.onMtuChanged(gatt, mtu, status); }
@Override protected Object clone() throws CloneNotSupportedException { return super.clone(); }
@Override public boolean equals(Object o) { return super.equals(o); }
@Override protected void finalize() throws Throwable { super.finalize(); }
@Override public int hashCode() { return super.hashCode(); }
@Override public String toString() { return super.toString(); } };
private boolean enableNotification(boolean enable, BluetoothGattCharacteristic characteristic) { if (bluetoothGatt == null || characteristic == null) return false; if (!bluetoothGatt.setCharacteristicNotification(characteristic, enable)) return false; BluetoothGattDescriptor clientConfig = characteristic.getDescriptor(UUID.fromString("00002902-0000-1000-8000-00805f9b34fb")); if (clientConfig == null) return false; if (enable) { clientConfig.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); } else { clientConfig.setValue(BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE); } LogUtils.e(bluetoothGatt.writeDescriptor(clientConfig) + "---------->>>>>>>>>bluetoothGatt.writeDescriptor(clientConfig)"); return bluetoothGatt.writeDescriptor(clientConfig); }
double wen;
public double wen() { return wen; }
public void erWen(BluetoothGattCharacteristic characteristic) { blueCount = new BlueCount(); byte[] value = characteristic.getValue(); String str = conversionUtils.Bytes2HexString(value); Log.e("接收到的16进制字符串--->>", str); if (str.equals("")){ if (isConnecting) { bluetoothGatt.disconnect(); LogUtils.e("耳温仪已断开蓝牙连接"); return; } } String str1 = str.substring(10, 12); Log.e("截取后的字符串--->>", str1); if (str1.equals("")) { String str2 = str.substring(12, 16); Log.e("拿到的数据--->>", str2); blueCount.show(str2); }else {
}
final double n = blueCount.getM();
if (n != 0.0) {
} } }
|