[英]android: Bluetooth won't discover devices
I'm writing an Android-App, which is supposed to discover devices via Bluetooth. 我正在编写一个Android应用程序,应该可以通过蓝牙发现设备。
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). 我没有任何例外,但是即使我的Windows PC找到了这些设备(也可以自己找到),却找不到这些设备。
I'm certain they are BLE, but I tried both ways. 我确定它们是BLE,但是我尝试了两种方法。 And of course I tried them separately.
当然,我分开尝试了。
Here is my ListActivity , which searches for the devices: 这是我的ListActivity ,它搜索设备:
@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 . 日志在Scan mLeDeviceListAdapter:0 之后和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. 永远不会调用该日志( onScanResult )。
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(); 对于非BLE:私有最终的BroadcastReceiver mReceiver = new BroadcastReceiver(){//待办事项示例public void onReceive(Context context,Intent intent){字符串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 . Not BLE Discovry表面处理已打印,因此Not BLE Discovry开始 。 Found device is never printed.
找到的设备从不打印。 But I'm positiv that the devices are BLE.
但是我认为这些设备是BLE。
I have the necessary permissions in my manifest: 我在清单中拥有必要的权限:
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
What am I doing wrong? 我究竟做错了什么?
Edit: i really need help. 编辑:我真的需要帮助。 Thinks I tried/checked so far:
认为我到目前为止尝试过/检查过:
Maybe because you didn't request ACCESS_COARSE_LOCATION in onCreate
可能是因为您没有在
onCreate
请求ACCESS_COARSE_LOCATION
This project is a good resource for you to start your Bluetooth scanner: 该项目是启动蓝牙扫描仪的良好资源:
https://github.com/joelwass/Android-BLE-Scan-Example https://github.com/joelwass/Android-BLE-Scan-Example
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.