Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/179.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Android中使用蓝牙激活创建服务_Android_Service_Bluetooth_Discovery - Fatal编程技术网

在Android中使用蓝牙激活创建服务

在Android中使用蓝牙激活创建服务,android,service,bluetooth,discovery,Android,Service,Bluetooth,Discovery,我想在Android中创建一个服务,它首先会询问用户是否要启动蓝牙并设置蓝牙发现 我的问题是: 我可以在服务中启动以下活动吗 if (!mBluetoothAdapter.isEnabled()) { Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent, 0); } // Set Phone Discov

我想在Android中创建一个服务,它首先会询问用户是否要启动蓝牙并设置蓝牙发现

我的问题是:

  • 我可以在服务中启动以下活动吗

    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);
    
  • 我想在应用程序的生命周期内设置手机的可发现性。可能吗

  • 我想访问SD卡上的可用空间。我该怎么做

  • 提前感谢。

    1)您无法使用该特定代码,因为您无法从服务调用
    startActivity()
    。您需要使用以下代码来启用蓝牙:

    BluetoothAdapter mAdapter = BluetoothAdapter.getDefaultAdapter();
    mAdapter.enable();
    
    文件上说

    boolean enable()
    
    打开本地蓝牙适配器如果没有明确的用户操作,请勿使用以打开蓝牙

    因此,您需要确保首先提示用户。此外,您无法将设备设置为可在服务中发现,因为唯一的方法是使用
    startActivity()
    ,因此您需要在某种配置活动中执行该部分

    2) 不,让蓝牙设备可以被发现会让它面临许多安全问题,所以这不仅是不可能的,而且是个坏主意


    3) 如果要写入SD卡,只需向清单文件添加
    write\u EXTERNAL\u STORAGE
    权限,然后就可以使用标准Java文件IO。

    谢谢您的帮助。我认为在服务中启用蓝牙发现是不可能的。对于我问的第三个问题,我想访问SD卡上的可用空间。我该怎么做?Android的Android Java library Java.io不提供标准Java.io提供的任何访问当前文件系统可用空间的功能。但我发现android.os的StatFs类解决了我的问题。这不是很好的建议,因为谷歌不建议在未询问用户的情况下启用蓝牙。。可以这样做,但必须在清单中设置BT管理员的特殊权限。