【问题标题】:Scanning BLE device not working扫描 BLE 设备不工作
【发布时间】:2016-07-05 07:19:56
【问题描述】:

我正在尝试使用

扫描 BLE 设备
mBluetoothAdapter.startLeScan(this);

(我知道它对于较新的版本已经过时,但只是为了看到它适用于我的手机 [4.4],我正在使用它)。所以它开始扫描,然后继续前进,没有给出错误,但没有检测到设备。 OnLEScan 事件也被触发,但其中的设备参数为空。我的 LE 设备就在那里并已连接。

在谷歌搜索时,我发现如果蓝牙适配器没有 UUID,就会发生这种情况。 如何设置 UUID? OnLeScan 何时被调用/解雇?有没有其他解决办法?

我的回调代码放在这里

//BluetoothAdapte.LEScanCallBack on MainActivity
@Override
public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord){
    Log.i(TAG, "New LE Device: " + device.getName() + "@" + rssi);
    if(Device_Name.equals(device.getName())){
            mDevices.put(device.hashCode(), device);
            invalidateOptionsMenu();
        }
}

【问题讨论】:

    标签: android bluetooth-lowenergy android-bluetooth scanning gatt


    【解决方案1】:
    • 使用此代码,因为它可以让您深入了解所有数据 在您的 BLE 设备中可用(将在日志中显示数据)。
    • UUID 基本上是设备厂商提供的,不能自行设置。
    • 例如:我使用的是 BLE 信标,它的制造商为我提供了帮助我获取其 UUID 的 API。
    • 有时,设备本身有一个特殊的 ID(在我的例子中是工厂 ID),它可以帮助您检索您的 UUID,也可以从制造商的网站或 API 中检索。

      BluetoothAdapter bluetoothAdapter;
      BluetoothLeScanner bluetoothLeScanner;
      
      
      
      bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
      bluetoothLeScanner = bluetoothAdapter.getBluetoothLeScanner();
      
      
      
          bluetoothLeScanner.startScan(new ScanCallback() {
              @Override
              public void onScanResult(int callbackType, ScanResult result) {
                  super.onScanResult(callbackType, result);
      
                  String s = "\nRssi : "+result.getRssi()+"" +
                          "\nName (Get Device) : "+result.getDevice().getName()+"" +
                          "\nBytes"+result.getScanRecord().getBytes()+"" +
                          "\nGet Device : " + result.getDevice()+"" +
                          "\nAddress : "+result.getDevice().getAddress()+"" +
                          "\nService UUIds : "+result.getScanRecord().getServiceUuids().get(0)+"" +       //Unique
                          "\nName (Scan Record) : "+result.getScanRecord().getDeviceName()+"" +
                          "\nUuids device : "+result.getDevice().getUuids()+"" +
                          "\nDescribe contents : "+result.describeContents();
      
                  //This will show you all the data in logs.
                  Log.e("All Data",s);
      
      
      
              }
      
              @Override
              public void onBatchScanResults(List<ScanResult> results) {
                  super.onBatchScanResults(results);
              }
      
              @Override
              public void onScanFailed(int errorCode) {
                  super.onScanFailed(errorCode);
              }
          });
      
      }
      

    【讨论】:

    • 非常感谢!似乎只是我收到错误的正确答案:方法查找失败选择器“getBluetoothLeScanner”,签名为“()Landroid/bluetooth/le/BluetoothLeScanner;”当我这样做时-> mBluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner();
    • 我的 java 编译器版本是 1.7(如果重要的话)
    • 您可能会收到此错误,因为 API 21 (Android 5.0) 中添加了 BluetoothLeScanner。所以,尝试设置minSdkVersion 21
    • 现在没有错误但同样的问题,现在也没有检测到设备。在我的 Android Manifest 文件中,我拥有蓝牙、bluetooth_admin、Coarse_location、Fine_Location 的权限。我的 GPS 和蓝牙都在我的设备上
    【解决方案2】:

    Anroid 4.4 不兼容,我使用的是 android 5.1 设备,它的工作原理非常棒!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-18
      • 2021-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-28
      相关资源
      最近更新 更多