1

我試圖掃描BLE裝置與掃描BLE裝置不能正常工作

mBluetoothAdapter.startLeScan(this); 

(我知道它的新版本過時,但只是爲了看看它與我的電話[4.4],我使用它)。所以它開始掃描,然後繼續前進而不會發生錯誤,但是沒有檢測到設備。 OnLEScan事件也被觸發,但其中的設備參數爲空。我的LE設備就在那裏並且已連接。

在谷歌搜索,我發現這發生如果BluetoothAdapter沒有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(); 
     } 
} 

回答

0

Anroid的4.4是不兼容的,我用的是Android 5.1設備,它的工作就像一個魅力!

0
  • 使用此代碼,因爲它會爲您提供所有的數據 提供您的BLE裝置的洞察力(將在日誌中顯示的數據)。
  • UUID基本上由設備的製造商提供,您無法自行設置。
  • 對於實例:我使用BLE Beacons及其製造商提供給我的API,它幫助我獲取其UUID。
  • 有時候,設備上有一個特殊的ID(我的例子中是工廠ID),它可以幫助您從製造商的網站或API中檢索您的UUID。

    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); 
         } 
        }); 
    
    } 
    
+0

非常感謝!似乎只是正確的答案,我得到的錯誤:方法查找失敗的選擇器「getBluetoothLeScanner」簽名「()Landroid /藍牙/樂/ BluetoothLeScanner;」 \t當我這樣做 - > mBluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner(); – Samra

+0

我的java編譯器版本是1.7(如果重要) – Samra

+0

您可能會收到此錯誤,因爲** BluetoothLeScanner **已添加到API 21(Android 5.0)中。所以,嘗試設置'minSdkVersion 21'。 –