我正在尝试在后台服务中发现可用的蓝牙设备。我被困在“startActivityForResult”部分,因为服务不是 Activity 。请帮忙...
private void CheckBTState() {
// TODO Auto-generated method stub
// Check for Bluetooth support and then check to make sure it is turned on
// If it isn't request to turn it on
// List paired devices
// Emulator doesn't support Bluetooth and will return null
Log.d("check","4");
if (btAdapter.isEnabled()) {
// out.append("\nBluetooth is enabled...");
// Starting the device discovery
btAdapter.startDiscovery();
Log.d("check","5");
} else {
Intent enableBtIntent = new Intent(btAdapter.ACTION_REQUEST_ENABLE);
Log.d("check","6");
STUCK OVER HERE.... DON'T NO HOW TO CALL startActivityForResult IN SERVICE
// ((Activity) context).startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
// activity.startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}
最佳答案
您必须在服务开始之前激活蓝牙。
只需将以下代码添加到您的 Activity 中:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == <needed value>) {
}
super.onActivityResult(requestCode, resultCode, data);
}
关于java - Android 蓝牙搜索服务中可用设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32991238/