我现在有点新,现在我正在尝试设置一个基本的BLE扫描仪应用程序,但该应用程序没有发现我的BLE模块(即时通讯使用Android开发页面上的基本代码)。
我试着在网上查找解决方案,发现问题是位置要求。然而,即使我申请此请求后,我仍然无法找到我的BLE模块
this is the location request
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST_COARSE_LOCATION: {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
scanLeDevice(true);
} else {
// Alert the user that this application requires the location permission to perform the scan.
}
}
}
}
这是我的扫描码
private void scanLeDevice(final boolean enable) {
if (enable) {
// Stops scanning after a pre-defined scan period.
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
mScanning = false;
mBluetoothAdapter.stopLeScan(mLeScanCallback);
invalidateOptionsMenu();
}
}, SCAN_PERIOD);
mScanning = true;
mBluetoothAdapter.startLeScan(mLeScanCallback);
} else {
mScanning = false;
mBluetoothAdapter.stopLeScan(mLeScanCallback);
}
invalidateOptionsMenu();
}
private BluetoothAdapter.LeScanCallback mLeScanCallback =
new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
runOnUiThread(new Runnable() {
@Override
public void run() {
mLeDeviceListAdapter.addDevice(device);
mLeDeviceListAdapter.notifyDataSetChanged();
}
});
}
};
您是否还在AndroidManifest.xml中添加了权限?它看起来像
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
同时,您需要在Application中的某个位置设置您的单元格。
找到您自己的Android应用程序,然后授予它位置权限。
希望它有所帮助。