我正在开发一个应该在Android服务中启动蓝牙扫描的应用程序(没有请求用户启用蓝牙因为应用程序根本没有任何活动)我的应用程序没有UI或更确切地说我的设备没有LCD / LED显示。经过详尽的搜索和引用google和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消息也不会显示。
我的代码中没有启用&amp;扫描蓝牙?
答案 0 :(得分:0)
你已经用错误的方法添加了代码,这就是为什么它不会被称为只使用onStartCommand
方法。为此请检查service life cycle
@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);
}