我一直在开发一款需要找到一些可发现蓝牙设备的应用。我跟着导游,在这里看了答案,然后跟着。但是我似乎无法让手机开始扫描。
以下是完整的活动代码:
public class MainActivity extends AppCompatActivity {
private TextView scanningText;
private ListView scanningListView;
private Button scanningButton;
boolean scanComplete;
private final static int REQUEST_ENABLE_BT = 1;
private ArrayList<String> mDeviceList;
private BluetoothAdapter mBluetoothAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDeviceList = new ArrayList<>();
scanningButton = (Button) findViewById(R.id.scanningButton);
scanningText = (TextView) findViewById(R.id.scanningText);
scanningListView = (ListView) findViewById(R.id.scanningList);
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
scanningText.setText("Your device does not support Bluetooth, Sorry!");
}
else if (!mBluetoothAdapter.isEnabled()) {
scanningText.setText("You need to enable bluetooth to use this app..");
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
// Register for broadcasts when a device is discovered.
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter);
scanningButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
scanningText.setText("Scanning...");
mBluetoothAdapter.startDiscovery();
}
});
}
@Override
protected void onDestroy() {
unregisterReceiver(mReceiver);
super.onDestroy();
}
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent
.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
mDeviceList.add(device.getName() + "\n" + device.getAddress());
Log.i("BT", device.getName() + "\n" + device.getAddress());
scanningListView.setAdapter(new ArrayAdapter<>(context,
android.R.layout.simple_list_item_1, mDeviceList));
}
else {
Log.i("BT", "none"+ "");
}
}
};
}
当我检查android日志控制台时,没有日志。
由于
更新
我已将startDiscovery移至onclick侦听器。
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
if (pairedDevices.size() > 0) {
// There are paired devices. Get the name and address of each paired device.
for (BluetoothDevice device : pairedDevices) {
String deviceName = device.getName();
String deviceHardwareAddress = device.getAddress(); // MAC address
}
Log.i("found", pairedDevices + "");
}
按下扫描按钮时会发生以下检查。我被告知设备没有被吐司发现。
scanningButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
scanningText.setText("Scanning...");
mBluetoothAdapter.startDiscovery();
if (mBluetoothAdapter.isDiscovering()) {
mBluetoothAdapter.isDiscovering();
Toast toast = Toast.makeText(getApplicationContext(), "discovering", Toast.LENGTH_LONG);
toast.show();
}
else {
Toast toast = Toast.makeText(getApplicationContext(), "not discovering", Toast.LENGTH_LONG);
toast.show();
}
}
});
整个日志:
06-14 14:50:13.356 25211-25211/? E/Zygote: v2
06-14 14:50:13.356 25211-25211/? I/libpersona: KNOX_SDCARD checking this for 10319
06-14 14:50:13.356 25211-25211/? I/libpersona: KNOX_SDCARD not a persona
06-14 14:50:13.357 25211-25211/? E/Zygote: accessInfo : 0
06-14 14:50:13.361 25211-25211/? W/SELinux: SELinux selinux_android_compute_policy_index : Policy Index[1], Con:u:r:zygote:s0 SPD:SEPF_SECMOBILE_7.0_0006 RAM:SEPF_SECMOBILE_7.0_0005, [-1 -1 0 1 0 1]
06-14 14:50:13.362 25211-25211/? I/SELinux: SELinux: seapp_context_lookup: seinfo=untrusted, level=s0:c512,c768, pkgname=com.smartscan.app.smartscanapp
06-14 14:50:13.365 25211-25211/? I/art: Late-enabling -Xcheck:jni
06-14 14:50:13.408 25211-25211/com.smartscan.app.smartscanapp W/System: ClassLoader referenced unknown path: /data/app/com.smartscan.app.smartscanapp-2/lib/arm64
06-14 14:50:13.417 25211-25211/com.smartscan.app.smartscanapp I/InstantRun: Instant Run Runtime started. Android package is com.smartscan.app.smartscanapp, real application class is null.
06-14 14:50:13.607 25211-25211/com.smartscan.app.smartscanapp W/System: ClassLoader referenced unknown path: /data/app/com.smartscan.app.smartscanapp-2/lib/arm64
06-14 14:50:13.688 25211-25211/com.smartscan.app.smartscanapp W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
06-14 14:50:13.770 25211-25211/com.smartscan.app.smartscanapp I/found: [C1:20:21:40:9C:65, 8C:C8:CD:BB:95:28, C9:50:76:7B:89:F2, 54:53:ED:A3:B1:70, 88:A3:F2:BE:DE:42, C4:73:1E:02:1F:6B, E4:04:39:29:74:E6]
06-14 14:50:13.826 25211-25250/com.smartscan.app.smartscanapp I/OpenGLRenderer: Initialized EGL, version 1.4
06-14 14:50:13.880 25211-25211/com.smartscan.app.smartscanapp I/InputMethodManager: [IMM] startInputInner - mService.startInputOrWindowGainedFocus
06-14 14:50:22.465 25211-25211/com.smartscan.app.smartscanapp W/System: ClassLoader referenced unknown path: /system/framework/QPerformance.jar
06-14 14:50:22.465 25211-25211/com.smartscan.app.smartscanapp E/BoostFramework: BoostFramework() : Exception_1 = java.lang.ClassNotFoundException: Didn't find class "com.qualcomm.qti.Performance" on path: DexPathList[[],nativeLibraryDirectories=[/system/lib64, /vendor/lib64]]
答案 0 :(得分:0)
您应该首先在每个步骤添加日志。
Log.i("TAG", "Message")
您的清单文件是否包含适当的权限?
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
接下来,您似乎没有检查设备上是否启用了蓝牙。你应该使用这样做的意图。
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBT = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBT, REQUEST_BLUETOOTH);
}
答案 1 :(得分:0)
尝试在清单中添加这些权限:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
此外,我们始终建议您询问用户的权限,所以请转到:
if (Build.VERSION.SDK_INT >= 23 && ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
}
if(Build.VERSION.SDK_INT >= 23 && ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 1);
}