2

所以,最近我按照这一系列教程http://www.youtube.com/playlist?list=PL2cK5QO_pN1gfEcWZ8tCWUb-WyxAiMyIK 使用蓝牙模块 HC-05 连接 Arduino 和 Android

我完全按照他的计划做了,蓝牙模块在我的 android 上检测为 HC-05,但不会配对。红色 LED 一直闪烁。如 http://mcuoneclipse.com/2013/06/19/using-the-hc-06-bluetooth-module/表示模块上的红色LED指示状态:闪烁:准备配对常亮:已配对

这是我应该在设备名称旁边输出“(配对)”的代码

receiver = new BroadcastReceiver(){
            @Override
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
                if (BluetoothDevice.ACTION_FOUND.equals(action)){
                    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                    devices.add(device);
                    String s = "";
                    for(int a=0;a<pairedDevices.size();a++){
                        if (device.getName().equals(pairedDevices.get(a))){
                            //append
                            s = "(Paired)";
                            break;
                        }
                    }
                    listAdapter.add(device.getName()+" "+s+" "+"\n"+device.getAddress());

                }else if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)){

                }else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){

                }else if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)){
                    if (btAdapter.getState() == btAdapter.STATE_OFF){
                        turnOnBT();
                    }
                }  
            }

        };

相反,我得到了祝酒词,说设备未配对

@Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // TODO Auto-generated method stub
        if (btAdapter.isDiscovering()){
            btAdapter.cancelDiscovery();
        }
        if (listAdapter.getItem(arg2).contains("(Paired)")){

            BluetoothDevice selectedDevice = devices.get(arg2);
            ConnectThread connect = new ConnectThread(selectedDevice);
            connect.start();
        }else {
            Toast.makeText(getApplicationContext(), "device is not paired", 0).show();
        }
    }

我错过了什么?注意:我正在使用,外部电源模块 HC-05 有两个芯片(视频上只有一个芯片)Arduino UNO(视频上使用 Android Pro Mini)

4

2 回答 2

2

我找到了自己的答案,在使用我们自己的应用程序在 android 中连接之前,我们必须先从系统设置>蓝牙>输入蓝牙模块的密码(在我的情况下为 1234)配对它

于 2013-11-05T09:09:11.017 回答
0

您还可以通过代码连接您的 hc 05,而不是从设置中配对。您只是在代码中犯了一个错误:在 onItemClick 方法中,您正在检查设备是否已配对,然后调用连接线程,如果未配对,则显示吐司说设备未配对,但这没有任何意义..您应该连接如果它不包含“配对”,则创建 connectThread 对象并调用 connect 方法。希望这有效!如果没有,请告诉我。

于 2015-07-01T09:51:50.457 回答