java - 蓝牙启动发现未给出结果

标签 java android bluetooth discovery

这是我在 Android 中的第一个应用程序。我对 Android 和 Java 非常陌生。不过我是 iOS/ObjC 方面的专家。我边做边学。所以我直接开始制作一个连接蓝牙设备的应用程序。第一步当然是获取范围内可用的蓝牙设备的列表。

这是我的 list :

<?xml version="1.0" encoding="utf-8"?>
<manifest...>

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

这是我的 Activity 代码:

private BluetoothAdapter btAdapter;

    @Override
    public void onDestroy() {
        super.onDestroy();
       // Unregister broadcast listeners
        unregisterReceiver(mReceiver);
    }


    /*-------------  ON CREATE ------------------------------*/
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btAdapter =  BluetoothAdapter.getDefaultAdapter();
        if (btAdapter == null) {
            System.out.println ("Bluetooth non support");
        } else {
            System.out.println ("Bluetooth initialized");
        }

        IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
        registerReceiver(mReceiver, filter);

        IntentFilter filterDevice = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        registerReceiver(mReceiver, filterDevice);

        if (btAdapter.isEnabled()) {
            String mydeviceaddress = btAdapter.getAddress();
            String mydevicename = btAdapter.getName();

            String status = mydevicename + " : " + mydeviceaddress;
            System.out.println(status);
            System.out.println ("Start discover");
            btAdapter.startDiscovery();
        } else {
            System.out.println ("Not enabled");
            Intent enableBT = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBT, 1);
        }
    }


    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            final String action = intent.getAction();

            if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
                final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,
                        BluetoothAdapter.ERROR);
                switch (state) {
                    case BluetoothAdapter.STATE_OFF:
                        System.out.println("1");
                        break;
                    case BluetoothAdapter.STATE_TURNING_OFF:
                        System.out.println("2");
                        break;
                    case BluetoothAdapter.STATE_ON:
                        System.out.println("3");
                        // SCAN HERE
                        btAdapter.startDiscovery();
                        break;
                    case BluetoothAdapter.STATE_TURNING_ON:
                        System.out.println("4");
                        break;
                }
            }

            if (BluetoothDevice.ACTION_FOUND.equals(action))
            {
                // Get the BluetoothDevice object from the Intent
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                // Add the name and address to an array adapter to show in a ListView
                System.out.println(device.getName() + "\n" + device.getAddress());
            } else {
                System.out.println("What de fuq");
            }

        }
    };

我在 Android 手机上打开蓝牙,然后运行应用程序显示该日志:

Bluetooth initialized Start discover

仅此而已。其他日志不打印出来。知道为什么吗?我的代码看起来很完美,我不知道。

编辑:Android 检测到的蓝牙模块 HC-05 的屏幕截图。 enter image description here

最佳答案

其他设备可能未处于可发现模式。确保它们是可发现的。

关于java - 蓝牙启动发现未给出结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55279965/

相关文章:

java - 同步VS strip 锁

java - 如何使用提供的 .cer 文件解码 JWT RS256 token ?

安卓工作室 : Activity preview does not show title bar

android - 保存和恢复 View 状态android

linux - BlueZ:不使用 sdptool 命令添加服务、属性和配置文件

bluetooth - 如何使用基于HM-10蓝牙模块的BLE Shield?

java - 使用 freemarker 配置构建代码时出现 ClassNotFoundException : freemarker. cache.TemplateLoader --updated

java - Maven EAR 插件,重命名应用程序

当键盘出现在 EditText 的 Click 上时,Android Scrollview 不滚动

c++ - 如何使用 Qt 检测蓝牙崩溃或关机?