android 蓝牙discover,android: Bluetooth won't discover devices

I'm writing an Android-App, which is supposed to discover devices via Bluetooth.

I don't get any exceptions, but the devices are just not found, even though my windows pc finds them (and can be found itself).

I'm certain they are BLE, but I tried both ways. And of course I tried them separately.

Here is my ListActivity, which searches for the devices:

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

mHandler = new Handler();

setContentView(R.layout.bluetooth_list_view);

listView = getListView();

if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {

Toast.makeText(this, "BLE is not supported", Toast.LENGTH_SHORT).show();

finish();

}

final BluetoothManager bluetoothManager =

(BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);

mBluetoothAdapter = bluetoothManager.getAdapter();

mBluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner();

Log.d(TAG, "on Create start5");

if (mBluetoothAdapter == null) {

Toast.makeText(this, "BLE is not supported", Toast.LENGTH_SHORT).show();

finish();

return;

}

mLeDeviceListAdapter = new LeDeviceListAdapter(this);

setListAdapter(mLeDeviceListAdapter);

listView.setAdapter(mLeDeviceListAdapter);

IntentFilter filter = new IntentFilter();

filter.addAction(BluetoothDevice.ACTION_FOUND);

filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);

filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);

registerReceiver(mReceiver, filter);

mBluetoothAdapter.startDiscovery();

}

private void scanLeDevice(final boolean enable) {

Log.e(TAG, "scanLeDevice: " + enable);

if (enable) {

// Stops scanning after a pre-defined scan period.

mHandler.postDelayed(new Runnable() {

@Override

public void run() {

Log.e(TAG, "after Scan mLeDeviceListAdapter: " + mLeDeviceListAdapter.getCount());

Log.e(TAG, "after Scan isEmpty(): " + mLeDeviceListAdapter.isEmpty());

mScanning = false;

mBluetoothLeScanner.stopScan(mScanCallback);

invalidateOptionsMenu();

}

}, SCAN_PERIOD);

mScanning = true;

mBluetoothLeScanner.startScan(mScanCallback);

} else {

mScanning = false;

mBluetoothLeScanner.stopScan(mScanCallback);

}

invalidateOptionsMenu();

}

The logs say after Scan mLeDeviceListAdapter: 0 and after Scan isEmpty(): true.

ScanCallback mScanCallback = new ScanCallback() {

@Override

public void onScanResult(int callbackType, ScanResult result) {

Log.e(TAG, "onScanResult: " + result);

super.onScanResult(callbackType, result);

mLeDeviceListAdapter.addDevice(result.getDevice());

mLeDeviceListAdapter.notifyDataSetChanged();

}

};

That log (onScanResult) is never called.

And for not BLE:

private final BroadcastReceiver mReceiver = new BroadcastReceiver() { //TODO not ble example

public void onReceive(Context context, Intent intent) {

String action = intent.getAction();

if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {

Log.e(TAG, "Not BLE discovry starts");

//discovery starts, we can show progress dialog or perform other tasks

} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {

Log.e(TAG, "Not BLE discovry finishes");

//discovery finishes, dismis progress dialog

} else if (BluetoothDevice.ACTION_FOUND.equals(action)) {

//bluetooth device found

BluetoothDevice device = (BluetoothDevice) intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

Log.e(TAG, "Found device " + device.getName());

}

}

};

Not BLE discovry finishes is printed and so is Not BLE discovry starts. Found device is never printed. But I'm positiv that the devices are BLE.

I have the necessary permissions in my manifest:

What am I doing wrong?

Edit:

i really need help. Thinks I tried/checked so far:

Enabled Bluetooth adapter.

Bluetooth usage permissions granted.

Location permission (ACCESS_COARSE and ACCESS_FINE) granted.

Made sure gps provider is enabled (Smartphone GPS settings).

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值