蓝牙LeScanner开始扫描仅转到错误代码2



我在手机上打开了蓝牙,我的手机是最新版本的Galaxy S8。 不明白我无法在回拨时获得蓝牙 4.0 设备。

下面是代码片段。

public class MainActivity extends AppCompatActivity {
private BluetoothLeScanner mBLEScanner;
BluetoothAdapter mBluetoothAdapter;
private static final long SCAN_PERIOD = 5000;
Handler mHandler=null;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checkPermission();
BluetoothAdapter bluetoothAdapter =  
BluetoothAdapter.getDefaultAdapter();
if(!bluetoothAdapter.isEnabled()){
bluetoothAdapter.enable();
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mBLEScanner=bluetoothAdapter.getBluetoothLeScanner();
}
mHandler = new Handler();
btn=(Button)findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e("main","now calling bluetooth low energy");
scanLeDevice(true);
}
});
}
void checkPermission(){
String[] permissions = {
Manifest.permission.INTERNET,
Manifest.permission.ACCESS_NETWORK_STATE,
Manifest.permission.BLUETOOTH_ADMIN,
Manifest.permission.BLUETOOTH,
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION,
};
int permissionCheck = PackageManager.PERMISSION_GRANTED;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]. 
{Manifest.permission.READ_PHONE_STATE}, 0);
requestPermissions(new String[]
{Manifest.permission.ACCESS_COARSE_LOCATION}, 1);
}
for (int i = 0; i < permissions.length; i++) {
permissionCheck = ContextCompat.checkSelfPermission(this,      
permissions[i]);
if (permissionCheck == PackageManager.PERMISSION_DENIED) {
Log.e("main", "denied" + permissions[i]);
ActivityCompat.requestPermissions(this, permissions, i);
if  
(ActivityCompat.shouldShowRequestPermissionRationale(this, 
permissions[i])) {
Log.e("main", "shouldshow" + permissions[i]);
} else {
Log.e("main", "requesting" + permissions[i]);
ActivityCompat.requestPermissions(this, permissions, i);
}
break;
} else {
Log.e("main", "granted" + permissions[i]);
}
}

}
@TargetApi(21)
private void scanLeDevice(final boolean enable) {
if (enable) {
//stops scanning after a pre-defined scan period
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
if (Build.VERSION.SDK_INT < 21) {
System.out.println("BLE// mBluetoothAdapter.stopLeScan(mLeScanCallback) ");
mBluetoothAdapter.stopLeScan(mLeScanCallback);
} else {
mBLEScanner.stopScan(mScanCallback);
System.out.println("BLE// mLEScanner.stopScan(mScanCallback) ");
}
}
}, SCAN_PERIOD);
if (Build.VERSION.SDK_INT < 21) {
System.out.println("BLE// mBluetoothAdapter.startLeScan(mLeScanCallback)");
mBluetoothAdapter.startLeScan(mLeScanCallback);
} else {
Log.e("main","starting scan for second"+mBLEScanner);
mBLEScanner.startScan(mScanCallback);
//mLEScanner.startScan(filters, settings, mScanCallback);
}
} else {
if (Build.VERSION.SDK_INT < 21) {
mBluetoothAdapter.stopLeScan(mLeScanCallback);
} else {
mBLEScanner.stopScan(mScanCallback);
}
}
}
private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback() {
@Override
public void onLeScan(final BluetoothDevice device, int rssi,
byte[] scanRecord) {
}
};
private ScanCallback mScanCallback = new ScanCallback() {
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public void onScanResult(int callbackType, ScanResult result) {
super.onScanResult(callbackType, result);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Log.e("main","this name : "+result.getDevice().getName());
Log.e("main","this addr : "+result.getDevice().getAddress());
}
BluetoothDevice btDevice = result.getDevice();
}
@Override
public void onBatchScanResults(List<ScanResult> results) {
}
@Override
public void onScanFailed(int errorCode) {
Log.e("main", "Error Code: " + errorCode);
}
};
}

和日志消息如下。

3:19:59.167 3251-3251/smartiot.ica.org.blelowtest E/main: grantedandroid.permission.INTERNET
06-13 23:19:59.167 3251-3251/smartiot.ica.org.blelowtest E/main: grantedandroid.permission.ACCESS_NETWORK_STATE
06-13 23:19:59.167 3251-3251/smartiot.ica.org.blelowtest E/main: grantedandroid.permission.BLUETOOTH_ADMIN
06-13 23:19:59.168 3251-3251/smartiot.ica.org.blelowtest E/main: grantedandroid.permission.BLUETOOTH
06-13 23:19:59.168 3251-3251/smartiot.ica.org.blelowtest E/main: deniedandroid.permission.ACCESS_COARSE_LOCATION
06-13 23:19:59.171 3251-3251/smartiot.ica.org.blelowtest E/main: requestingandroid.permission.ACCESS_COARSE_LOCATION
06-13 23:20:15.188 3251-3251/smartiot.ica.org.blelowtest E/main: now calling bluetooth low energy
06-13 23:20:15.189 3251-3251/smartiot.ica.org.blelowtest E/main: starting scan for secondandroid.bluetooth.le.BluetoothLeScanner@e24e0e4
06-13 23:20:15.264 3251-3251/smartiot.ica.org.blelowtest E/main: Error Code: 2

清单代码如下

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="smartiot.ica.org.mbluetooth">
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<application

您是否在清单中声明了 thest,从清单中从谷歌中找到它并添加我刚刚从您的代码中复制的权限

如果不在清单文件中声明,运行时权限不起作用

Manifest.permission.INTERNET,
Manifest.permission.ACCESS_NETWORK_STATE,
Manifest.permission.BLUETOOTH_ADMIN,
Manifest.permission.BLUETOOTH,
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION,

您应该在运行时请求启用蓝牙适配器:

private void sendEnableBLERequestIntent() {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}

在此处捕获 startActivityForResult 响应:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_ENABLE_BT && resultCode == RESULT_OK) {
// your stuff here
}
}

相关内容

  • 没有找到相关文章

最新更新