android - Can`t run BlueTooth discovery from Service -
i can`t run startdiscovery (for bluetooth) service. service run sleep wakefulbroadcastreceiver (by timer). source code of service:
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()); } } }; }
in log see:
service onstartcommand ==bt: run btscanner ==bt: end btscanner
but don`t see:
==bt: started ==bt: finished
and list of discovered devices.
in manifest permissions installed:
<uses-permission android:name="android.permission.wake_lock" /> <uses-permission android:name="android.permission.bluetooth" /> <uses-permission android:name="android.permission.bluetooth_admin" />
and service enabled in application field of manifest:
<service android:name=".locationservice" android:enabled="true" android:exported="true" />
what did wrong? tnx.
are sure receiver has not been unregistered in ondestroy method before having time handle action? put break point in see what's happenning in there?
Comments
Post a Comment