i'm trying calculate bluetooth rssi , found example, broadcastreceiver not working. code this:
private final broadcastreceiver receiver = new broadcastreceiver(){ @override public void onreceive(context context, intent intent) { string action = intent.getaction(); if(bluetoothdevice.action_found.equals(action)) { int rssi = intent.getshortextra(bluetoothdevice.extra_rssi,short.min_value); string name = intent.getstringextra(bluetoothdevice.extra_name); textview rssi_msg = (textview) findviewbyid(r.id.textview1); rssi_msg.settext(rssi_msg.gettext() + name + " => " + rssi + "dbm\n"); } } };
registered through this:
registerreceiver(receiver, new intentfilter(bluetoothdevice.action_found));
and when button clicked, btadapter.startdiscovery(); working. nothing changed in textview.
can please have advice me?
edit again: changed code little more , i'll show whole code.
public class blutooth extends activity { private bluetoothadapter btadapter = bluetoothadapter.getdefaultadapter(); @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_blutooth); button boton = (button) findviewbyid(r.id.button1); boton.setonclicklistener(new onclicklistener(){ public void onclick(view v) { btadapter.startdiscovery(); } }); } @override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.blutooth, menu); return true; } }
and receiver class is
public class testreceiver extends broadcastreceiver { @override public void onreceive(context context, intent intent) { // todo auto-generated method stub string name = intent.getaction(); string device = intent.getstringextra(bluetoothdevice.extra_name); string rssi_msg = integer.tostring(intent.getshortextra(bluetoothdevice.extra_rssi,short.min_value)); toast.maketext(context, name, toast.length_short).show(); } }
i'm trying toast intent.getaction();
nothing happened. think bluetoothdevice.action_found
not broadcast.
edit again : i'm using android 6.0 version. , whole manifest this:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.blutooth" android:versioncode="1" android:versionname="1.0" > <uses-feature android:name="android.hardware.bluetooth" /> <uses-permission android:name="android.permission.bluetooth" /> <uses-permission android:name="android.permission.bluetooth_admin" /> <uses-permission android:name="android.permission.access_fine_location" /> <uses-permission android:name="android.permission.access_coarse_location" /> <uses-sdk android:minsdkversion="23" android:targetsdkversion="23" /> <application android:allowbackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <activity android:name=".blutooth" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <receiver android:name=".testreceiver"> <intent-filter> <action android:name="bluetoothdevice.action_found"> </action></intent-filter> </receiver> </application> </manifest>
i heard on 6.0 version need additional permission use action_found
. so, added access_fine_location
, access_coarse_location
. , added filter.addaction(bluetoothdevice.action_name_changed);
code in activity. action_name_changed
worked fine , can bluetoothdevice.extra_name
. bluetoothdevice.extra_rssi,short.min_value
not working. print default value -32768. still bluetoothdevice.action_found
not working.
i found answer. android 6.0 needs additional permission, adding uses-permission manifest not working. have check permission user. write code this:
int permissioncheck = contextcompat.checkselfpermission(thisactivity, manifest.permission.access_coarse_location); bluetoothadapter bluetoothadapter = bluetoothadapter.getdefaultadapter(); if (!bluetoothadapter.isenabled()) bluetoothadapter.enable(); lescanner = bluetoothadapter.getbluetoothlescanner(); button boton = (button) findviewbyid(r.id.button1); boton.setonclicklistener(new onclicklistener(){ public void onclick(view v) { lescanner.startscan(scancallback); } });
this code show permission pop up. many answers.
Comments
Post a Comment