Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bluetooth ACTION_FOUND broadcastReceiver is not working

I'm trying to calculate bluetooth RSSI and found some example, but broadcastReceiver is not working. The code is 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 is clicked, BTAdapter.startDiscovery(); is working. But nothing changed in textview.

Can you please have some advice for me?

Edit Again: I changed my code little more and I'll show my 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 to Toast intent.getAction(); but nothing happened. I think BluetoothDevice.ACTION_FOUND is not broadcast.

edit again : I'm using android 6.0 version. And my whole manifest is 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 over 6.0 version need additional permission to use ACTION_FOUND. So, I added ACCESS_FINE_LOCATION and ACCESS_COARSE_LOCATION. And I added filter.addAction(BluetoothDevice.ACTION_NAME_CHANGED); code in my activity. ACTION_NAME_CHANGED is worked fine and I can get BluetoothDevice.EXTRA_NAME. But BluetoothDevice.EXTRA_RSSI,Short.MIN_VALUE is not working. It print default value -32768. Still BluetoothDevice.ACTION_FOUND is not working.

like image 759
L. Woo Avatar asked Jul 21 '16 19:07

L. Woo


People also ask

What is a BroadCastReceiver?

Android BroadcastReceiver is a dormant component of android that listens to system-wide broadcast events or intents. When any of these events occur it brings the application into action by either creating a status bar notification or performing a task.


1 Answers

I finally found answer. Android 6.0 needs additional permission, but Just adding uses-permission to Manifest is not working. You have to check permission to user. I write my code like 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 you permission pop up. Thanks for many answers.

like image 134
L. Woo Avatar answered Dec 03 '22 15:12

L. Woo