无法 运行 从服务中发现蓝牙
Can`t run BlueTooth discovery from Service
我无法 运行 从服务启动发现(用于蓝牙)。
服务 运行 通过 WakefulBroadcastReceiver(通过计时器)从睡眠中恢复。
服务源代码:
public class LocationService extends Service implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {
private static final String TAG = "LocationService";
private BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "Service onCreate");
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(mBTReceiver, filter);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "Service onStartCommand");
BTscanner();
return START_NOT_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
if (btAdapter != null) {
btAdapter.cancelDiscovery();
}
unregisterReceiver(mBTReceiver);
}
private void BTscanner() {
Log.e(TAG, "==BT: Run BTscanner");
btAdapter.cancelDiscovery();
btAdapter.startDiscovery();
Log.e(TAG, "==BT: End BTscanner");
}
private final BroadcastReceiver mBTReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
Log.e(TAG, "==BT: Started");
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
Log.e(TAG, "==BT: Finished");
} else if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = (BluetoothDevice) intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Log.e(TAG, "==BT: " + device.getAddress());
}
}
};
}
在日志中我看到:
Service onStartCommand
==BT: Run BTscanner
==BT: End BTscanner
但没看到:
==BT: Started
==BT: Finished
和发现的设备列表。
在 Manifest 中安装了所有权限:
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
并在 Manifest 的应用领域启用服务:
<service
android:name=".LocationService"
android:enabled="true"
android:exported="true" />
我做错了什么?
Tnx.
您确定您的接收器在有时间处理任何操作之前没有在 onDestroy 方法中注销吗?我会在其中设置一个断点以查看其中发生了什么?
我想,我找到了解决办法。
我在智能手机上安装和 运行 启用蓝牙的应用程序时出现问题。
安装应用程序和手册后deactivate/activate蓝牙设备一切正常。
可能是某种硬件故障。
然后重新安装应用两次,问题没有重复
我无法 运行 从服务启动发现(用于蓝牙)。 服务 运行 通过 WakefulBroadcastReceiver(通过计时器)从睡眠中恢复。 服务源代码:
public class LocationService extends Service implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {
private static final String TAG = "LocationService";
private BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "Service onCreate");
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(mBTReceiver, filter);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "Service onStartCommand");
BTscanner();
return START_NOT_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
if (btAdapter != null) {
btAdapter.cancelDiscovery();
}
unregisterReceiver(mBTReceiver);
}
private void BTscanner() {
Log.e(TAG, "==BT: Run BTscanner");
btAdapter.cancelDiscovery();
btAdapter.startDiscovery();
Log.e(TAG, "==BT: End BTscanner");
}
private final BroadcastReceiver mBTReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
Log.e(TAG, "==BT: Started");
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
Log.e(TAG, "==BT: Finished");
} else if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = (BluetoothDevice) intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Log.e(TAG, "==BT: " + device.getAddress());
}
}
};
}
在日志中我看到:
Service onStartCommand
==BT: Run BTscanner
==BT: End BTscanner
但没看到:
==BT: Started
==BT: Finished
和发现的设备列表。
在 Manifest 中安装了所有权限:
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
并在 Manifest 的应用领域启用服务:
<service
android:name=".LocationService"
android:enabled="true"
android:exported="true" />
我做错了什么? Tnx.
您确定您的接收器在有时间处理任何操作之前没有在 onDestroy 方法中注销吗?我会在其中设置一个断点以查看其中发生了什么?
我想,我找到了解决办法。
我在智能手机上安装和 运行 启用蓝牙的应用程序时出现问题。
安装应用程序和手册后deactivate/activate蓝牙设备一切正常。
可能是某种硬件故障。
然后重新安装应用两次,问题没有重复