android蓝牙在传输过程中会发生进度条停止的现象

平台高通8260,Android4.0:

 最近项目组手机发现,手机被传入文件后,在传入几个文件时就停止在某一进度条。并且内核一直在打印h4_recv: Unknown HCI packet type ,其实一直就怀疑是频繁打开关闭串口导致的。

今天终于证实了。

之前机制是这样的:

     首先申请Host_wake中断类型为高电平。当第一次打开蓝牙时, Host_wake =1时,此时产生中断。然后就轮番改变中断类型。在中断函数中,都会打开或者关闭串口。这就是问题之所在。当我们bt 给 cpu传数据的时候。在我们在传输的过程中,再打开、关闭串口会导致bt 中的buf溢出。这就导致了数据的帧检测位出问题:rfcomm_recv_frame:bad checksum in packet。

         我们的做法是:传输过程中不再让它打开或关闭串口就ok了。 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是一个示例代码,用于在 Android 平台上使用蓝牙传输文件并显示传输进度: ```java public class BluetoothTransferActivity extends AppCompatActivity { private static final int REQUEST_ENABLE_BLUETOOTH = 1; private static final int REQUEST_SELECT_FILE = 2; private BluetoothAdapter mBluetoothAdapter; private BluetoothDevice mBluetoothDevice; private BluetoothSocket mBluetoothSocket; private ProgressBar mProgressBar; private TextView mProgressText; private File mSelectedFile; private long mFileSize; private boolean mIsTransferInProgress; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_bluetooth_transfer); mProgressBar = findViewById(R.id.progress_bar); mProgressText = findViewById(R.id.progress_text); mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (mBluetoothAdapter == null) { Toast.makeText(this, "Bluetooth is not available on this device", Toast.LENGTH_SHORT).show(); finish(); return; } if (!mBluetoothAdapter.isEnabled()) { Intent enableBluetoothIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBluetoothIntent, REQUEST_ENABLE_BLUETOOTH); return; } selectFile(); } private void selectFile() { Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType("*/*"); startActivityForResult(intent, REQUEST_SELECT_FILE); } private void setupBluetoothConnection() { mBluetoothDevice = mBluetoothAdapter.getRemoteDevice("00:11:22:33:44:55"); // Replace with your device address try { mBluetoothSocket = mBluetoothDevice.createRfcommSocketToServiceRecord(UUID.randomUUID()); mBluetoothSocket.connect(); } catch (IOException e) { Toast.makeText(this, "Failed to connect to Bluetooth device", Toast.LENGTH_SHORT).show(); e.printStackTrace(); finish(); } } private void startTransfer() { mIsTransferInProgress = true; try { InputStream inputStream = new FileInputStream(mSelectedFile); OutputStream outputStream = mBluetoothSocket.getOutputStream(); byte[] buffer = new byte[1024]; int length; long totalBytesRead = 0; while ((length = inputStream.read(buffer)) > 0) { outputStream.write(buffer, 0, length); totalBytesRead += length; int progress = (int) (totalBytesRead * 100 / mFileSize); updateProgress(progress); } outputStream.flush(); inputStream.close(); outputStream.close(); mIsTransferInProgress = false; Toast.makeText(this, "File transfer complete", Toast.LENGTH_SHORT).show(); finish(); } catch (IOException e) { mIsTransferInProgress = false; Toast.makeText(this, "File transfer failed", Toast.LENGTH_SHORT).show(); e.printStackTrace(); finish(); } } private void updateProgress(final int progress) { runOnUiThread(new Runnable() { @Override public void run() { mProgressBar.setProgress(progress); mProgressText.setText(progress + "%"); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == REQUEST_ENABLE_BLUETOOTH) { if (resultCode == RESULT_OK) { selectFile(); } else { Toast.makeText(this, "Bluetooth must be enabled to transfer files", Toast.LENGTH_SHORT).show(); finish(); } } if (requestCode == REQUEST_SELECT_FILE) { if (resultCode == RESULT_OK) { Uri fileUri = data.getData(); String filePath = getRealPathFromUri(fileUri); mSelectedFile = new File(filePath); mFileSize = mSelectedFile.length(); mProgressBar.setMax(100); setupBluetoothConnection(); startTransfer(); } else { finish(); } } } private String getRealPathFromUri(Uri uri) { String[] projection = {MediaStore.Images.Media.DATA}; Cursor cursor = getContentResolver().query(uri, projection, null, null, null); int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); String filePath = cursor.getString(column_index); cursor.close(); return filePath; } @Override protected void onDestroy() { super.onDestroy(); if (mIsTransferInProgress) { try { mBluetoothSocket.close(); } catch (IOException e) { e.printStackTrace(); } } } } ``` 这个示例代码包含以下步骤: 1. 检查设备是否支持蓝牙,如果不支持则退出应用程序。 2. 检查蓝牙是否启用,如果未启用则请求启用。 3. 选择待传输的文件。 4. 连接到蓝牙设备。 5. 开始传输文件,更新进度条和进度文本。 6. 当传输完成或失败时,显示适当的消息并退出应用程序。 请注意,此示例代码仅用于演示目的。在实际应用程序中,您应该添加错误处理并确保正确关闭蓝牙连接。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_807315755

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值