2014-07-04 87 views
0

我正在開發一個演示應用程序來掃描Blutooth Le設備。但startLeScan()返回null。而我沒有得到任何設備名稱。我已經嘗試使用正常掃描它顯示罰款。我在這裏添加我的代碼片段Blutooth LE掃描

private void scanLeDevice(final boolean enable) { 
     if (enable) { 
      // Stops scanning after a pre-defined scan period. 
      new Handler().postDelayed(new Runnable() { 
       @Override 
       public void run() { 
        mScanning = false; 
        mTxtInfo.setText("Stopped Scanning."); 
        mBluetoothAdapter.stopLeScan(mLeScanCallback); 
       } 
      }, SCAN_PERIOD); 

      mScanning = true; 
      mTxtInfo.setText("Started Scanning..."); 
      mBluetoothAdapter.startLeScan(mLeScanCallback); 
     } else { 
      mTxtInfo.setText("Stopped Scanning."); 
      mScanning = false; 
      mBluetoothAdapter.stopLeScan(mLeScanCallback); 
     } 

    } 

這是我的啓動樂掃描的功能。

// Device scan callback. 
    private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() { 
     @Override 
     public void onLeScan(final BluetoothDevice device, int rssi, 
       byte[] scanRecord) { 
      runOnUiThread(new Runnable() { 
       @Override 
       public void run() { 

        mTxtInfo.setText("Detected devices: " + device.getName()); 
        Toast.makeText(MainActivity.this, 
          "Detected devices: " + device.getName(), 
          Toast.LENGTH_LONG).show(); 
       } 
      }); 
     } 


    }; 

這是回調。它顯示我

07-04 12:50:17.833: D/BluetoothAdapter(3564): startLeScan(): null 

希望有任何幫助。

回答

2

的問題還有就是你要使用

startLeScan(callback) 

而不設置UUID參數。所以BluetoothAdapter代碼它做的是這樣的:上回

startLeScan(null, callback) 

和打印

"startLeScan:" + Uuid. 

這對你是空的。

+0

這是正確的答案 –