Android:设置蓝牙可发现性无限制

Android: Set Bluetooth Discoverability Unbounded

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

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 分钟。这是相当令人沮丧的,因为我知道该设备可以无限期地被发现! (我可以手动访问蓝牙设置并将蓝牙可见性设置为 'Never Time Out'!)

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

(*) Make Bluetooth on Android 2.1 discoverable indefinitely

(**) Extend Android Bluetooth Discoverability Android Application Bluetooth visibility duration(答案部分)

清单:

<?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 分钟*)被发现,并且只有当用户直接启用可发现性(= 可见性)时,扫描设备并自动交换数据的应用程序才能实现。 (* 用户通常可以将可见性设置为 'No Time Out',但这需要用户直接在智能手机的蓝牙设置下设置该选项,这不是一个非常优雅的解决方案...)

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

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

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

MDisc.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,300)

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

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

这将永远循环发现,如果您想在找到设备时停止,请在接收器的 ACTION_FOUND 侦听器中调用 cancelDiscovery(),如果您需要查找设备,也可以在此处进行一些检查特定设备 - 在我的例子中,我再次检查特定设备的 mac 地址,因此发现将继续,直到找到它。

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

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

和平。

对我有用。

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

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

我对我拥有的三台设备得出相同的结论。

  • ANDROID v 4.3 及更高版本:EXTRA_DISCOVERABLE_DURATION 0 无限制
  • ANDROIND v 4.1 : EXTRA_DISCOVERABLE_DURATION 0 最多 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);