我正在开发基于蓝牙的应用程序。
有一位用户希望通过蓝牙与其他用户的手机共享数据。 我正面临一个问题。
设备与其他设备配对。但如果配对设备有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 :(得分:2)
与Android 6.0相比,仅在清单上包含权限是不够的。您必须明确询问用户有关被视为“危险”的每项权限.BluetoothDevice. ACTION_FOUND
需要BLUETOOTH and ACCESS_COARSE_LOCATION permissions
uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
如果这不起作用,请发布您的错误日志。
答案 1 :(得分:0)
我有不同的approch来处理这个问题。
设备将被唤醒,直到数据,文件等不传输。完成后,在活动onDestroy()中释放此锁。
所以这只适用于Android 5.0或更高版本。你必须首先跟踪sdk然后找到API Level 21并且ablove然后实现这个apporch。