简体   繁体   English

为什么BluetoothAdapter.startDiscovery(..)需要广播蓝牙设备?

[英]Why BluetoothAdapter.startDiscovery(..) require to get Bluetooth device broadcast?

Normally how Android Broadcast work is: app have to create BroadcastReceiver and have to register action intent for it want to get receive event. 通常,Android Broadcast的工作方式是:应用必须创建BroadcastReceiver,并且必须为其要获取接收事件的操作注册意图。

But in case of Bluetooth device discovery/scanning why it required request call through BluetoothAdapter.startDsiccovery() . 但是在发现/扫描蓝牙设备的情况下,为什么它需要通过BluetoothAdapter.startDsiccovery()请求调用。

Basically I want to dicover BLE device through long live Service running in Background. 基本上,我想通过在后台运行的长期服务来发现BLE设备。

Any one have idea here? 有人在这里有主意吗?

private void listenPairedDevice() {
Button listenBtn = (Button)findViewById(R.id.button_listen);
listenBtn.setOnClickListener(new OnClickListener() {
  public void onClick(View view) {
    Intent disc = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
    startActivityForResult(disc, DISCOVERY_REQUEST);     
 }
 });
}
@Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
 if (requestCode == DISCOVERY_REQUEST) {
boolean isDiscoverable = resultCode > 0;
 if (isDiscoverable) {
 String name = "bluetoothserver";
 try {
final BluetoothServerSocket btserver = bluetooth.listenUsingRfcommWithServiceRecord(name, uuid);


      AsyncTask<Integer, Void, BluetoothSocket> acceptThread = new AsyncTask<Integer, Void, BluetoothSocket>() {

        @Override
        protected BluetoothSocket doInBackground(Integer... params) {
          try {

             socket = btserver.accept();
            return socket;
             } catch (IOException e) {
            Log.d("BLUETOOTH", e.getMessage());            
          }
          finally {
            //close statement added later by MR
              try{
              btserver.close();
              } catch (IOException e){

              }
          }
          return null;
        }

        @Override
        protected void onPostExecute(BluetoothSocket result) {
          if (result != null)
            changeLayout();
        }            
      };          
      acceptThread.execute(resultCode);
    } catch (IOException e) {
      Log.d("BLUETOOTH", e.getMessage());            
    }
  }
}

The startDiscovery() does a 2 step process, startDiscovery()执行两步过程,

  • Finding the device by inquiring for it. 通过查询查找设备。

  • Followed by a name discovery ie paging and connecting to the device. 随后进行名称发现,即分页并连接到设备。

If you have gone through the docs it says public boolean startDiscovery () 如果您浏览过文档,则显示public boolean startDiscovery()

Start the remote device discovery process. 启动远程设备发现过程。

The discovery process usually involves an inquiry scan of about 12 seconds, followed by a page scan of each new device to retrieve its Bluetooth name. 发现过程通常涉及大约12秒的查询扫描,然后是对每个新设备的页面扫描,以检索其蓝牙名称。

This is an asynchronous call, it will return immediately. 这是一个异步调用,它将立即返回。 Register for ACTION_DISCOVERY_STARTED and ACTION_DISCOVERY_FINISHED intents to determine exactly when the discovery starts and completes. 注册ACTION_DISCOVERY_STARTED和ACTION_DISCOVERY_FINISHED意向,以准确确定发现何时开始和完成。 Register for ACTION_FOUND to be notified as remote Bluetooth devices are found. 注册ACTION_FOUND以在找到远程蓝牙设备时得到通知。

Device discovery is a heavyweight procedure. 设备发现是一项繁重的过程。 New connections to remote Bluetooth devices should not be attempted while discovery is in progress, and existing connections will experience limited bandwidth and high latency. 发现过程中,请勿尝试与远程蓝牙设备建立新连接,并且现有连接将受到带宽和高延迟的限制。 Use cancelDiscovery() to cancel an ongoing discovery. 使用cancelDiscovery()取消正在进行的发现。 Discovery is not managed by the Activity, but is run as a system service, so an application should always call cancelDiscovery() even if it did not directly request a discovery, just to be sure. 发现不是由活动管理的,而是作为系统服务运行的,因此,请确保即使应用程序未直接请求发现,也应始终调用cancelDiscovery()。

Device discovery will only find remote devices that are currently discoverable (inquiry scan enabled). 设备发现将仅找到当前可发现的远程设备(启用查询扫描)。 Many Bluetooth devices are not discoverable by default, and need to be entered into a special mode. 默认情况下,许多蓝牙设备是无法发现的,需要进入特殊模式。

If Bluetooth state is not STATE_ON, this API will return false. 如果蓝牙状态不是STATE_ON,则此API将返回false。 After turning on Bluetooth, wait for ACTION_STATE_CHANGED with STATE_ON to get the updated value. 开启蓝牙后,请等待带有STATE_ON的ACTION_STATE_CHANGED以获取更新的值。

Requires BLUETOOTH_ADMIN. 需要BLUETOOTH_ADMIN。

Returns true on success, false on error 成功返回 true,错误返回 false

EDIT 编辑

Plese follow Bluetooth device discovery in Android — startDiscovery() Plese跟随Android中的蓝牙设备发现— startDiscovery()

I think you have to start on Bluetooth discovery using UUId and socket. 我认为您必须开始使用UUId和套接字进行蓝牙发现。

After getting any device you have to set up a connection to this and start a thread that will check regularly while it is connected or not. 获取任何设备后,您必须建立与此设备的连接并启动一个线程,该线程会在连接与否时定期进行检查。

Scanning for peripherals is not just a heavy-weight task, it is also not friendly to the battery so it can't be left on all the time. 扫描外围设备不仅是一项繁重的任务,而且对电池也不友好,因此不能一直使用。

That said, you can still have something in the background doing the work for you, like something that wakes up every 15 seconds and scans for 5 seconds. 也就是说,您仍然可以在后台为您完成工作,例如每15秒唤醒一次并扫描5秒的内容。 On some devices when you try to connect to a device that is no longer available you will get a disconnected callback to onConnectionStateChange in the BluetoothGattCallback , and on other devices the same connection attempt will wait until the device is back and then connect. 在某些设备上,当您尝试连接到不再可用的设备时,您将在BluetoothGattCallback收到断开连接的回调到onConnectionStateChange ,而在其他设备上,相同的连接尝试将等待,直到该设备返回然后再连接。

So basically, wait, scan, connect if you find your device and if not then wait again. 因此,基本上,如果找到设备,请等待,扫描并连接,如果找不到,请再次等待。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何让 BluetoothAdapter.startDiscovery() 在我的 Android 10 设备上工作? - How do I get the BluetoothAdapter.startDiscovery() working on my Android 10 device? BluetoothAdapter.startDiscovery() 不会发现屏幕关闭的那些蓝牙设备(在我的情况下是智能手机) - BluetoothAdapter.startDiscovery() doesn't discovers those bluetooth devices(Smart Phones in my case) whose screen is off bluetoothAdapter.startDiscovery() 不从通知面板单击蓝牙按钮就不会启动并立即完成 - bluetoothAdapter.startDiscovery() doesn't start without clicking the bluetooth button from the notification panel and finishes immediately Android 11:BluetoothAdapter.startDiscovery() 返回 false; 如何知道错误是什么? - Android 11: BluetoothAdapter.startDiscovery() returns false; How to know what the error is? Android中的蓝牙设备发现 - startDiscovery() - Bluetooth device discovery in Android — startDiscovery() BluetoothAdapter.getDefaultAdapter().startDiscovery() 不工作 - BluetoothAdapter.getDefaultAdapter().startDiscovery() not working 由于棉花糖使用BluetoothAdapter.getDefaultAdapter()。startDiscovery();更新了蓝牙发现。 被打破 - Since marshmallow update Bluetooth discovery using BluetoothAdapter.getDefaultAdapter().startDiscovery(); is broken 取消绑定蓝牙设备时的BluetoothAdapter通知 - BluetoothAdapter notification when a bluetooth device is unboded Android蓝牙启动发现不起作用 - Android Bluetooth startDiscovery not working BluetoothAdapter StartDiscovery 在前台服务中不起作用 - BluetoothAdapter StartDiscovery does not work in Foreground Service
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM