Android蓝牙程序在大约一分钟后停止接收字符。java.io.exception:BTSocketClosedReadReturn:-1
创始人
2024-10-08 21:01:33
0

这个问题通常是由于蓝牙Socket连接被意外关闭引起的。为了解决这个问题,可以在程序的onPause()方法中注销蓝牙连接,然后在程序恢复时重新连接。以下是示例代码:

public class MainActivity extends Activity {
   private BluetoothAdapter mBluetoothAdapter;
   private BluetoothSocket mmSocket;
   private InputStream mmInStream;
   private OutputStream mmOutStream;
   private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
   private static String address = "00:00:00:00:00:00";

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);

       mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
       BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);

       try {
           mmSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
           mmSocket.connect();
           mmInStream = mmSocket.getInputStream();
           mmOutStream = mmSocket.getOutputStream();
       } catch (IOException e) {
           e.printStackTrace();
       }
   }

   @Override
   protected void onPause() {
       super.onPause();

       try {
           mmSocket.close();
       } catch (IOException e) {
           e.printStackTrace();
       }
   }

   @Override
   protected void onResume() {
       super.onResume();

       BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);

       try {
           mmSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
           mmSocket.connect();
           mmInStream = mmSocket.getInputStream();
           mmOutStream = mmSocket.getOutputStream();
       } catch (IOException e) {
           e.printStackTrace();
       }
   }
}

在这个示例代码中,当程序进入暂停状态时,BluetoothSocket会被关闭。当程序恢复时,BluetoothSocket会重新连接并打开。这样可以确保每次程序运行时都能够建立有效的BluetoothSocket连接,并避免蓝牙连接意外关闭的问题。

相关内容

热门资讯

透视app!红龙poker辅助... 透视app!红龙poker辅助平台(透视)竟然是真的有挂(详细辅助第三方教程);亲,关键说明,红龙p...
最新通报!wepoker辅助器... 最新通报!wepoker辅助器安装包,wepoker透视脚本安卓,攻略教程(有挂透明);支持2-10...
透视工具!hhpoker透视脚... 透视工具!hhpoker透视脚本(透视)切实是有挂(详细辅助揭秘教程)1、实时hhpoker透视脚本...
透视私人局!wepoker辅助... 透视私人局!wepoker辅助插件功能(透视)其实是真的有挂(详细辅助规律教程);一、AI软件牌型概...
分享一款!wpk作弊,hhpo... 分享一款!wpk作弊,hhpoker作弊码怎么用,实用技巧(有挂神器);玩家必备必赢加哟《13670...
透视科技!hhpoker脚本下... 透视科技!hhpoker脚本下载(透视)竟然真的是有挂(详细辅助靠谱教程)1、玩家可以在软件透明挂俱...
玩家必知教程!拱趴大菠萝有什么... 1、玩家必知教程!拱趴大菠萝有什么挂,hhpoker有没有作弊辅助,攻略方法(有挂方法)。2、hhp...
透视辅助!wepoker透视辅... 透视辅助!wepoker透视辅助下载(透视)竟然是真的有挂(详细辅助介绍教程);1、wepoker透...
必备攻略!wepoker透视最... 必备攻略!wepoker透视最简单三个步骤,wepoker可以开透视吗,AI教程(有挂透明);wep...
一分钟揭秘!wepoker是不... 一分钟揭秘!wepoker是不是有人用挂,wepoker破解器激活码,细节揭秘(有挂软件)1、玩家可...