| | |
| | | |
| | | if (bluetoothGatt != null) { |
| | | bluetoothGatt.disconnect(); // 先断开连接 |
| | | // bluetoothGatt.close(); |
| | | // bluetoothGatt = null; |
| | | bluetoothGatt.close(); |
| | | bluetoothGatt = null; |
| | | Log.d(TAG, "Connecting to 先断开旧蓝牙连接"); |
| | | } |
| | | BluetoothDevice device = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(deviceAddress); |
| | |
| | | gatt.discoverServices(); |
| | | } else if (newState == BluetoothProfile.STATE_DISCONNECTED) { |
| | | isConnected = false; |
| | | if (bluetoothGatt != null) { |
| | | bluetoothGatt.close(); |
| | | bluetoothGatt = null; |
| | | } |
| | | // if (bluetoothGatt != null) { |
| | | // bluetoothGatt.close(); |
| | | // bluetoothGatt = null; |
| | | // } |
| | | if (connectListener != null) { |
| | | callbackHandler.post(() -> connectListener.onDisconnected()); |
| | | } |
| | |
| | | } |
| | | return; |
| | | } |
| | | Log.e(TAG, "onCharacteristicWrite: 写入成功回复"); |
| | | Log.d(TAG, "onCharacteristicWrite: 写入成功回复"); |
| | | if (characteristic.getUuid().equals(charUuid)) { |
| | | //写入成功 |
| | | if (writeListener != null) { |
| | |
| | | |
| | | @Override |
| | | public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { |
| | | Log.e(TAG, "onCharacteristicChanged: " + characteristic.getUuid()); |
| | | Log.d(TAG, "onCharacteristicChanged: " + characteristic.getUuid()); |
| | | //收到蓝牙回复的数 |
| | | if (characteristic.getUuid().equals(notifyCharUuid) && writeListener != null) { |
| | | byte[] value = characteristic.getValue(); |