2

在过去的几天里,我一直在尝试制作一个应用程序,让我的三星 Galaxy S3 mini (Android 2.1.4) 在“无限”时间内被发现。我的代码目前如下所示:

package com.example.downtoone;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.widget.Toast;

import com.example.downtoone.*;
import android.bluetooth.*;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

public class MainActivity extends Activity {

    private BluetoothAdapter mBluetoothAdapter = null;

    // Intent request codes
    private static final int REQUEST_CONNECT_DEVICE = 1;
    private static final int REQUEST_ENABLE_BT = 2;
    private static final int REQUEST_ENABLE_DSC = 3;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if (mBluetoothAdapter == null) {
            Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show();
            finish();
            return;
        }
    }   
    @Override
    public void onStart() {
        super.onStart();

        if (!mBluetoothAdapter.isEnabled()) {
            Intent MDisc = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
            MDisc.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,0);
            startActivityForResult(MDisc, REQUEST_ENABLE_DSC);
        }
    }
    @Override
    public void onRestart(){
        super.onRestart();
    }
    @Override
    public void onResume() {
        super.onResume();
    }
    @Override
    public void onStop() {
        super.onStop();
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
        case REQUEST_CONNECT_DEVICE:
            if (resultCode == Activity.RESULT_OK) {

            }
            break;
        case REQUEST_ENABLE_BT:
            if (resultCode == Activity.RESULT_CANCELED) {
                Toast.makeText(this, "BLUETOOTH NEEDS TO BE ENABLED AND DISCOVERABLE", Toast.LENGTH_SHORT).show();
                finish();
            }
            break;
        case REQUEST_ENABLE_DSC:
            if (resultCode == Activity.RESULT_CANCELED) {
                Toast.makeText(this, "BLUETOOTH NEEDS TO BE ENABLED AND DISCOVERABLE", Toast.LENGTH_SHORT).show();
                //finish();
            }
            break;
        }
    }

    public void finishBTsetup(){

    }
}

尽管我将时间设置为“0”,但可发现性仅运行 2 分钟。这是相当令人沮丧的,因为我知道该设备可以无限期地被发现!(我可以手动访问蓝牙设置并将蓝牙可见性设置为“永不超时”!)

我一直在寻找一个没有成功的答案......许多帖子给出了(对于像我这样的相对不熟练的程序员)看起来过于模糊(*)、令人困惑(**)或完全错误的神秘解决方案。一个简单直接的答案解决这个问题(如果它当然存在!)将不胜感激!

(*) 使 Android 2.1 上的蓝牙可无限期被发现

(**) 延长 Android 蓝牙可发现 性 Android 应用程序蓝牙可见性持续时间 (答案部分)

显现:

<?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.downtoone"
        android:versionCode="1"
        android:versionName="1.0" >

        <uses-sdk
            android:minSdkVersion="14"
            android:targetSdkVersion="14" />
        <uses-permission android:name="android.permission.BLUETOOTH"/>
        <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
        <uses-permission android:name="android.permission.WRITE_SETTINGS" />  
        <uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />


        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name=".MainActivity"
                android:label="@string/app_name">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />

                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>

    </manifest>

编辑: 为了给人们一些背景信息,这个应用程序的目标之一是尝试发现附近的所有蓝牙设备,以便它可以直接与他们交谈。由于大多数智能手机可以在很短的时间内被发现(通常为 2 分钟*),并且只有当用户直接启用可发现性(= 可见性)时,才能实现扫描设备并自动交换数据的应用程序。(* 用户通常可以将可见性设置为“无超时”,但这需要用户直接在智能手机的蓝牙设置下设置该选项,这不是一个非常优雅的解决方案......)

4

6 回答 6

2

我在我拥有的三台设备上得出了相同的结论。

  • ANDROID v 4.3 及更高版本:EXTRA_DISCOVERABLE_DURATION 0 无限制
  • ANDROID v 4.1:EXTRA_DISCOVERABLE_DURATION 0 最长为 1 小时。必须手动更改为无参数限制。
于 2015-03-21T11:25:20.450 回答
1

以上 api 级别 14 EXTRA_DISCOVERABLE_DURATION 0 以无限限制工作,但低于此限制最多工作 1 小时。

这段代码对我有用

Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 0);
startActivityForResult(intent, Utils.REQUEST_DEVICE_DISCOVERABLE);
于 2015-05-13T04:45:24.547 回答
0

根据 Android 文档,可发现的最长时间上限为 300 秒。你不能让 BT 永远被发现。

请参阅:http: //developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#ACTION_REQUEST_DISCOVERABLE

为了获得 300 秒的最大周期,您需要将一行代码更改为:

MDisc.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,300)
于 2015-01-20T02:12:55.807 回答
0

我不确定这是否适合您的应用程序,或者是否有其他原因您不想这样做,但为什么不只是在默认时间发现,然后在超时时重新启动发现?这种方式在技术上将是一个无限的发现,它只会触发一个非常轻微的暂停,我在我的应用程序中使用了这种方法,使用广播接收器。

我将 startDiscovery() 放在 onStart() 方法中以在活动开始时触发发现,然后在广播接收器的 onReceive() 方法中收听 ACTION_DISCOVERY_FINISHED,这里再次调用 startDiscovery()。

这将永远循环发现,如果您想在找到设备时停止然后在您的接收器的 ACTION_FOUND 侦听器中调用 cancelDiscovery(),如果您需要查找特定设备,您也可以在此处进行一些检查 - 在我的情况下,我是检查特定设备的 MAC 地址,以便发现将继续,直到找到为止。

不确定这是否有用,但如果您需要更多详细信息,请告诉我。

于 2015-01-23T10:42:48.627 回答
0

我得出的结论是,它无法完成,除非用户转到蓝牙 > 设置 > 可见性超时并相应地设置超时。

和平。

于 2015-01-27T05:46:17.530 回答
0

对我有用。

Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
enableBtIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 0);
startActivityForResult(enableBtIntent, Utils.REQUEST_DEVICE_DISCOVERABLE);

手动禁用蓝牙,然后运行代码。它会起作用的,是的。

于 2015-01-30T16:58:30.987 回答