2017-03-07 96 views
0

我想創建一個簡單的Android應用程序,可以掃描藍牙低功耗(BLE)設備和打印名稱作爲日誌。主要的活動是非常基本的,我不需要檢查,我想這是可以的。掃描開始,但我沒有結果。簡單的Android BLE掃描儀

的Manifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.s1080994.tid.findble"> 


    <uses-permission android:name="android.permission.BLUETOOTH" /> 
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> 


    <uses-feature 
     android:name="android.hardware.bluetooth_le" 
     android:required="true" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <activity android:name=".MainActivity"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

MainActivity.java

public class MainActivity extends AppCompatActivity { 

    private BluetoothAdapter mBluetoothAdapter = null; 
    private BluetoothLeScanner mBluetoothLeScanner = null; 

    public static final int REQUEST_BT_PERMISSIONS = 0; 
    public static final int REQUEST_BT_ENABLE = 1; 

    private boolean mScanning = false; 
    private Handler mHandler = null; 

    private Button btnScan = null; 

    private ScanCallback mLeScanCallback = 
      new ScanCallback() { 

       @Override 
       public void onScanResult(int callbackType, final ScanResult result) { 
        //super.onScanResult(callbackType, result); 
        Log.i("BLE", result.getDevice().getName()); 

       } 

       @Override 
       public void onScanFailed(int errorCode) { 
        super.onScanFailed(errorCode); 
        Log.i("BLE", "error"); 
       } 
      }; 



    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     btnScan = (Button) findViewById(R.id.btnScan); 

     this.mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
     this.mBluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner(); 
     this.mHandler = new Handler(); 

     checkBtPermissions(); 
     enableBt(); 
    } 

    public void onBtnScan(View v){ 
     if (mScanning){ 
      mScanning = false; 
      scanLeDevice(false); 
      btnScan.setText("STOP"); 
     } else { 
      mScanning = true; 
      scanLeDevice(true); 
      btnScan.setText("SCAN"); 
     } 
    } 

    public void checkBtPermissions() { 
     this.requestPermissions(
       new String[]{ 
         Manifest.permission.BLUETOOTH, Manifest.permission.BLUETOOTH_ADMIN 
       }, 
       REQUEST_BT_PERMISSIONS); 
    } 

    public void enableBt(){ 
     if (mBluetoothAdapter == null) { 
      // Device does not support Bluetooth 
     } 
     if (!mBluetoothAdapter.isEnabled()) { 
      Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
      startActivityForResult(enableBtIntent, REQUEST_BT_ENABLE); 
     } 
    } 

    public void scanLeDevice(final boolean enable) { 
     //ScanSettings mScanSettings = new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_BALANCED).setCallbackType(ScanSettings.CALLBACK_TYPE_ALL_MATCHES).build(); 

     if (enable) { 
      mScanning = true; 
      Log.i("Scanning", "start"); 
      mBluetoothLeScanner.startScan(mLeScanCallback); 
     } else { 
      Log.i("Scanning", "stop"); 
      mScanning = false; 
      mBluetoothLeScanner.stopScan(mLeScanCallback); 
     } 
    } 
} 

回答

1

你需要爲了掃描附近的android上的BLE裝置啓用位置/ GPS設置。請啓用位置/ gps並嘗試掃描。

+0

謝謝!我解決了添加以顯示ACCESS_COARSE_LOCATION權限並在代碼中請求此權限的問題。 –