Bluetooth device discovery in Android — startDisco

2019-02-01 06:23发布

问题:

Goal: Build an Android app that discovers the names and addresses of BT devices within range and submits their values to a webservice. BT devices have not been previously bonded to the host device, I just want to poll everything as I walk about.

What I've done:

  1. Pored over documentation.
  2. Implemented a local instance of the host device's BT adapter.
  3. Implemented a notification to enable BT if it isn't enabled.
  4. Registered Broadcast Receivers and Intents to parse the ACTION_FOUNDs coming off of startDiscovery().
  5. Registered BLUETOOTH and BLUETOOTH_ADMIN permissions in the manifest.

Things work (as tested with incremental console logging) up until startDiscovery().


Frustration:

  • startDiscovery() -- I suspect I am passing this in the wrong context. What context does this method need to be placed within to function properly?

If you have been able to get this method working, I would very much appreciate your wisdom.

UPDATE - here's a stripped down simplified version of the code that is causing me grief; this simplification recapitulates my error. This code runs, it throws no cat.log errors or other errors, it simply doesn't give any output.

package aqu.bttest;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.Toast;

public class BT2Activity extends Activity {

private BluetoothAdapter mBTA;
private SingBroadcastReceiver mReceiver;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

  //register local BT adapter
    mBTA = BluetoothAdapter.getDefaultAdapter();
    //check to see if there is BT on the Android device at all
    if (mBTA == null){
        int duration = Toast.LENGTH_SHORT;
        Toast.makeText(this, "No Bluetooth on this handset", duration).show();
    }
    //let's make the user enable BT if it isn't already
    if (!mBTA.isEnabled()){
        Intent enableBT = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBT, 0xDEADBEEF);
    }
    //cancel any prior BT device discovery
    if (mBTA.isDiscovering()){
        mBTA.cancelDiscovery();
    }
    //re-start discovery
    mBTA.startDiscovery();

    //let's make a broadcast receiver to register our things
    mReceiver = new SingBroadcastReceiver();
    IntentFilter ifilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
    this.registerReceiver(mReceiver, ifilter);
}

private class SingBroadcastReceiver extends BroadcastReceiver {

    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction(); //may need to chain this to a recognizing function
        if (BluetoothDevice.ACTION_FOUND.equals(action)){
            // Get the BluetoothDevice object from the Intent
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            // Add the name and address to an array adapter to show in a Toast
            String derp = device.getName() + " - " + device.getAddress();
            Toast.makeText(context, derp, Toast.LENGTH_LONG);
        }
    }
}

}

回答1:

What context does this method need to be placed within to function properly.

To put it simply, you should use startDiscovery() when you want your application to discover local Bluetooth devices... for instance, if you wanted to implement a ListActivity that scans and dynamically adds nearby Bluetooth devices to a ListView (see DeviceListActivity).

Your usage of the startDiscovery() method should look something like this:

  1. Define a class variable representing the local Bluetooth adapter.

    BluetoothAdapter mBtAdapter = BluetoothAdapter.getDefaultAdapter();
    
  2. Check to see if your device is already "discovering". If it is, then cancel discovery.

    if (mBtAdapter.isDiscovering()) {
        mBtAdapter.cancelDiscovery();
    }
    
  3. Immediately after checking (and possibly canceling) discovery-mode, start discovery by calling,

    mBtAdapter.startDiscovery();
    
  4. Be very careful in general about accidentally leaving your device in discovery-mode. Performing device discovery is a heavy procedure for the Bluetooth adapter and will consume a lot of its resources. For instance, you want to make sure you check/cancel discovery prior to attempting to make a connection. You most likely want to cancel discovery in your onDestroy method too.

Let me know if this helped... and if you are still having trouble, update your answer with your logcat output and/or any error messages you are getting, and maybe I can help you out a bit more.