蓝牙开始发现没有给出结果

问题描述 投票:0回答:2

这是我在Android中的第一款应用。我是Android和Java的新手。我虽然是iOS / ObjC的专家。我正在边做边学。所以我直接跳到制作应用程序连接到蓝牙设备。第一步当然是获得范围内可用的蓝牙设备列表。

这是我的清单:

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

这是我的活动代码:

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手机上打开蓝牙,然后运行应用程序显示该日志:

蓝牙初始化开始发现

就这样。其他日志未打印出来。知道为什么吗?我的代码看起来很完美。

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

java android bluetooth discovery
2个回答
1
投票

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


1
投票

如果你的其他设备是蓝牙模块,在你的情况下Arduino,对吧?

如果是这样,请查看本教程,描述Android设备和HC05模块之间的连接。 bthc-05 to android tutorial

此外,根据这个官方样本:google sample - bluetooth chat

或者,您也可以使用以下方法使您的设备可被发现。并将其安装在两部手机上。然后你应该能够至少发现彼此的电话。

protected void makeDiscoverable(){
    // Make local device discoverable
    Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
    discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, DISCOVERABLE_DURATION);
    startActivityForResult(discoverableIntent, DISCOVERABLE_BT_REQUEST_CODE);
}

也许这些帮助!

© www.soinside.com 2019 - 2024. All rights reserved.