Android的蓝牙服务冻结屏幕? [英] Android Bluetooth Service Freezes Screen?

查看:172
本文介绍了Android的蓝牙服务冻结屏幕?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图解决开发蓝牙连接的应用程序时,我有这个问题。理想情况下,应用程序会自动与蓝牙设备连接,启动该服务,然后阅读 MainActivity 中的数据。当设备联机眼下,一切都完美。问题是:当设备处于脱机状态,它将应用程序取消其服务之前需要约8秒,而这个过程将冻结屏幕,即使我明确地把 doBindService()在sepa​​rete线程。

I am trying to solve the problem I have when developing a bluetooth connection application. Ideally the app should automatically connect with the bluetooth device, start the service and read the data in MainActivity. Right now when the device is online, everything works perfectly. The problem is: when the device is offline, it will take about 8 seconds before the app cancels its service, and this process will freeze the screen even if I explicitly put the doBindService()in a separete thread.

公共类BluetoothManager {

public class BluetoothManager {

private static final String TAG = BluetoothManager.class.getName();
private static final UUID myUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

public static BluetoothSocket connect(BluetoothDevice dev) throws IOException{

    BluetoothSocket sock = null;
    BluetoothSocket socketFallback = null;
    Log.d(TAG,"Start Bluetooth Connection...");
    try
    {
        sock = dev.createRfcommSocketToServiceRecord(myUUID);
        Log.d(TAG, "Probably gonna wait here...");
        sock.connect();
    }catch (Exception e1){
        Log.e(TAG, "There was an error while establishing Bluetooth connection, Failing back...", e1);

    }
    return sock;
}

}

所以我做了一个独立的应用我的主要的应用程序这里,其中只包含蓝牙连接功能。任何人都可以请看看在项目和任何建议,欢迎!谢谢!

So I made a stand-alone app out of my main app here, which only contains bluetooth connection function. Could anyone please take a look at the project and any suggestions are welcome! Thanks!

推荐答案

添加pernissions在mainefest

包=com.example.sairamkrishna.myapplication>

package="com.example.sairamkrishna.myapplication" >

  android:allowBackup="true"

  android:icon="@mipmap/ic_launcher"

  android:label="@string/app_name"

  android:theme="@style/AppTheme" >

  <activity

     android:name=".MainActivity"

     android:label="@string/app_name" >


     <intent-filter>

        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />

     </intent-filter>

  </activity>

这篇关于Android的蓝牙服务冻结屏幕?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆