19

目标:构建一个 Android 应用程序,发现范围内的 BT 设备的名称和地址,并将其值提交给 Web 服务。BT 设备之前没有绑定到主机设备,我只想在走动时轮询所有内容。

我做了什么:

  1. 仔细阅读文档。
  2. 实现了主机设备的 BT 适配器的本地实例。
  3. 如果未启用,则实施通知以启用 BT。
  4. 注册广播接收器和意图来解析startDiscovery()ACTION_FOUNDs的到来。
  5. 在清单中注册了BLUETOOTHBLUETOOTH_ADMIN权限。

直到startDiscovery().


挫折:

  • startDiscovery() - 我怀疑我在错误的上下文中传递了这个。此方法需要放置在什么上下文中才能正常运行?

如果您能够使这种方法发挥作用,我将非常感谢您的智慧。

更新- 这是导致我悲伤的代码的精简版;这种简化概括了我的错误。这段代码运行,它没有抛出cat.log错误或其他错误,它根本没有给出任何输出。

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);
        }
    }
}

}

4

1 回答 1

25

此方法需要放置在什么上下文中才能正常运行。

简而言之,startDiscovery()当您希望应用程序发现本地蓝牙设备时,您应该使用...例如,如果您想要实现ListActivity扫描附近的蓝牙设备并将其动态添加到 a ListView(请参阅 参考资料DeviceListActivity)。

您对该startDiscovery()方法的使用应如下所示:

  1. 定义一个代表本地蓝牙适配器的类变量。

    BluetoothAdapter mBtAdapter = BluetoothAdapter.getDefaultAdapter();
    
  2. 检查您的设备是否已经在“发现”。如果是,则取消发现。

    if (mBtAdapter.isDiscovering()) {
        mBtAdapter.cancelDiscovery();
    }
    
  3. 检查(并可能取消)发现模式后,立即通过调用开始发现,

    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.

于 2012-05-12T01:49:40.800 回答