我正在尝试一些代码以将BLE设备连接到Android手机。 当我打电话时出现问题: scanLeDevice(true)
从不调用mLeScanCallback的onLeScan方法,但从Debug Logcat中可以看到BluethoothLeScanner从发现的设备中接收一些结果。
private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {
Log.i(TAG, device.getName());
}
};
private void scanLeDevice(final boolean enable) {
if (enable) {
// Stops scanning after a pre-defined scan period.
Log.d(TAG,"scanLeDevice");
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
mScanning = false;
mBluetoothAdapter.stopLeScan(mLeScanCallback);
}
}, SCAN_PERIOD);
mScanning = true;
mBluetoothAdapter.startLeScan(mLeScanCallback);
} else {
mScanning = false;
ready = -1;
mBluetoothAdapter.stopLeScan(mLeScanCallback);
}
}
我知道不推荐使用BluethhothAdapter.startLeScan(),甚至尝试使用startScan()获得相同的结果。
我具有在AndroidManifest.xml上设置的权限
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="it.adacto.mysensortag">
<uses-feature
android:name="android.hardware.bluetooth_le"
android:required="true" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-sdk android:minSdkVersion="18"
android:targetSdkVersion="23"
android:maxSdkVersion="24" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".AndroidPlugin"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="@string/app_name"
android:theme="@style/FullscreenTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Debug Logcat显示了Bluethooth Scanner的一些ScanResult,但是从未调用过Callback。我丢失了一些东西,但我不知道...
first manudata for manu ID
08-20 13:02:16.451 6243-6253/it.adacto.mysensortag
D/BluetoothLeScanner: onScanResult() -
ScanResult{mDevice=24:71:89:BC:81:80, mScanRecord=ScanRecord
[mAdvertiseFlags=5, mServiceUuids=[0000aa80-0000-1000-8000-
00805f9b34fb], mManufacturerSpecificData={13=[3, 0, 1]}, mServiceData=
{}, mTxPowerLevel=0, mDeviceName=CC2650 SensorTag], mRssi=-33,
mTimestampNanos=2904217657384005}
08-20 13:02:16.551 6243-6252/it.adacto.mysensortag D/ScanRecord:
parseFromBytes
first manudata for manu ID
08-20 13:02:16.551 6243-6252/it.adacto.mysensortag
D/BluetoothLeScanner: onScanResult() -
ScanResult{mDevice=24:71:89:BC:81:80, mScanRecord=ScanRecord
[mAdvertiseFlags=5, mServiceUuids=[0000aa80-0000-1000-8000-
00805f9b34fb], mManufacturerSpecificData={13=[3, 0, 1]}, mServiceData=
{}, mTxPowerLevel=0, mDeviceName=CC2650 SensorTag], mRssi=-33,
mTimestampNanos=2904217761182697}
08-20 13:02:16.611 6243-6253/it.adacto.mysensortag D/ScanRecord: parseFromBytes
08-20 13:02:16.611 6243-6253/it.adacto.mysensortag
D/BluetoothLeScanner: onScanResult() -
ScanResult{mDevice=00:13:04:19:CD:B9, mScanRecord=ScanRecord [mAdvertiseFlags=6, mServiceUuids=[0000ffe0-0000-1000-8000-00805f9b34fb,
0000fea3-0000-1000-8000-00805f9b34fb, 0000180a-0000-1000-8000-
00805f9b34fb], mManufacturerSpecificData={}, mServiceData={0000525d-0000-1000-8000-00805f9b34fb=[-98, -18]}, mTxPowerLevel=-2147483648,
mDeviceName=Beurer BF700], mRssi=-74, mTimestampNanos=2904217817201082}
08-20 13:02:16.661 6243-6252/it.adacto.mysensortag D/ScanRecord: parseFromBytes
编辑: 我可以使用以下方法检查权限:
checkPermissions(){
if (ActivityCompat.checkSelfPermission(AndroidPlugin.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(AndroidPlugin.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(AndroidPlugin.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
return;
}else{
Log.i(TAG,"PERMISSION ALREADY GIVEN ");
}
}