ここ数日、Samsung 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 分間しか実行されません。デバイスが無期限に検出可能になることを知っているので、これはかなりイライラします! (手動で Bluetooth 設定にアクセスし、Bluetooth の可視性を「タイムアウトなし」に設定することができました!)
私は成功せずに答えを探しました...多くの投稿は、(私のような比較的熟練していないプログラマーにとって)難解な解決策のように見えますが、あいまいすぎる(*)、混乱する(**)、またはまったく間違っている. この問題を解決する簡単でわかりやすい答え (もちろん存在する場合) をいただければ幸いです。
(*) Android 2.1 の Bluetooth を無期限に検出可能にします
(**) Android Bluetooth Discoverability Android アプリケーションの Bluetooth 表示期間の延長 (回答セクション)
マニフェスト:
<?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>
編集: 人々に少しのコンテキストを提供するために、このアプリケーションの目標の 1 つは、近くにあるすべての Bluetooth デバイスを検出可能にして、それらと直接通信できるようにすることです。ほとんどのスマートフォンは短時間 (通常は 2 分*) 検出可能であり、ユーザーが直接検出可能性 (= 可視性) を有効にした場合にのみ検出可能であるため、デバイスをスキャンして自動的にデータを交換するアプリを実装することは不可能です。(* ユーザーは通常、可視性を「タイムアウトなし」に設定できますが、その場合、スマートフォンの Bluetooth 設定で直接そのオプションを設定する必要があり、これはあまり洗練されたソリューションではありません...)