java - Android 蓝牙发现不记录任何内容

标签 java android bluetooth device

我一直在开发一个需要查找一些可发现的蓝牙设备的应用程序。我遵循了指南并查看了此处的答案并遵循了。但是我似乎无法让手机开始扫描。

以下是完整的 Activity 代码:

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 + "");
        }

当我按下扫描按钮时,会进行以下检查。我被告知该设备没有被 toast 发现。

 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]]

最佳答案

尝试在 list 中添加这些权限:

 <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);
    }

关于java - Android 蓝牙发现不记录任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44545958/

相关文章:

java - 将效果应用于高清图像

java - 编码 ISO-8859-5 的 jedis 或 redis 问题

java - 使用android手机发送TCP命令但没有发送数据

java - ListView 在打开时删除选择

java - 在蓝牙设备搜索期间将 obd 扫描工具与其他蓝牙设备隔离

bluetooth - 消息访问配置文件 (MAP) 与低功耗蓝牙兼容吗?

java - 通过调用生成的代理类来使用 SOAP WSDL

java - 如何在安卓应用中添加暂停/恢复录音功能

Android:如何在带有可绘制圆形图标的ImageView上选择效果 "selectableItemBackground"?

android - 我如何使用 android,以编程方式向连接的蓝牙设备发送振动命令?