在过去的几天里,我一直在尝试制作一个应用程序,让我的三星 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 蓝牙可发现 性 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 分钟*),并且只有当用户直接启用可发现性(= 可见性)时,才能实现扫描设备并自动交换数据的应用程序。(* 用户通常可以将可见性设置为“无超时”,但这需要用户直接在智能手机的蓝牙设置下设置该选项,这不是一个非常优雅的解决方案......)