12

目标:构建一个Android应用程序,用于发现范围内BT设备的名称和地址并将其值提交给web服务。 BT设备以前没有被绑定到主机设备,我只是想在我走过的时候轮询一切。Android中的蓝牙设备发现 - startDiscovery()

我所做的:

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

事情工作(如增量控制台日志记录测试)直到startDiscovery()


无奈:

  • startDiscovery() - 我怀疑我传递这在错误的上下文。这种方法需要放在什么上下文才能正常工作?

如果你已经能够得到这个方法的工作,我会非常感谢你的智慧。

UPDATE - 这是简化版本的代码,这让我很悲伤;这种简化重演了我的错误。此代码运行,它不会抛出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); 
     } 
    } 
} 

}

+0

这将有助于了解您所得到的错误......或者至少是什么让您相信'startDiscovery()'运行不正常。 –

回答

15

确实需要何种情况下这种方法被妥善安置到函数中。

简单地说,你应该使用startDiscovery()当你想你的应用程序发现本地的蓝牙设备......举例来说,如果你想实现一个ListActivity,扫描和动态地将附近的蓝牙设备的ListView(见DeviceListActivity)。

你的startDiscovery()方法的使用应该是这个样子:

  1. 定义表示本地蓝牙适配器类变量。

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

    if (mBtAdapter.isDiscovering()) { 
        mBtAdapter.cancelDiscovery(); 
    } 
    
  3. 检查(也可能取消)发现模式后,立即致电开始发现,

    mBtAdapter.startDiscovery(); 
    
  4. 是一般非常小心,不小心让你的设备处于发现模式。执行设备发现对于蓝牙适配器来说是一个沉重的过程,并且会消耗大量资源。例如,您想确保在尝试建立连接之前检查/取消发现。您最有可能还想在您的onDestroy方法中取消发现。

让我知道,如果这有助于...如果你仍然有问题,你的logcat的输出和/或您收到任何错误消息更新你的答案,也许我可以帮你出位更多。

+0

这有帮助。此外,将结果传递给ArrayAdapter而不是Toast也有帮助。 – Lemminkainen

+0

有没有什么办法可以在BG服务中保持连续扫描,而不需要放置Timer ...任何系统级别的API。 – CoDe

+0

@Shubh我不这么认为。这种限制的原因在于确保应用程序不会通过在后台连续扫描来滥用“蓝牙”。 –