| | |
| | | } |
| | | characteristic = targetService.getCharacteristic(charUuid); |
| | | notifyCharacteristic = targetService.getCharacteristic(notifyCharUuid); |
| | | boolean mtuRequestResult = gatt.requestMtu(512); // 默认设置请求最大 MTU,有设备可能不支持 |
| | | Log.d(TAG, "requestMtu(512) 结果:" + mtuRequestResult); |
| | | |
| | | if (characteristic == null) { |
| | | Log.e(TAG, "Required characteristics not found"); |
| | | notifyConnectionFailed("Characteristics not found"); |
| | |
| | | } |
| | | return; |
| | | } |
| | | |
| | | Log.e(TAG, "onCharacteristicWrite: 写入成功回复"); |
| | | if (characteristic.getUuid().equals(charUuid)) { |
| | | //写入成功 |
| | | if (writeListener != null) { |
| | |
| | | |
| | | @Override |
| | | public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { |
| | | Log.e(TAG, "onCharacteristicChanged: " + characteristic.getUuid()); |
| | | //收到蓝牙回复的数 |
| | | if (characteristic.getUuid().equals(notifyCharUuid) && writeListener != null) { |
| | | byte[] value = characteristic.getValue(); |
| | | String response = new String(value, StandardCharsets.UTF_8); |
| | |
| | | |
| | | @Override |
| | | public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) { |
| | | Log.d(TAG, "onDescriptorWrite: status=" + status + |
| | | ", descriptor=" + descriptor.getUuid()); |
| | | // 通知描述符写入完成,可以忽略 |
| | | // if (status == BluetoothGatt.GATT_SUCCESS) { |
| | | // Log.d(TAG, "通知开启成功,可以接收设备响应了"); |
| | | // } else { |
| | | // Log.e(TAG, "通知开启失败:" + status); |
| | | // if (writeListener != null) { |
| | | // callbackHandler.post(() -> writeListener.onWriteFailed(status)); |
| | | // } |
| | | // } |
| | | if (status == BluetoothGatt.GATT_SUCCESS) { |
| | | Log.d(TAG, "通知开启成功,可以接收设备响应了"); |
| | | } else { |
| | | Log.e(TAG, "通知开启失败:" + status); |
| | | if (writeListener != null) { |
| | | callbackHandler.post(() -> writeListener.onWriteFailed(status)); |
| | | } |
| | | } |
| | | } |
| | | @Override |
| | | public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) { |
| | | //todo 后面兼用mtc分包的,目前默认设置512字节 |
| | | if (status == BluetoothGatt.GATT_SUCCESS) { |
| | | // currentMtu = mtu; |
| | | Log.d(TAG, "✅ MTU 协商成功:当前 MTU = " + mtu + " 字节"); |
| | | Log.d(TAG, " 有效数据长度 = " + (mtu - 5) + " 字节"); |
| | | |
| | | // // MTU 设置成功后,标记连接成功 |
| | | // isConnected = true; |
| | | // if (connectListener != null) { |
| | | // callbackHandler.post(() -> { |
| | | // Log.d(TAG, "通知连接成功回调"); |
| | | // connectListener.onConnected(); |
| | | // }); |
| | | // } |
| | | } else { |
| | | // Log.w(TAG, "⚠️ MTU 协商失败,status: " + status + ",使用默认 MTU=23"); |
| | | //// currentMtu = 23; |
| | | // // 即使 MTU 失败,也认为连接成功(使用默认 MTU) |
| | | // isConnected = true; |
| | | // if (connectListener != null) { |
| | | // callbackHandler.post(() -> connectListener.onConnected()); |
| | | // } |
| | | } |
| | | } |
| | | |
| | | |
| | | }; |
| | | |
| | |
| | | BluetoothGattDescriptor descriptor = characteristic.getDescriptor( |
| | | UUID.fromString("00002902-0000-1000-8000-00805f9b34fb")); |
| | | if (descriptor != null) { |
| | | // // 第 3 步:设置描述符的值 |
| | | // // 先检查特征属性,决定使用 NOTIFY 还是 INDICATE |
| | | // int charProp = characteristic.getProperties(); |
| | | // if ((charProp & BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) { |
| | | // descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); |
| | | // Log.d(TAG, "使用 NOTIFICATION 模式"); |
| | | // } else if ((charProp & BluetoothGattCharacteristic.PROPERTY_INDICATE) > 0) { |
| | | // descriptor.setValue(BluetoothGattDescriptor.ENABLE_INDICATION_VALUE); |
| | | // Log.d(TAG, "使用 INDICATION 模式"); |
| | | // } else { |
| | | // Log.e(TAG, "该特征不支持通知/指示"); |
| | | // return; |
| | | // } |
| | | // 第 3 步:设置描述符的值为 ENABLE_NOTIFICATION_VALUE, |
| | | // ↑ 这个值是 byte[]{0x01, 0x00},告诉设备端"我要订阅通知" |
| | | descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE); |
| | |
| | | */ |
| | | @RequiresPermission(Manifest.permission.BLUETOOTH_CONNECT) |
| | | public void writeCredentials(String data, WriteListener listener) { |
| | | Log.e(TAG, "writeCredentials :准备写入"); |
| | | this.writeListener = listener; |
| | | if (!isConnected || characteristic == null) { |
| | | if (listener != null) { |
| | |
| | | |
| | | @RequiresPermission(Manifest.permission.BLUETOOTH_CONNECT) |
| | | private void writeCharacteristic(BluetoothGattCharacteristic characteristic, String value) { |
| | | Log.e(TAG, "writeCharacteristic :开始写入"); |
| | | if (!this.getMissingPermissions().isEmpty()) { |
| | | callbackHandler.post(() -> this.writeListener.onWriteFailed(-2)); // 没有权限 |
| | | return; |
| | |
| | | characteristic.setValue(value.getBytes(StandardCharsets.UTF_8)); |
| | | characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT); |
| | | bluetoothGatt.writeCharacteristic(characteristic); |
| | | Log.e(TAG, "writeCharacteristic :开始完成"); |
| | | } |
| | | |
| | | // ==================== 工具方法 ==================== |