小编Lem*_*nen的帖子

Android中的蓝牙设备发现 - startDiscovery()

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

我做了什么:

  1. 仔细阅读文档.
  2. 实现了主机设备的BT适配器的本地实例.
  3. 如果未启用BT,则实施启用BT的通知.
  4. 注册的广播接收者和意图来解析startDiscovery()的ACTION_FOUNDs即将到来.
  5. 清单中注册的BLUETOOTH和BLUETOOTH_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 …
Run Code Online (Sandbox Code Playgroud)

android bluetooth android-intent android-context android-adapter

15
推荐指数
1
解决办法
5万
查看次数