Android中发现蓝牙设备的广播是,Android中的蓝牙设备发现 – startDiscovery()

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

我做了什么:仔细阅读文档。

实现了主机设备的BT适配器的本地实例。

如果未启用BT,则实施启用BT的通知。

注册的广播接收者和意图来解析startDiscovery()的ACTION_FOUND 。

清单中注册的BLUETOOTH和BLUETOOTH_ADMIN权限。

在startDiscovery()之前一切正常(通过增量控制台日志记录测试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); } } }

}

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

简单地说,当你希望你的应用程序发现本地蓝牙设备时,你应该使用startDiscovery() …例如,如果你想实现一个ListActivity来扫描并动态地将附近的蓝牙设备添加到ListView (参见DeviceListActivity )。

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

定义表示本地蓝牙适配器的类variables。

BluetoothAdapter mBtAdapter = BluetoothAdapter.getDefaultAdapter();

检查您的设备是否已“发现”。 如果是,则取消发现。

if (mBtAdapter.isDiscovering()) { mBtAdapter.cancelDiscovery(); }

在检查(并可能取消)发现模式后,立即通过调用开始发现,

mBtAdapter.startDiscovery();

一般要非常小心,不小心将设备置于发现模式。 执行设备发现对于蓝牙适配器来说是一个繁重的过程,并且会消耗大量资源。 例如,您要确保在尝试建立连接之前检查/取消发现。 您很可能也希望在onDestroy方法中取消发现。

如果这有帮助,请告诉我…如果您仍然遇到问题,请使用您的logcat输出和/或您收到的任何错误消息更新您的答案,也许我可以帮助您更多。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值