Android:将蓝牙可发现性设置为无界 [英] Android: Set Bluetooth Discoverability Unbounded

查看:44
本文介绍了Android:将蓝牙可发现性设置为无界的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have spent the last couple of days trying to make an app that keeps my Samsung Galaxy S3 mini (Android 2.1.4) discoverable for an "infinite" amount of time. My code looks currently as follows:

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 分钟.这相当令人沮丧,因为我知道该设备可以在无限时间内被发现!(我可以手动访问蓝牙设置并将蓝牙可见性设置为永不超时"!)

Despite the fact that I am setting the time to '0', discoverability only runs for 2minutes. This is rather frustrating since I know the device can handle to be discoverable for an indefinite amount of time! ( I could manually access the bluetooth settings and set Bluetooth Visibility to 'Never Time Out'!)

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

I've looked all over for an answer without success... many posts give what (for a relative unskilled programmer such as me) look like arcane solutions that are either too vague(*), confusing(**) or downright wrong. A simple straightforward answer solving this issue (if it exists of course!) would be greatly appreciated!

(*)让 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>

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

To give people a little context, one of the goals of this application is to try and be DISCOVERABLE to all nearby Bluetooth devices so that it can directly talk to them. Since most smartphones are discoverable for short amounts of time (2min usually*) and only so when the user directly enables discoverability (= visibility), apps that scan for devices and automatically exchange data are impossible to implement. (* The user can usually set the visibility to 'No Time Out', but that requires the user to set that option directly under Bluetooth Settings of their smartphone, which is not a very elegant solution...)

推荐答案

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

I come to the same conclusion on three devices I have.

  • ANDROID v 4.3 及更高版本:EXTRA_DISCOVERABLE_DURATION 0 无限制工作
  • ANDROIND v 4.1:EXTRA_DISCOVERABLE_DURATION 0 最多为 1 小时.必须手动更改为无参数限制.

这篇关于Android:将蓝牙可发现性设置为无界的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆