Java 颤振Android BluetoothDevice.Action\u发现不工作
我正在使用flatter和Java本机代码进行蓝牙扫描。我已经设置了Java 颤振Android BluetoothDevice.Action\u发现不工作,java,android,flutter,Java,Android,Flutter,我正在使用flatter和Java本机代码进行蓝牙扫描。我已经设置了BroadcastReceiver,可以看到BluetoothAdapter。操作\u发现\u已启动和BluetoothAdapter。操作\u发现\u已完成被触发,但BluetoothDevice。操作\u发现不工作。有人能帮我吗 我目前正在安卓8.1.0手机上测试扫描。我已把这个计划付诸实施 AndroidManifest.xml文件: <manifest xmlns:android="http://sche
BroadcastReceiver
,可以看到BluetoothAdapter。操作\u发现\u已启动
和BluetoothAdapter。操作\u发现\u已完成
被触发,但BluetoothDevice。操作\u发现
不工作。有人能帮我吗
我目前正在安卓8.1.0手机上测试扫描。我已把这个计划付诸实施
AndroidManifest.xml文件:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.mobile">
<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" />
<application android:name="io.flutter.app.FlutterApplication" android:label="mobile" android:icon="@mipmap/ic_launcher">
<activity android:name=".MainActivity" android:launchMode="singleTop" android:theme="@style/LaunchTheme" android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode" android:hardwareAccelerated="true" android:windowSoftInputMode="adjustResize">
<meta-data android:name="io.flutter.embedding.android.NormalTheme" android:resource="@style/NormalTheme" />
<meta-data android:name="io.flutter.embedding.android.SplashScreenDrawable" android:resource="@drawable/launch_background" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data android:name="flutterEmbedding" android:value="2" />
</application>
</manifest>
Java本机代码
android {
compileSdkVersion 29
lintOptions {
disable 'InvalidPackage'
}
defaultConfig {
applicationId "com.example.mobile"
minSdkVersion 16
targetSdkVersion 29
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
}
}
// scan
private void scanBluetoothDevices() {
IntentFilter filter = new IntentFilter();
filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
registerReceiver(receiver, filter);
boolean isDiscovering = bluetoothAdapter.isDiscovering();
if (isDiscovering) {
System.out.println("Discovering in progress...");
bluetoothAdapter.cancelDiscovery();
}
try {
System.out.println("Start to do discovery...");
boolean a = bluetoothAdapter.startDiscovery();
System.out.println(a);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
// Create a BroadcastReceiver for ACTION_FOUND.
private final BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
try {
String onReceiveAction = "current action: " + action;
System.out.println(onReceiveAction);
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
System.out.println("Action Found..........");
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
String deviceName = device.getName();
String deviceHardwareAddress = device.getAddress(); // MAC
// address
System.out.println("***********************************");
System.out.println(deviceName);
System.out.println(deviceHardwareAddress);
} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
System.out.println("Discovery Finished..........");
scanResult();
} else if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) {
System.out.println("Discovery Started..........");
}
} catch (Exception e) {
// TODO: handle exception
System.out.println("got error in onReceive");
e.printStackTrace();
}
}
};
private void scanResult() {
bluetoothAdapter.cancelDiscovery();
unregisterReceiver(receiver);
}