0

Android サービスで Bluetooth スキャンを開始する必要があるアプリケーションを開発しています (ユーザーに Bluetooth coz を有効にするように要求することなく、アプリにはまったくアクティビティがありません)。Googleとstackoverflowのリンクを徹底的に検索して参照した後、解決策を部分的に見つけて、以下のコードを書きました。

ここで、ブロードキャストレシーバーからサービスを開始し、サービスでBluetoothを開始しますが、Bluetoothがオンにならないようです。コードを使用してBluetoothを直接有効にしようとしました

BluetoothAdapter.getDefaultAdapter().enable()

そしてまた試した

Intent btIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
btIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
serviceContext.startActivity(btIntent);

しかし、どちらの場合も、Bluetooth はオンになりません。

マニフェスト ファイルは次のとおりです。

android:versionCode="1"
android:versionName="1.0" android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <receiver android:name=".StartReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
        </intent-filter>
    </receiver>

    <service android:name=".StartService"></service>
</application>

ブロードキャスト レシーバー クラスは次のとおりです。

public class StartReceiver extends BroadcastReceiver{

@Override
public void onReceive(Context context, Intent intent) {
    Intent receiverIntent = new Intent(context, StartService.class);
    context.startService(receiverIntent);
    Log.i("Autostart", "started");
}
}

そしてサービスクラス:

public class StartService extends Service{

private static final String TAG = "BluetoothService";
BluetoothAdapter btAdapter;
BluetoothDevice device;

Context serviceContext;

@Override
public IBinder onBind(Intent arg0) {
    return null;
}

@Override
public void onStart(Intent intent, int startid){
    Toast.makeText(this, "Bluetooth Service Started", Toast.LENGTH_LONG).show();
    Log.d(TAG, "OnStart");

    BluetoothAdapter.getDefaultAdapter().enable();
    //Intent btIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    //btIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    //serviceContext.startActivity(btIntent);

    registerReceiver(bcReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));

        btAdapter.startDiscovery();

 }

@Override
public void onDestroy(){
    BluetoothAdapter.getDefaultAdapter().disable();
    unregisterReceiver(bcReceiver);
    btAdapter.cancelDiscovery();
}

private final BroadcastReceiver bcReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {

        String action = intent.getAction();
        if(BluetoothDevice.ACTION_FOUND.equals(action)){

            //Do Something
        }
    }
};
}

また、サービス クラスで使用されるトースト メッセージも表示されません。

Bluetooth を有効にしてスキャンしないためのコードの何が間違っている可能性がありますか?

4

1 に答える 1