与蓝牙激活Android中创建服务 [英] Creating Service with Bluetooth activation in Android
问题描述
我想创建一个Android的服务,如果他们想启动蓝牙并设置蓝牙发现它最初将询问用户。
我的问题是:
-
我可以在以下活动服务推出?
如果(!mBluetoothAdapter.isEnabled())
{
意图enableBtIntent =新意图(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent,0);
}
//设置手机可发现300秒。
意图discoverableIntent =新意图(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,600);
startActivity(discoverableIntent); -
我想设置discoverabilty手机上的应用程序的生命周期。这可能吗?
-
我要访问SD卡上的可用空。我应该怎么办呢?
先谢谢了。
1)您不能使用特殊的code,因为你不能从一个叫 startActivity()
服务。您将需要使用以下code,使蓝牙:
BluetoothAdapter mAdapter = BluetoothAdapter.getDefaultAdapter();
mAdapter.enable();
该文件说。
布尔使能()
在本地蓝牙适配器接通,请不要在未显式用户操作使用开启蓝牙。
块引用>所以,你需要确保先提示用户。您也可以不设置该设备是在服务发现,因为这样做的唯一方法是用
startActivity()
,所以你需要做的那一部分在某种配置活动。2)没有,有一个蓝牙设备可发现打开它高达安全问题众多,因此,它不仅是不可能的,这是一个坏主意。
3)如果你想写信给你只需要在
WRITE_EXTERNAL_STORAGE
的权限添加到您的清单文件的SD卡,然后就可以使用标准的Java文件IO。I want to create a service in Android which will initially ask user if they want to start Bluetooth and set the Bluetooth discovery.
My question is:
Can I launch in the service following activities?
if (!mBluetoothAdapter.isEnabled()) { Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, 0); } // Set Phone Discoverable for 300 seconds. Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 600); startActivity(discoverableIntent);
I want to set discoverabilty of the phone on for lifetime of application. Is it possible?
I want to access empty space available on SD card. How should i do it?
Thanks in advance.
解决方案1) You cannot use that particular code because you cannot call
startActivity()
from a service. You would need to use the following code to enable the Bluetooth:BluetoothAdapter mAdapter = BluetoothAdapter.getDefaultAdapter(); mAdapter.enable();
The documentation says
boolean enable()
Turn on the local Bluetooth adapter—do not use without explicit user action to turn on Bluetooth.
So you need to be sure to prompt the user first. Also you cannot set the device to be discoverable in the service because the only way to do that is with the
startActivity()
so you would need to do that part in some kind of a configuration Activity.2) No, having a Bluetooth device be discoverable opens it up to a multitude of security concerns, so not only is it not possible, it's a bad idea.
3) If you want to write to the SDCard you just need to add the
WRITE_EXTERNAL_STORAGE
permission to your manifest file and then you can use standard Java File IO.这篇关于与蓝牙激活Android中创建服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!