Cas*_*per 4 android bluetooth android-permissions android-6.0-marshmallow
目标
发现所有可用的蓝牙设备,例如我的iPad蓝牙ON(可发现).
ANDROID VERSION
6
问题
无法发现任何蓝牙设备.
码
// Permission
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
public BroadcastReceiver mReceiver;
public IntentFilter filter;
private boolean discover_AvailableBluetoothDevice() {
mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "onReceive Called");
String action = intent.getAction();
// When discovery finds a device
if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
Log.d(TAG, "ACTION_DISCOVERY_STARTED");
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
Log.d(TAG, "ACTION_DISCOVERY_FINISHED");
} else if (BluetoothDevice.ACTION_FOUND.equals(action)) {
Log.d(TAG, "ACTION_FOUND");
// Get the BluetoothDevice object from the Intent
BluetoothDevice device = (BluetoothDevice) intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// Add the name and address to an array adapter to show in a ListView
String str = device.getName() + "\n( " + device.getAddress() + " )";
adapter.add(str);
}
}
};
filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(mReceiver, filter);
boolean isSuccessDiscovery = mBluetoothAdapter.startDiscovery();
return isSuccessDiscovery;
}
Run Code Online (Sandbox Code Playgroud)
执行结果(logcat)
02-02 01:17:26.142 7194-7194/eie.imt.smartswitch D/†MainActivity: This device support Bluetooth.
02-02 01:17:55.052 7194-7194/eie.imt.smartswitch D/†ConnectSwitchActivity: isSuccessDiscovery=true
02-02 01:17:55.147 7194-7194/eie.imt.smartswitch D/†ConnectSwitchActivity: onReceive Called
02-02 01:17:55.147 7194-7194/eie.imt.smartswitch D/†ConnectSwitchActivity: ACTION_DISCOVERY_STARTED
02-02 01:18:07.909 7194-7194/eie.imt.smartswitch D/†ConnectSwitchActivity: onReceive Called
02-02 01:18:07.910 7194-7194/eie.imt.smartswitch D/†ConnectSwitchActivity: ACTION_DISCOVERY_FINISHED
Run Code Online (Sandbox Code Playgroud)
我看到程序没有进入ACTION_FOUND的条件块但我的iPad的蓝牙是ON,问题来自哪里?
Rai*_*aiv 11
如果您使用的是Android 6,请添加以下权限之一:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCTION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
Run Code Online (Sandbox Code Playgroud)
并打开位置服务(GPS):
并启用位置权限:
Google现在需要ACCESS_FINE_LOCTION或ACCESS_COARCE_LOCATION权限才能扫描蓝牙或Wifi设备.这是一种奇怪的行为,但谷歌表示从现在开始应该如何运作.
| 归档时间: |
|
| 查看次数: |
2256 次 |
| 最近记录: |