Ich versuche, eine einfache Android-Anwendung zu erstellen, mit der ich Bluetooth Low Energy (BLE) Geräte scannen und Namen als Log drucken kann. Die Hauptaktivität ist sehr einfach, ich brauche keine Kontrollen, das ist wohl in Ordnung. Der Scan startet, aber ich habe keine Ergebnisse.Einfache Android BLE Scanner
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);
}
}
}
Danke !! Ich habe das Hinzufügen der Berechtigungen ACCESS_COARSE_LOCATION zum Manifest gelöst und diese Berechtigung im Code angefordert. –