2015-11-05 116 views
3

我正在开发基于蓝牙的应用程序。Android 5.0中的蓝牙连接问题(棒棒糖)

有一个用户想要通过蓝牙将数据分享给其他用户的手机。 我正面临一个问题。

设备与其他设备配对。但如果配对的设备有Android 5.0(Lollipop)及以上版本的Android OS,那么我面临问题,问题是当屏幕关闭时,连接将会丢失。在Android 5.0下,它正常工作。 “短棍棒棒糖”那么为什么会发生这种情况呢?

这是我的代码。

private BluetoothAdapter mAdapter; 
    mAdapter = BluetoothAdapter.getDefaultAdapter(); 
      if (!mAdapter.isEnabled()) { 
       @SuppressWarnings("static-access") 
       Intent enableBTIntent = new Intent(mAdapter.ACTION_REQUEST_ENABLE); 
       startActivity(enableBTIntent); 
      } 

    IntentFilter filter = new IntentFilter(); 
    filter.addAction(BluetoothDevice.ACTION_FOUND); 
    filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED); 
    filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); 
    registerReceiver(mReceiver1, filter); 
    find = new ArrayList<String>(); 
    mAdapter.startDiscovery(); 


final BroadcastReceiver mReceiver1 = new BroadcastReceiver() { 
     public void onReceive(Context context, Intent intent) { 
      String action = intent.getAction(); 
     if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) { 
      pdialog = ProgressDialog.show(FindPeopleActivity.this, 
        "Please wait", "Device Scanning..."); 
      // discovery starts, we can show progress dialog or perform 
      // other tasks 
     } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED 
       .equals(action)) { 
      if (pdialog != null && pdialog.isShowing()) 
       pdialog.dismiss(); 
     } else if (BluetoothDevice.ACTION_FOUND.equals(action)) { 
      // bluetooth device found 
      BluetoothDevice device = (BluetoothDevice) intent 
        .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
      find.add(device.getAddress()); 

     } 

    } 
}; 

清单文件

<uses-permission android:name="android.permission.BLUETOOTH" /> 
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> 

如果有任何解决方案,链接,任何差别接近那里将是巨大的,并帮助不少。提前致谢。

+0

是否有可能在数据传输是当时配对的设备必须解锁。 (仅适用于Lolipop设备)您可以为屏幕锁定lolipop设备做一些硬编码吗? – Vrajesh

回答

2

盯着Android 6.0只包含清单权限是不够的。你必须明确地向用户询问关于被认为是“危险”的每个权限.BluetoothDevice. ACTION_FOUND需要BLUETOOTH and ACCESS_COARSE_LOCATION permissions

uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 

如果不工作,然后发布您的错误日志。

+2

** ,我认为它用于GPS ..所以为什么我在蓝牙中使用它?** –

+0

在Android 6.0中对蓝牙设备进行后台扫描,包括信标,现在需要android.permission.ACCESS_FINE_LOCATION或android.permission.ACCESS_COARSE_LOCATION。 :) – Aks4125

+1

我添加此权限,并尝试..但不工作。:-(有一些其他的想法?? –

0

我有不同的方法来处理这个问题。

设备将被唤醒,直到数据,文件等不传输。当完成后,在onDestroy()活动中释放此锁。

所以这只适用于android 5.0或更高版本。您必须先跟踪SDK,然后找到API Level 21,然后实施此Apporch。

看到这一点:Screen Lock and unlock

+1

是它的好主意,但首先设备没有找到从低版本到更高版本。例如我的棒棒糖设备找不到糖果设备 –

+0

所以你可以检查,默认的蓝牙设置?是否有设备列出或没有?有时连接丢失,甚至已经连接。例如,请参阅蓝牙设备在kitkate设备(这是搜索lolipop设备)是Lolipop设备可见如果可见,那么检查设备是否能配对?如果没有,那么开关蓝牙,并再次从我们的kitkate设备扫描 – Vrajesh

+1

是的,我检查默认的蓝牙设置,设备完美列出..但在我的应用程序设备不可见... –