在android服务中启动蓝牙

问题描述 投票:0回答:1

我正在开发一个应用程序,该应用程序应该在 Android 服务中启动蓝牙扫描(无需请求用户启用蓝牙,因为该应用程序根本没有任何活动),并且我的应用程序没有 UI,或更准确地说,我的设备没有 LCD/LED 显示屏。经过详尽的搜索并参考谷歌和 stackoverflow 中的链接后,我可以部分找到解决方案并编写以下代码。

这里我从广播接收器启动一个服务,在服务中我启动了蓝牙,但是蓝牙似乎没有打开,我尝试过直接使用代码启用蓝牙

BluetoothAdapter.getDefaultAdapter().enable()

也尝试过

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

但是在这两种情况下,蓝牙都不会打开。

这是清单文件:

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
        }
    }
};
}

服务类中使用的 Toast 消息也不会显示。

我的代码中没有启用和扫描蓝牙可能有什么问题?

android bluetooth broadcastreceiver android-service
1个回答
0
投票

您在错误的方法中添加了代码,这就是为什么它不被调用,只需使用

onStartCommand
方法。为此,请检查服务生命周期

@Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub
        Toast.makeText(getApplicationContext(), "Bluetooth Service Started", Toast.LENGTH_LONG).show();

        Log.e("in onStartCommand", "onStartCommand");
        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();

       return super.onStartCommand(intent, flags, startId);
    }
© www.soinside.com 2019 - 2024. All rights reserved.