安卓:设置蓝牙可发现无界 [英] Android: Set Bluetooth Discoverability Unbounded

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

问题描述

我已经花了过去几天试图让这使我的三星Galaxy S3的迷你应用(Android 2.1.4)中发现的时间是无限的金额。我的code目前看起来如下:

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'!)

我到处寻找无功而返答案...很多帖子给什么(对于一个相对不熟练的程序员比如我)像神秘的解决方案,要么太模糊(*),混淆(**)或彻头彻尾的错了。一个简单明确的答案解决这一问题(如果存在的话,当然!)将大大AP preciated!

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!

(*)
<一href=\"http://stackoverflow.com/questions/3190623/make-bluetooth-on-android-2-1-discoverable-indefinitely\">Make蓝牙An​​droid 2.1的发现无限期

(**)
扩展Android的蓝牙可发现
<一href=\"http://stackoverflow.com/questions/6899976/android-application-bluetooth-visibility-duration\">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分钟通常*)和仅如此,当用户直接使曝光度(=可见性),该扫描装置和应用程式自动交换数据是不可能实现的。 (*用户通常可以设置能见度为无超时,但需要用户直接在他们的智能手机的蓝牙设置,这不是一个很优雅的解决方案......来设置这个选项)

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 4.3 v和更高:EXTRA_DISCOVERABLE_DURATION 0作品没有限制

  • ANDROIND V4.1版本:0 EXTRA_DISCOVERABLE_DURATION最大是1小时。必须手动更改为参数没有任何限制。

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

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