gpt4 book ai didi

安卓蓝牙 : startDiscovery() not working

转载 作者:行者123 更新时间:2023-11-29 19:48:35 26 4
gpt4 key购买 nike

import android.app.Service;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.Toast;


public class BluetoothService extends Service {
BroadcastReceiver mReceiver;
final BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
public static final String TAG = "bluetooth_service";

@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public void onCreate() {
super.onCreate();
this.mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.d(TAG, action);
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Log.d(TAG, device.getName());
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
if (mBluetoothAdapter != null) {
if (mBluetoothAdapter.isEnabled()) {
mBluetoothAdapter.disable();
}
}
stopSelf();
}
}
};
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
this.registerReceiver(this.mReceiver, filter);

}

@Override
public void onDestroy() {
super.onDestroy();
this.unregisterReceiver(this.mReceiver);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (mBluetoothAdapter != null) {
if (!mBluetoothAdapter.isEnabled()) {
mBluetoothAdapter.enable();
if (mBluetoothAdapter.isDiscovering()) {
mBluetoothAdapter.cancelDiscovery();
}
mBluetoothAdapter.startDiscovery();
}
}
return START_STICKY;
}
}

我正在运行一项检测蓝牙设备的服务。在接收器中,我能够检测到 ACTION_DISCOVERY_STARTEDACTION_DISCOVERY_FINISHED。它有时也会检测到 ACTION_FOUND。但并不是所有的蓝牙设备都被看到。

如果我转到设置>蓝牙>更多设置>刷新。现在所有设备都显示在日志中。它仍然命中 DISCOVERY_STARTEDDISCOVERY_FINISHED

最佳答案

Android 6.0 需要额外的权限才能发现范围内的设备。添加权限如下。

private void discoverDevices() {
if (mBluetoothAdapter.isDiscovering()) {
mBluetoothAdapter.cancelDiscovery();
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
switch (ContextCompat.checkSelfPermission(mActivity, Manifest.permission.ACCESS_COARSE_LOCATION)) {
case PackageManager.PERMISSION_DENIED:
ActivityCompat.requestPermissions(mActivity,
new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
REQUEST_ACCESS_COARSE_LOCATION);

break;
case PackageManager.PERMISSION_GRANTED:
mBluetoothAdapter.startDiscovery();
break;
}
}

}

@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case REQUEST_ACCESS_COARSE_LOCATION: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
mBluetoothAdapter.startDiscovery();
}
else {
//exit application or do the needful
}
return;
}
}
}

关于安卓蓝牙 : startDiscovery() not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37384199/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com