0
我正試圖在我的應用程序中實現藍牙開發人員入門套件。 但我有以下錯誤。BLE BluetoothAdapter.getBluetoothLeScanner()error
java.lang.NullPointerException: Attempt to invoke virtual method 'android.bluetooth.le.BluetoothLeScanner android.bluetooth.BluetoothAdapter.getBluetoothLeScanner()' on a null object reference
Rrunning此如下功能:
public void startScanning(final ScanResultsConsumer scan_results_consumer, long stop_after_ms) {
if (scanning) {
Log.d(Constants.TAG, "Already scanning so ignoring startScanning request");
return;
}
Log.d(Constants.TAG, "Scanning...");
if (scanner == null) {
scanner = bluetooth_adapter.getBluetoothLeScanner();
Log.d(Constants.TAG, "Created BluetoothScanner object");
}
handler.postDelayed(new Runnable() {
@Override
public void run() {
if (scanning) {
Log.d(Constants.TAG, "Stopping scanning");
scanner.stopScan(scan_callback);
setScanning(false);
}
}
}, stop_after_ms);
this.scan_results_consumer = scan_results_consumer;
List<ScanFilter> filters;
filters = new ArrayList<ScanFilter>();
/*ScanFilter filter = new ScanFilter.Builder().setDeviceName("SP5").build();
filters.add(filter);*/
ScanSettings settings = new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).build();
setScanning(true);
scanner.startScan(filters, settings, scan_callback);
}
在呼叫:
scanner = bluetooth_adapter.getBluetoothLeScanner();
你有任何想法,爲什麼我的藍牙適配器是空?
bluetooth_adapter不爲空? –